Documentation ¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( ErrLockUnlockFailed = errors.New("lock unlock failed") ErrLockNotObtained = errors.New("lock not obtained") ErrLockDurationExceeded = errors.New("lock duration exceeded") )
ErrLockNotObtained may be returned by Obtain() and Run() if a lock could not be obtained.
Functions ¶
Types ¶
type Locker ¶
type Locker struct {
// contains filtered or unexported fields
}
Locker allows (repeated) distributed locking.
func New ¶
func New(client RedisClient, key string, opts *Options) *Locker
New creates a new distributed locker on a given key.
func Obtain ¶
func Obtain(client RedisClient, key string, opts *Options) (locker *Locker, err error)
Obtain is a shortcut for New().Lock(). It may return ErrLockNotObtained if a lock was not successfully acquired.
func (*Locker) Lock ¶
Lock applies the lock. If error (lock not obtain or something else), don't call Unlock(). If no error, don't forget to call Unlock() function to release the lock after usage.
func (*Locker) LockWithContext ¶
LockWithContext is like Lock but allows to pass an additional context which allows cancelling lock attempts prematurely. If error (lock not obtain or something else), don't call Unlock(). If no error, don't forget to call Unlock() function to release the lock after usage.
type Options ¶
type Options struct { // The maximum duration to lock a key for // Default: 5s LockTimeout time.Duration // The number of time the acquisition of a lock will be retried. // Default: 0 = do not retry RetryCount int // RetryDelay is the amount of time to wait between retries. // Default: 100ms RetryDelay time.Duration // TokenPrefix the redis lock key's value will set TokenPrefix + randomToken // If we set token prefix as hostname + pid, we can know who get the locker TokenPrefix string }
Options describe the options for the lock
type RedisClient ¶
type RedisClient interface { SetNX(key string, value interface{}, expiration time.Duration) *redis.BoolCmd Eval(script string, keys []string, args ...interface{}) *redis.Cmd EvalSha(sha1 string, keys []string, args ...interface{}) *redis.Cmd ScriptExists(scripts ...string) *redis.BoolSliceCmd ScriptLoad(script string) *redis.StringCmd }
RedisClient is a minimal client interface.