redsync

package module
v2.3.0 Latest Latest
Warning

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

Go to latest
Published: Nov 30, 2022 License: BSD-3-Clause Imports: 9 Imported by: 0

README

Redsync

Build Status

Redsync provides a Redis-based distributed mutual exclusion lock implementation for Go as described in this post. A reference library (by antirez) for Ruby is available at github.com/antirez/redlock-rb.

Installation

Install Redsync using the go get command:

$ go get gopkg.in/redsync.v1

The only dependencies are the Go distribution and Redigo.

NOTICE: Do NOT use versions after v2.0.0 and before v2.2.1

Documentation

Contributing

Contributions are welcome.

License

Redsync is available under the BSD (3-Clause) License.

Disclaimer

This code implements an algorithm which is currently a proposal, it was not formally analyzed. Make sure to understand how it works before using it in production environments.

Documentation

Overview

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.

Index

Constants

This section is empty.

Variables

View Source
var ErrTaken = errors.New(takenMsg)

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

Functions

This section is empty.

Types

type DelayFunc

type DelayFunc func(tries int) time.Duration

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

type Mutex

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

A Mutex is a distributed mutual exclusion lock.

func (*Mutex) Extend

func (m *Mutex) Extend() error

Extend resets the mutex's expiry.

If not nil, the error will be either ErrTaken or a NoQuorum, which in turn contains NodeTaken or RedisError.

func (*Mutex) Lock

func (m *Mutex) Lock() error

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

If not nil, the error will be either ErrTaken or a NoQuorum, which in turn contains NodeTaken or RedisError.

Only the last try's errors are returned.

func (*Mutex) Unlock

func (m *Mutex) Unlock() error

Unlock unlocks m.

If not nil, the error will be either ErrTaken or a NoQuorum, which in turn contains NodeTaken or RedisError.

type NoQuorum

type NoQuorum []error

NoQuorum is a series of errors, either NodeTaken or RedisError, that happened while trying to acquire or extend a lock in a cluster and prevent the lock to be taken in a quorum of nodes.

func (NoQuorum) Error

func (errs NoQuorum) Error() string

type NodeTaken

type NodeTaken struct {
	Node int
}

A NodeTaken happens when the lock is already taken in one of the cluster's nodes.

func (NodeTaken) Error

func (err NodeTaken) Error() string

type Option

type Option interface {
	Apply(*Mutex)
}

An Option configures a mutex.

func SetDriftFactor

func SetDriftFactor(factor float64) Option

SetDriftFactor can be used to set the clock drift factor.

func SetExpiry

func SetExpiry(expiry time.Duration) Option

SetExpiry can be used to set the expiry of a mutex to the given value.

func SetRetryDelay

func SetRetryDelay(delay time.Duration) Option

SetRetryDelay can be used to set the amount of time to wait between retries.

func SetRetryDelayFunc

func SetRetryDelayFunc(delayFunc DelayFunc) Option

SetRetryDelayFunc can be used to override default delay behavior.

func SetTries

func SetTries(tries int) Option

SetTries can be used to set the number of times lock acquire is attempted.

type OptionFunc

type OptionFunc func(*Mutex)

OptionFunc is a function that configures a mutex.

func (OptionFunc) Apply

func (f OptionFunc) Apply(mutex *Mutex)

Apply calls f(mutex)

type Pool

type Pool interface {
	GetContext(ctx context.Context) (redis.Conn, error)
}

A Pool maintains a pool of Redis connections.

type RedisError

type RedisError struct {
	Node int
	Err  error
}

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

func (RedisError) Error

func (err RedisError) Error() string

type Redsync

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

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

func New

func New(pools []Pool) *Redsync

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

func (*Redsync) NewMutex

func (r *Redsync) NewMutex(ctx context.Context, name string, options ...Option) *Mutex

NewMutex returns a new distributed mutex with given name.

Jump to

Keyboard shortcuts

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