setlock

package module
v0.0.0-...-29770f0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 20, 2019 License: BSD-2-Clause Imports: 6 Imported by: 0

README

setlock

Simple lock implemented through Redis using SET+GET/DEL

Documentation

Overview

Package setlock implements a very simple locking strategy described at https://redis.io/commands/set.

Index

Constants

This section is empty.

Variables

View Source
var ErrAlreadyHeld = errors.New("already held")

ErrAlreadyHeld is returned if the lock was already held.

View Source
var ErrNotHeld = errors.New("not held")

ErrNotHeld is returned if the lock was not held.

Functions

This section is empty.

Types

type DoFunc

type DoFunc func(ctx context.Context) error

DoFunc is a function that is executed while the associated lock automatically extended.

type Lock

type Lock struct {
	// Key is the Redis key used.
	Key string

	// Pool is the Redis connection pool used.
	Pool *redis.Pool
	// contains filtered or unexported fields
}

Lock is a simple SET+GET/DEL Redis lock. The lock is safe for concurrent use.

func (*Lock) Do

func (l *Lock) Do(ctx context.Context, f DoFunc) error

Do acquires lock l and executes f and releases afterwards. If lock cannot be acquired, ErrNotHeld is returned. The context that is passed to f is cancelled if the lock is released because of some error condition.

func (*Lock) Extend

func (l *Lock) Extend(ctx context.Context, d time.Duration) error

Extend extends lock l.

func (*Lock) Lock

func (l *Lock) Lock(ctx context.Context, d time.Duration) (time.Duration, error)

Lock tries to lock for a specified duration d. If the returned duration is != 0 and the returned error is ErrNotHeld, this signals that the caller is not holding the lock. If ErrAlreadyHeld is returned with a non 0 duration, this signals that the caller is already holding the lock and for the returned duration left.

If duration d is less than a millisecond the lock will be held for a millisecond.

func (*Lock) Unlock

func (l *Lock) Unlock(ctx context.Context) error

Unlock unlocks lock l.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL