Documentation ¶
Overview ¶
Package dblock provides a distributed lock abstraction.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrNotObtained is returned when a lock cannot be obtained. ErrNotObtained = errors.New("dblock: not obtained") // ErrLockNotHeld is returned when trying to release an inactive lock. ErrLockNotHeld = errors.New("dblock: lock not held") // ErrNoProviders is returned when trying to obtain a lock. ErrNoProviders = errors.New("dblock: no providers registered") )
Functions ¶
Types ¶
type Client ¶
type Client interface { // Obtain tries to obtain a new lock using a key with the given TTL. // May return ErrNotObtained if not successful. Obtain(ctx context.Context, key string, ttl time.Duration, optionsFns ...OptionsFn) (Lock, error) }
Client abstracts the distributed lock.
var DefaultClient Client
DefaultClient setup the default client.
type ClientView ¶
ClientView abstracts the distributed lock viewing.
type Lock ¶
type Lock interface { // Token returns the token value set by the lock. Token() string // Metadata returns the metadata of the lock. Metadata() string // TTL returns the remaining time-to-live. Returns 0 if the lock has expired. TTL(ctx context.Context) (time.Duration, error) // Refresh extends the lock with a new TTL. // May return ErrNotObtained if refresh is unsuccessful. Refresh(ctx context.Context, ttl time.Duration) error // Release manually releases the lock. // May return ErrLockNotHeld. Release(ctx context.Context) error }
Lock represents an obtained, distributed lock.
type LockView ¶
type LockView interface { // GetToken returns the token value set by the lock. GetToken() string // GetMetadata returns the metadata of the lock. GetMetadata() string // GetUntil returns expired time in RFC3339. GetUntil() string }
LockView is the view of a lock for viewing.
type Options ¶
type Options struct { // RetryStrategy allows to customise the lock retry strategy. // Default: do not retry RetryStrategy RetryStrategy // Meta string. Meta string // Token is a unique value that is used to identify the lock. By default, a random tokens are generated. Use this // option to provide a custom token instead. Token string }
Options describe the options for the lock.
func (*Options) GetRetryStrategy ¶
func (o *Options) GetRetryStrategy() RetryStrategy
GetRetryStrategy returns the retry strategy.
type OptionsFn ¶
type OptionsFn func(*Options)
OptionsFn allows to customise the lock retry strategy.
func WithRetryStrategy ¶
func WithRetryStrategy(strategy RetryStrategy) OptionsFn
WithRetryStrategy set the lock retry strategy.
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