Categorygithub.com/go-redsync/redsync/v4
modulepackage
4.13.0
Repository: https://github.com/go-redsync/redsync.git
Documentation: pkg.go.dev

# 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/redis/go-redis/v9"
	"github.com/go-redsync/redsync/v4"
	"github.com/go-redsync/redsync/v4/redis/goredis/v9"
)

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.
  • Gocron by go-co-op: gocron is a job distributed scheduling package which lets you run Go functions at pre-determined intervals using a simple, human-friendly syntax. Uses Redsync with its distributed job scheduler implementation.

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

# Packages

No description provided by the author
No description provided by the author

# Functions

New creates and returns a new Redsync instance from given Redis connection pools.
WithDriftFactor can be used to set the clock drift factor.
WithExpiry can be used to set the expiry of a mutex to the given value.
WithFailFast can be used to quickly acquire and release the lock.
WithGenValueFunc can be used to set the custom value generator.
WithRetryDelay can be used to set the amount of time to wait between retries.
WithRetryDelayFunc can be used to override default delay behavior.
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.
WithShufflePools can be used to shuffle Redis pools to reduce centralized access in concurrent scenarios.
WithTimeoutFactor can be used to set the timeout factor.
WithTries can be used to set the number of times lock acquire is attempted.
WithValue can be used to assign the random value without having to call lock.

# Variables

ErrExtendFailed is the error resulting if Redsync fails to extend the lock.
ErrFailed is the error resulting if Redsync fails to acquire the lock after exhausting all retries.
ErrLockAlreadyExpired is the error resulting if trying to unlock the lock which already expired.

# Structs

ErrNodeTaken is the error resulting if the lock is already taken in one of the cluster's nodes.
ErrTaken happens when the lock is already taken in a quorum on nodes.
A Mutex is a distributed mutual exclusion lock.
A RedisError is an error communicating with one of the Redis nodes.
Redsync provides a simple method for creating distributed mutexes using multiple Redis connection pools.

# Interfaces

An Option configures a mutex.

# Type aliases

A DelayFunc is used to decide the amount of time to wait between retries.
OptionFunc is a function that configures a mutex.