redislock

package module
v0.0.0-...-e8d168e Latest Latest
Warning

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

Go to latest
Published: Aug 29, 2023 License: MIT Imports: 7 Imported by: 0

README

redis-lock

GitHub watchers GitHub stars GitHub forks GitHub last commit GitHub repo size GitHub license

Distributed lock based on redis.

redis-lock supports watchdog mechanism in redisson.

install

go get

go get -u github.com/XdpCs/redis-lock

go mod

require github.com/XdpCs/redis-lock latest

example

Error handling is simplified to panic for shorter example.

You can run this program in this directory.

package main

import (
	"context"
	"time"

	redislock "github.com/XdpCs/redis-lock"
	"github.com/redis/go-redis/v9"
)

func main() {
	// init context
	ctx := context.Background()
	// init redis client
	rdb := redis.NewClient(&redis.Options{
		Addr: ":6379",
	})
	// close redis client
	defer rdb.Close()
	// flush redis
	_ = rdb.FlushDB(ctx).Err()
	// init redislock client
	client, err := redislock.NewDefaultClient(rdb)
	if err != nil {
		panic(err)
	}
	// try lock with default parameter
	mutex, err := client.TryLock(ctx, "XdpCs", -1)
	if err != nil {
		panic(err)
	}

	defer func(mutex *redislock.Mutex, ctx context.Context) {
		// unlock mutex
		err := mutex.Unlock(ctx)
		if err != nil {
			panic(err)
		}
	}(mutex, ctx)
	time.Sleep(time.Second * 30)
}

License

redis-lock is under the MIT. Please refer to LICENSE for more information.

Documentation

Index

Constants

View Source
const DefaultExpiration = 30 * time.Second

DefaultExpiration is the default expiration of lock.

Variables

View Source
var (
	ErrWatchDogExpiredNotLessThanZero = errors.New("watch dog expired not less than zero")
	ErrWatchDogNotStarted             = errors.New("watch dog not started")
	ErrWatchDogIsNil                  = errors.New("watch dog is nil")
	ErrMutexLockFailed                = errors.New("mutex locks failed")
	ErrMutexNotHeld                   = errors.New("mutex not held")
	ErrMutexNotInitialized            = errors.New("mutex not initialized")
)

Functions

func IsMutexLockFailed

func IsMutexLockFailed(err error) bool

IsMutexLockFailed returns true if err is ErrMutexLockFailed.

func IsMutexNotHeld

func IsMutexNotHeld(err error) bool

IsMutexNotHeld returns true if err is ErrMutexNotHeld.

func IsMutexNotInitialized

func IsMutexNotInitialized(err error) bool

IsMutexNotInitialized returns true if err is ErrMutexNotInitialized.

func IsWatchDogExpiredNotLessThanZero

func IsWatchDogExpiredNotLessThanZero(err error) bool

IsWatchDogExpiredNotLessThanZero returns true if err is ErrWatchDogExpiredNotLessThanZero.

func IsWatchDogIsNil

func IsWatchDogIsNil(err error) bool

IsWatchDogIsNil returns true if err is ErrWatchDogIsNil.

func IsWatchDogNotStarted

func IsWatchDogNotStarted(err error) bool

IsWatchDogNotStarted returns true if err is ErrWatchDogNotStarted.

Types

type AverageRetry

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

AverageRetry is a retry strategy that retries for a fixed number of times with a fixed interval.

func NewAverageRetry

func NewAverageRetry(maxRetryCount uint, retryInterval time.Duration) *AverageRetry

NewAverageRetry creates a new AverageRetry.

func (*AverageRetry) NextRetryTime

func (a *AverageRetry) NextRetryTime() time.Duration

NextRetryTime returns retryInterval if maxRetryCount is greater than 0, otherwise returns 0.

type Client

type Client struct {
	*rc4.Cipher // customize cipher, default is rc4.NewCipher with cipherKey.
	// contains filtered or unexported fields
}

Client is the redislock client, wraps RedisClient.

func NewClient

func NewClient(redisClient RedisClient, options ...ClientOption) (*Client, error)

NewClient creates a new redislock client.

func NewDefaultClient

func NewDefaultClient(redisClient RedisClient) (*Client, error)

NewDefaultClient creates a new default redislock client.

func (*Client) TryLock

func (c *Client) TryLock(ctx context.Context, key string, expiration time.Duration) (*Mutex, error)

TryLock tries to acquire a lock with default parameter.

func (*Client) TryLockWithRetryAndWatchDog

func (c *Client) TryLockWithRetryAndWatchDog(ctx context.Context, key string, retryStrategy RetryStrategy, watchDog *WatchDog) (*Mutex, error)

TryLockWithRetryAndWatchDog tries to acquire a lock with retry strategy and watch dog.

func (*Client) TryLockWithRetryStrategy

func (c *Client) TryLockWithRetryStrategy(ctx context.Context, key string, expiration time.Duration, retryStrategy RetryStrategy) (*Mutex, error)

TryLockWithRetryStrategy tries to acquire a lock with retry strategy.

func (*Client) TryLockWithWatchDog

func (c *Client) TryLockWithWatchDog(ctx context.Context, key string, watchDog *WatchDog) (*Mutex, error)

TryLockWithWatchDog tries to acquire a lock with watch dog.

type ClientOption

type ClientOption func(client *Client)

func WithCipher

func WithCipher(cipher *rc4.Cipher) ClientOption

WithCipher sets the cipher of the client, if you set WithCipherKey and WithCipher at the same time, WithCipherKey will be ignored.

func WithCipherKey

func WithCipherKey(cipherKey string) ClientOption

WithCipherKey sets the cipherKey of the client.

type Mutex

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

Mutex is a distributed mutex implementation based on redis.

func (*Mutex) Refresh

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

Refresh resets the lock's expiration.

func (*Mutex) Unlock

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

Unlock releases the lock.

type NoRetry

type NoRetry struct{}

NoRetry is a retry strategy that never retries.

func NewNoRetry

func NewNoRetry() *NoRetry

NewNoRetry creates a new NoRetry.

func (*NoRetry) NextRetryTime

func (n *NoRetry) NextRetryTime() time.Duration

NextRetryTime returns 0, which means no retry.

type RedisClient

type RedisClient interface {
	redis.Scripter
	SetNX(ctx context.Context, key string, value interface{}, expiration time.Duration) *redis.BoolCmd
}

RedisClient is the interface used by redislock to interact with redis.

type RetryStrategy

type RetryStrategy interface {
	NextRetryTime() time.Duration
}

RetryStrategy is the interface used by redislock to retry.

type WatchDog

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

WatchDog is a watch dog for redis lock.

func NewDefaultWatchDog

func NewDefaultWatchDog() *WatchDog

NewDefaultWatchDog creates a new WatchDog with default expiration.

func NewWatchDog

func NewWatchDog(expiration time.Duration) *WatchDog

NewWatchDog creates a new WatchDog.

Jump to

Keyboard shortcuts

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