redsync

package module
v1.5.0 Latest Latest
Warning

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

Go to latest
Published: Dec 28, 2022 License: BSD-3-Clause Imports: 10 Imported by: 0

README

Redsync

Go Reference 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 github.com/go-redsync/redsync/v4

Two driver implementations will be installed; however, only the one used will be included in your project.

See the examples folder for usage of each driver.

Documentation

Usage

Error handling is simplified to panic for shorter example.

package main

import (
	
	goredislib "github.com/go-redis/redis/v8"
	"github.com/go-weylan/redsync/v4"
	"github.com/weylan/redsync/redis/goredis/v8"
)

func main() {
	// Create a pool with go-redis (or redigo) which is the pool redisync will
	// use while communicating with Redis. This can also be any pool that
	// implements the `redis.Pool` interface.
	client := goredislib.NewClient(&goredislib.Options{
		Addr: "localhost:6379",
	})
	pool := goredis.NewPool(client) // or, pool := redigo.NewPool(...)

	// Create an instance of redisync to be used to obtain a mutual exclusion
	// lock.
	rs := redsync.New(pool)

	// Obtain a new mutex by using the same name for all instances wanting the
	// same lock.
	mutexname := "my-global-mutex"
	mutex := rs.NewMutex(mutexname)

	// Obtain a lock for our given mutex. After this is successful, no one else
	// can obtain the same lock (the same mutex name) until we unlock it.
	if err := mutex.Lock(); err != nil {
		panic(err)
	}

	// Do your work that requires the lock.

	// Release the lock so other processes or threads can obtain a lock.
	if ok, err := mutex.Unlock(); !ok || err != nil {
		panic("unlock failed")
	}
}

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.

Real World Uses

Below is a list of public, open source projects that use Redsync:

  • Sourcegraph: Universal code search and intelligence platform. Uses Redsync in an internal cache implementation.
  • Open Match by Google: Flexible, extensible, and scalable video game matchmaking. Uses Redsync with its state store implementation.

If you are using Redsync in a project please send a pull request to add it to the list.

Documentation

Index

Constants

This section is empty.

Variables

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

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

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

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

Functions

This section is empty.

Types

type DelayFunc added in v1.4.3

type DelayFunc func(tries int) time.Duration

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

type ErrNodeTaken added in v1.4.9

type ErrNodeTaken struct {
	Node int
}

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

func (ErrNodeTaken) Error added in v1.4.9

func (err ErrNodeTaken) Error() string

type ErrTaken added in v1.4.9

type ErrTaken struct {
	Nodes []int
}

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

func (ErrTaken) Error added in v1.4.9

func (err ErrTaken) Error() string

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() (bool, error)

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

func (*Mutex) ExtendContext added in v1.4.3

func (m *Mutex) ExtendContext(ctx context.Context) (bool, error)

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

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.

func (*Mutex) LockContext added in v1.4.3

func (m *Mutex) LockContext(ctx context.Context) error

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

func (*Mutex) Name added in v1.4.3

func (m *Mutex) Name() string

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

func (*Mutex) SetVersion added in v1.4.4

func (m *Mutex) SetVersion(version int64) (bool, error)

func (*Mutex) SetVersionContext added in v1.4.4

func (m *Mutex) SetVersionContext(ctx context.Context, version int64) (bool, error)

func (*Mutex) Unlock

func (m *Mutex) Unlock() (bool, error)

Unlock unlocks m and returns the status of unlock.

func (*Mutex) UnlockContext added in v1.4.3

func (m *Mutex) UnlockContext(ctx context.Context) (bool, error)

UnlockContext unlocks m and returns the status of unlock.

func (*Mutex) UnlockVersion added in v1.4.5

func (m *Mutex) UnlockVersion(version int64) (bool, error)

func (*Mutex) UnlockVersionContext added in v1.4.5

func (m *Mutex) UnlockVersionContext(ctx context.Context, version int64) (bool, error)

func (*Mutex) Until added in v1.4.9

func (m *Mutex) Until() time.Time

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

func (*Mutex) Valid deprecated added in v1.4.3

func (m *Mutex) Valid() (bool, error)

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 (*Mutex) ValidContext deprecated added in v1.4.3

func (m *Mutex) ValidContext(ctx context.Context) (bool, error)

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 (*Mutex) Version added in v1.4.3

func (m *Mutex) Version() int64

type Option

type Option interface {
	Apply(*Mutex)
}

An Option configures a mutex.

func WithDriftFactor added in v1.4.3

func WithDriftFactor(factor float64) Option

WithDriftFactor can be used to set the clock drift factor.

func WithExpiry added in v1.4.3

func WithExpiry(expiry time.Duration) Option

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

func WithGenValueFunc added in v1.4.3

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

WithGenValueFunc can be used to set the custom value generator.

func WithRetryDelay added in v1.4.3

func WithRetryDelay(delay time.Duration) Option

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

func WithRetryDelayFunc added in v1.4.3

func WithRetryDelayFunc(delayFunc DelayFunc) Option

WithRetryDelayFunc can be used to override default delay behavior.

func WithTimeoutFactor added in v1.4.9

func WithTimeoutFactor(factor float64) Option

WithTimeoutFactor can be used to set the timeout factor.

func WithTries added in v1.4.3

func WithTries(tries int) Option

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

func WithVersion added in v1.4.3

func WithVersion(v int64) Option

WithVersion 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.

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 RedisError added in v1.4.9

type RedisError struct {
	Node int
	Err  error
}

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

func (RedisError) Error added in v1.4.9

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 ...redis.Pool) *Redsync

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

func (*Redsync) NewMutex

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

NewMutex returns a new distributed mutex with given name.

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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