Documentation ¶
Overview ¶
Package setlock implements a very simple locking strategy described at https://redis.io/commands/set.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ErrAlreadyHeld = errors.New("already held")
ErrAlreadyHeld is returned if the lock was already held.
var ErrNotHeld = errors.New("not held")
ErrNotHeld is returned if the lock was not held.
Functions ¶
This section is empty.
Types ¶
type DoFunc ¶
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 ¶
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) Lock ¶
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.