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

# 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 github.com/go-redsync/redsync

Two driver implementations will be installed; however, only the one used will be include 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 (
	"github.com/go-redsync/redsync"
	"github.com/go-redsync/redsync/v3/redis"
	"github.com/go-redsync/redsync/v3/redis/redigo"
	"github.com/gomodule/redigo/redis"
)

func main() {
	// Create a pool with redigo which is the pool redisync will use while
	// communicating with Redis. This can be any pool that implements the `Pool`
	// interface which just requres a `Get()` method that returns a
	// `redis.Conn`.
	redisPool := &redis.Pool{
		Dial: func() (redis.Conn, error) {
			return redis.DialURL("redis://localhost:6379/0")
		},
	}

	// Ensure we're connected to Redis before we try to obtain the lock.
	if _, err := redisPool.Get().Do("PING"); err != nil {
		panic(err)
	}
	

	// Create an instance of redisync to be used to obtain a mutual exclusion
	// lock.
	pool := goredis.NewGoredisPool(client) // or, pool := redigo.NewRedigoPool(...)
	rs := redsync.New([]redis.Pool{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. Use any preferred way to query the
	// Redis database.
	if _, err := pool.Get().Do("SET", "some-key-to-be-set", 1); err != nil {
		panic(err)
	}

	// Release the lock so other processess 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.

# 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.
SetDriftFactor can be used to set the clock drift factor.
SetExpiry can be used to set the expiry of a mutex to the given value.
SetGenValueFunc can be used to set the custom value generator.
SetRetryDelay can be used to set the amount of time to wait between retries.
SetRetryDelayFunc can be used to override default delay behavior.
SetTries can be used to set the number of times lock acquire is attempted.

# Variables

No description provided by the author

# Structs

A Mutex is a distributed mutual exclusion lock.
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.