lock

package
v0.0.0-...-1225d9a Latest Latest
Warning

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

Go to latest
Published: Dec 13, 2023 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
const (
	AppName = "lock"
)
View Source
const (
	PROVIDER_REDIS = redis.AppName
)

Variables

View Source
var (
	// ErrNotObtained is returned when a lock cannot be obtained.
	ErrNotObtained = errors.New("lock: not obtained")

	// ErrLockNotHeld is returned when trying to release an inactive lock.
	ErrLockNotHeld = errors.New("lock: lock not held")
)

Functions

This section is empty.

Types

type Lock

type Lock interface {
	// 锁配置
	WithOpt(opt *Options) Lock
	// 获取锁
	Lock(ctx context.Context) error
	// 释放锁
	UnLock(ctx context.Context) error
	// 刷新锁
	Refresh(ctx context.Context, ttl time.Duration) error
}

type LockFactory

type LockFactory interface {
	New(key string, ttl time.Duration) Lock
}

func L

func L() LockFactory

type Options

type Options struct {
	// RetryStrategy allows to customise the lock retry strategy.
	// Default: do not retry
	RetryStrategy RetryStrategy

	// Metadata string.
	Metadata 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 DefaultOptions

func DefaultOptions() *Options

type PROVIDER

type PROVIDER string

type RedisLock

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

Lock represents an obtained, distributed lock.

func (*RedisLock) Key

func (l *RedisLock) Key() string

Key returns the redis key used by the lock.

func (*RedisLock) Lock

func (l *RedisLock) Lock(ctx context.Context) error

获取锁

func (*RedisLock) Metadata

func (l *RedisLock) Metadata() string

Metadata returns the metadata of the lock.

func (*RedisLock) Refresh

func (l *RedisLock) Refresh(ctx context.Context, ttl time.Duration) error

刷新锁

func (*RedisLock) TTL

func (l *RedisLock) TTL(ctx context.Context) (time.Duration, error)

TTL returns the remaining time-to-live. Returns 0 if the lock has expired.

func (*RedisLock) Token

func (l *RedisLock) Token() string

Token returns the token value set by the lock.

func (*RedisLock) UnLock

func (l *RedisLock) UnLock(ctx context.Context) error

释放锁

func (*RedisLock) WithOpt

func (l *RedisLock) WithOpt(opt *Options) Lock

锁配置

type RedisLockProvider

type RedisLockProvider struct {
}

func NewRedisLockProvider

func NewRedisLockProvider() *RedisLockProvider

func (*RedisLockProvider) New

func (r *RedisLockProvider) New(key string, ttl time.Duration) Lock

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

func NoRetry

func NoRetry() RetryStrategy

NoRetry acquire the lock only once.

Jump to

Keyboard shortcuts

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