Categorygithub.com/XdpCs/redis-lock
modulepackage
0.0.0-20230829124757-e8d168ec56c0
Repository: https://github.com/xdpcs/redis-lock.git
Documentation: pkg.go.dev

# 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.

# Functions

IsMutexLockFailed returns true if err is ErrMutexLockFailed.
IsMutexNotHeld returns true if err is ErrMutexNotHeld.
IsMutexNotInitialized returns true if err is ErrMutexNotInitialized.
IsWatchDogExpiredNotLessThanZero returns true if err is ErrWatchDogExpiredNotLessThanZero.
IsWatchDogIsNil returns true if err is ErrWatchDogIsNil.
IsWatchDogNotStarted returns true if err is ErrWatchDogNotStarted.
NewAverageRetry creates a new AverageRetry.
NewClient creates a new redislock client.
NewDefaultClient creates a new default redislock client.
NewDefaultWatchDog creates a new WatchDog with default expiration.
NewNoRetry creates a new NoRetry.
NewWatchDog creates a new WatchDog.
WithCipher sets the cipher of the client, if you set WithCipherKey and WithCipher at the same time, WithCipherKey will be ignored.
WithCipherKey sets the cipherKey of the client.

# Constants

DefaultExpiration is the default expiration of lock.

# Variables

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

# Structs

AverageRetry is a retry strategy that retries for a fixed number of times with a fixed interval.
Client is the redislock client, wraps RedisClient.
Mutex is a distributed mutex implementation based on redis.
NoRetry is a retry strategy that never retries.
WatchDog is a watch dog for redis lock.

# Interfaces

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

# Type aliases

No description provided by the author