redlock

package module
v0.0.0-...-4cb36d0 Latest Latest
Warning

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

Go to latest
Published: Feb 26, 2023 License: MIT Imports: 8 Imported by: 0

README

koron-go/redlock

GoDoc CircleCI Go Report Card

An experimental implementation of Distributed Locks with Redis

How to test

$ docker run --rm -it -p 6379:6379 --name mem-redis redis:7.0.8-alpine3.17
$ go test

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// DefaultRetryCount is default max retry count to lock.
	DefaultRetryCount = 3

	// DefaultRetryDelay is default delay when retry to lock.
	DefaultRetryDelay = 200 * time.Millisecond

	// DefaultExpiration is default expiration time for Mutex's lock.
	DefaultExpiration = 30 * time.Second
)
View Source
var (
	// ErrLockedAlready occurs when previous lock is not released.
	ErrLockedAlready = errors.New("locked already")

	// ErrGaveUpLock ocurrs when Lock gave up.
	ErrGaveUpLock = errors.New("gave up lock")
)

Functions

func Lock

func Lock(a Adapter, key, id string, expiration time.Duration) (bool, error)

Lock locks a key with id against an adapter.

func Unlock

func Unlock(a Adapter, key, id string) error

Unlock unlocks a key with id against an adapter.

Types

type Adapter

type Adapter interface {
	SetNX(key string, val string, expiration time.Duration) (bool, error)
	Eval(script string, key []string, args ...interface{}) error
}

Adapter defines requirements for redis connection.

type Mutex

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

Mutex provides distributed locks with Redis (a.k.a. redlock). See https://redis.io/topics/distlock for details.

func New

func New(key string, adapters ...Adapter) *Mutex

New creates a Mutex instance.

func NewWithRedis

func NewWithRedis(key string, clients ...redis.Cmdable) *Mutex

NewWithRedis creates Mutex from some "github.com/go-redis/redis".Cmdable implementations.

func (*Mutex) Lock

func (m *Mutex) Lock() error

Lock tries to lock.

func (*Mutex) SetExpiration

func (m *Mutex) SetExpiration(ex time.Duration)

SetExpiration modifies expiration time of locked key.

func (*Mutex) SetRetryCount

func (m *Mutex) SetRetryCount(n int)

SetRetryCount modifies max retry count to lock.

func (*Mutex) SetRetryDelay

func (m *Mutex) SetRetryDelay(d time.Duration)

SetRetryDelay modifies delay for retry to lock.

func (*Mutex) Unlock

func (m *Mutex) Unlock()

Unlock releases a lock.

Jump to

Keyboard shortcuts

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