Root

github.com/go-redsync/redsync/v4

Package redsync provides a Redis-based distributed mutual exclusion lock implementation as described in the post http://redis.io/topics/distlock.

import "github.com/go-redsync/redsync/v4"

Package redsync provides a Redis-based distributed mutual exclusion lock implementation as described in the post http://redis.io/topics/distlock.

Values containing the types defined in this package should not be copied.

Variables

ErrExtendFailed is the error resulting if Redsync fails to extend the lock.

Source: error.go:14

var ErrExtendFailed = errors.New("redsync: failed to extend lock")

ErrFailed is the error resulting if Redsync fails to acquire the lock after exhausting all retries.

Source: error.go:10

var ErrFailed = errors.New("redsync: failed to acquire lock")

ErrLockAlreadyExpired is the error resulting if trying to unlock the lock which already expired.

Source: error.go:17

var ErrLockAlreadyExpired = errors.New("redsync: failed to unlock, lock was already expired")

Functions

func New(pools ...redis.Pool) *Redsync

Source: redsync.go:22

New creates and returns a new Redsync instance from given Redis connection pools.

func WithDriftFactor(factor float64) Option

Source: redsync.go:112

WithDriftFactor can be used to set the clock drift factor. The default value is 0.01.

func WithExpiry(expiry time.Duration) Option

Source: redsync.go:69

WithExpiry can be used to set the expiry of a mutex to the given value. The default is 8s.

func WithFailFast(b bool) Option

Source: redsync.go:145

WithFailFast can be used to quickly acquire and release the lock. When some Redis servers are blocking, we do not need to wait for responses from all the Redis servers response. As long as the quorum is met, we can assume the lock is acquired. The effect of this parameter is to achieve low latency, avoid Redis blocking causing Lock/Unlock to not return for a long time.

func WithGenValueFunc(genValueFunc func() (string, error)) Option

Source: redsync.go:127

WithGenValueFunc can be used to set the custom value generator.

func WithRetryDelay(delay time.Duration) Option

Source: redsync.go:85

WithRetryDelay can be used to set the amount of time to wait between retries. The default value is rand(50ms, 250ms).

func WithRetryDelayFunc(delayFunc DelayFunc) Option

Source: redsync.go:104

WithRetryDelayFunc can be used to override default delay behavior.

func WithSetNXOnExtend() Option

Source: redsync.go:97

WithSetNXOnExtend improves extending logic to extend the key if exist and if not, tries to set a new key in redis Useful if your redises restart often and you want to reduce the chances of losing the lock Read this MR for more info: https://github.com/go-redsync/redsync/pull/149

func WithShufflePools(b bool) Option

Source: redsync.go:152

WithShufflePools can be used to shuffle Redis pools to reduce centralized access in concurrent scenarios.

func WithTimeoutFactor(factor float64) Option

Source: redsync.go:120

WithTimeoutFactor can be used to set the timeout factor. The default value is 0.05.

func WithTries(tries int) Option

Source: redsync.go:77

WithTries can be used to set the number of times lock acquire is attempted. The default value is 32.

func WithValue(v string) Option

Source: redsync.go:135

WithValue can be used to assign the random value without having to call lock. This allows the ownership of a lock to be "transferred" and allows the lock to be unlocked from elsewhere.

Types

type DelayFunc

Source: mutex.go:14

A DelayFunc is used to decide the amount of time to wait between retries.

type DelayFunc func(tries int) time.Duration

type ErrNodeTaken

Source: error.go:30

ErrNodeTaken is the error resulting if the lock is already taken in one of the cluster's nodes

type ErrNodeTaken struct {
	Node int
}
Fields
  • Node int

func Error() string

Source: error.go:34

type ErrTaken

Source: error.go:20

ErrTaken happens when the lock is already taken in a quorum on nodes.

type ErrTaken struct {
	Nodes []int
}
Fields
  • Nodes []int

func Error() string

Source: error.go:24

type Mutex

Source: mutex.go:17

A Mutex is a distributed mutual exclusion lock.

type Mutex struct {
	// contains filtered or unexported fields
}

func Extend() (bool, error)

Source: mutex.go:153

Extend resets the mutex's expiry and returns the status of expiry extension.

func ExtendContext(ctx context.Context) (bool, error)

Source: mutex.go:158

ExtendContext resets the mutex's expiry and returns the status of expiry extension.

func Lock() error

Source: mutex.go:65

Lock locks m. In case it returns an error on failure, you may retry to acquire the lock by calling this method again.

func LockContext(ctx context.Context) error

Source: mutex.go:70

LockContext locks m. In case it returns an error on failure, you may retry to acquire the lock by calling this method again.

func Name() string

Source: mutex.go:40

Name returns mutex name (i.e. the Redis key).

func TryLock() error

Source: mutex.go:55

TryLock only attempts to lock m once and returns immediately regardless of success or failure without retrying.

func TryLockContext(ctx context.Context) error

Source: mutex.go:60

TryLockContext only attempts to lock m once and returns immediately regardless of success or failure without retrying.

func Unlock() (bool, error)

Source: mutex.go:137

Unlock unlocks m and returns the status of unlock.

func UnlockContext(ctx context.Context) (bool, error)

Source: mutex.go:142

UnlockContext unlocks m and returns the status of unlock.

func Until() time.Time

Source: mutex.go:50

Until returns the time of validity of acquired lock. The value will be zero value until a lock is acquired.

func Valid() (bool, error)

Source: mutex.go:180

Valid returns true if the lock acquired through m is still valid. It may also return true erroneously if quorum is achieved during the call and at least one node then takes long enough to respond for the lock to expire.

Deprecated: Use Until instead. See https://github.com/go-redsync/redsync/issues/72.

func ValidContext(ctx context.Context) (bool, error)

Source: mutex.go:189

ValidContext returns true if the lock acquired through m is still valid. It may also return true erroneously if quorum is achieved during the call and at least one node then takes long enough to respond for the lock to expire.

Deprecated: Use Until instead. See https://github.com/go-redsync/redsync/issues/72.

func Value() string

Source: mutex.go:45

Value returns the current random value. The value will be empty until a lock is acquired (or WithValue option is used).

type Option

Source: redsync.go:55

An Option configures a mutex.

type Option interface {
	Apply(*Mutex)
}
Methods
  • Apply func(*Mutex)

type OptionFunc

Source: redsync.go:60

OptionFunc is a function that configures a mutex.

type OptionFunc func(*Mutex)

func Apply(mutex *Mutex)

Source: redsync.go:63

Apply calls f(mutex)

type RedisError

Source: error.go:39

A RedisError is an error communicating with one of the Redis nodes.

type RedisError struct {
	Node int
	Err  error
}
Fields
  • Node int
  • Err error

func Error) Error() string

Source: error.go:44

func Unwrap() error

Source: error.go:48

type Redsync

Source: redsync.go:17

Redsync provides a simple method for creating distributed mutexes using multiple Redis connection pools.

type Redsync struct {
	// contains filtered or unexported fields
}

func NewMutex(name string, options ...Option) *Mutex

Source: redsync.go:29

NewMutex returns a new distributed mutex with given name.