redlock

package module
v0.0.1-alpha Latest Latest
Warning

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

Go to latest
Published: Sep 27, 2020 License: Apache-2.0 Imports: 6 Imported by: 0

README

go-redlock

A redis-based distributed lock implemented by Go.

Documentation

Index

Constants

View Source
const (
	// MinLockExpire is the minimum lock expire time.
	MinLockExpire = 1000

	// DefaultLockExpire is the default lock expire time.
	DefaultLockExpire = 3000

	// MaxRefreshRetryTimes is the maximum retry times of
	// auto refresh if the refresh fails continuously.
	MaxRefreshRetryTimes = 100
)

Variables

View Source
var (
	// ErrLockWithoutName is returned when creating a lock with a empty name.
	ErrLockWithoutName = errors.New("empty lock name")

	// ErrLockExpireTooSmall is returned when creating a lock with expire smaller than 300ms.
	ErrLockExpireTooSmall = errors.New("lock expiration too small")

	// ErrAlreadyAcquired is returned when try to lock an already acquired lock.
	ErrAlreadyAcquired = errors.New("lock already acquired")

	// ErrNotAcquired is returned when a lock cannot be acquired.
	ErrNotAcquired = errors.New("lock not acquired")

	// ErrLockNotHeld is returned when trying to release an unacquired lock.
	ErrLockNotHeld = errors.New("lock not held")
)

Functions

This section is empty.

Types

type RedLock

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

RedLock represents a redis lock.

func New

func New(cli *redis.Client, name string, expiration int, autoRefresh bool) (*RedLock, error)

New creates and returns a new redis lock.

Note that a distributed lock without expire time is dangerous, so expire time is always required. If no expire given, say expire <= 0, a default 3 seconds expire time will be used.

Furthermore a very small expire time, such as 5ms, does not make sense at all. The lock possibely has already expired after the caller just acquired it considering the network communication time between the caller and redis server. Always give an expiration larger than 1s.

A lock with autoRefresh enabled will refresh its expiration periodically in a background goroutine so that the lock holder can hold the lock all the time before releasing it. The refresh interval is always the 2/3 of lock expiration.

func (*RedLock) Lock

func (rd *RedLock) Lock(ctx context.Context, block bool) error

Lock locks rd. It returns an error if the lock is not acquired or context timeout is triggered.

func (*RedLock) Unlock

func (rd *RedLock) Unlock(ctx context.Context) error

Unlock unlocks rd.

Jump to

Keyboard shortcuts

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