Documentation
¶
Overview ¶
Package netlock provides simple mutex-like primitive that uses Redis server to coordinate.
It implements the algorithm described at https://redis.io/commands/set#patterns
Caller tries to set unique key with given TTL (upper estimate of time to hold the mutex), if it succeeds, it can proceed with its work using context to check whether TTL expired and cancel function to both cancel the context and release the lock.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ErrLocked = errors.New("lock is already held")
ErrLocked is the error returned by Acquire if key already exists
Functions ¶
func Acquire ¶
func Acquire(ctx context.Context, conn io.ReadWriter, name string, ttl time.Duration) (context.Context, context.CancelFunc, error)
Acquire attempts to acquire shared lock with given name on existing connection conn to Redist host. On success, the key name is set to expire after ttl or when context is canceled (if network connection is still alive). Returned context is set to cancel after ttl. CancelFunc should be used to release lock before it expires.
On success conn is bound to CancelFunc so it shouldn't be reused until CancelFunc is called otherwise it would break Redis session state. Normally conn should be created by caller specifically to be used by Acquire and closed once CancelFunc is called.
Types ¶
This section is empty.