Documentation ¶
Index ¶
- Constants
- Variables
- func IsMutexLockFailed(err error) bool
- func IsMutexNotHeld(err error) bool
- func IsMutexNotInitialized(err error) bool
- func IsWatchDogExpiredNotLessThanZero(err error) bool
- func IsWatchDogIsNil(err error) bool
- func IsWatchDogNotStarted(err error) bool
- type AverageRetry
- type Client
- func (c *Client) TryLock(ctx context.Context, key string, expiration time.Duration) (*Mutex, error)
- func (c *Client) TryLockWithRetryAndWatchDog(ctx context.Context, key string, retryStrategy RetryStrategy, ...) (*Mutex, error)
- func (c *Client) TryLockWithRetryStrategy(ctx context.Context, key string, expiration time.Duration, ...) (*Mutex, error)
- func (c *Client) TryLockWithWatchDog(ctx context.Context, key string, watchDog *WatchDog) (*Mutex, error)
- type ClientOption
- type Mutex
- type NoRetry
- type RedisClient
- type RetryStrategy
- type WatchDog
Constants ¶
const DefaultExpiration = 30 * time.Second
DefaultExpiration is the default expiration of lock.
Variables ¶
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 ¶
IsMutexLockFailed returns true if err is ErrMutexLockFailed.
func IsMutexNotHeld ¶
IsMutexNotHeld returns true if err is ErrMutexNotHeld.
func IsMutexNotInitialized ¶
IsMutexNotInitialized returns true if err is ErrMutexNotInitialized.
func IsWatchDogExpiredNotLessThanZero ¶
IsWatchDogExpiredNotLessThanZero returns true if err is ErrWatchDogExpiredNotLessThanZero.
func IsWatchDogIsNil ¶
IsWatchDogIsNil returns true if err is ErrWatchDogIsNil.
func IsWatchDogNotStarted ¶
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) 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.
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.
type NoRetry ¶
type NoRetry struct{}
NoRetry is a retry strategy that never retries.
func (*NoRetry) NextRetryTime ¶
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 ¶
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 ¶
NewWatchDog creates a new WatchDog.