Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client wraps a redis client.
func New ¶
func New(client RedisClient) *Client
New creates a new Client instance with a custom namespace.
type ErrLockNotHeld ¶
type ErrLockNotHeld struct {
// contains filtered or unexported fields
}
ErrLockNotHeld is returned when trying to release an inactive lock.
func (*ErrLockNotHeld) Error ¶
func (e *ErrLockNotHeld) Error() string
type ErrNotObtained ¶
type ErrNotObtained struct {
// contains filtered or unexported fields
}
ErrNotObtained is returned when a lock cannot be obtained.
func (*ErrNotObtained) Error ¶
func (e *ErrNotObtained) Error() string
type Lock ¶
type Lock struct {
// contains filtered or unexported fields
}
Lock represents an obtained, distributed lock.
func Obtain ¶
func Obtain(ctx context.Context, client RedisClient, key string, ttl time.Duration, opt *Options) (*Lock, error)
Obtain is a short-cut for New(...).Obtain(...).
func (*Lock) Refresh ¶
Refresh extends the lock with a new TTL. May return ErrNotObtained if refresh is unsuccessful.
type Options ¶
type Options struct { // RetryStrategy allows to customise the lock retry strategy. // Default: do not retry RetryStrategy RetryStrategy // Metadata string is appended to the lock token. Metadata string }
Options describe the options for the lock
type RedisClient ¶
type RedisClient interface {
redis.Scripter
SetNX(ctx context.Context, key string, value interface{}, expiration time.Duration) *redis.BoolCmd
}
RedisClient is a minimal client interface.
type RetryStrategy ¶
type RetryStrategy interface { // NextBackoff returns the next backoff duration. NextBackoff() time.Duration }
RetryStrategy allows to customise the lock retry strategy.
func ExponentialBackoff ¶
func ExponentialBackoff(min, max time.Duration) RetryStrategy
ExponentialBackoff strategy is an optimization strategy with a retry time of 2**n milliseconds (n means number of times). You can set a minimum and maximum value, the recommended minimum value is not less than 16ms.
func LimitRetry ¶
func LimitRetry(s RetryStrategy, max int) RetryStrategy
LimitRetry limits the number of retries to max attempts.
func LinearBackoff ¶
func LinearBackoff(backoff time.Duration) RetryStrategy
LinearBackoff allows retries regularly with customized intervals