lock

package
v2.0.25 Latest Latest
Warning

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

Go to latest
Published: Aug 31, 2024 License: MIT Imports: 14 Imported by: 2

README

锁服务

非分布式锁

分布式锁

redis
docker run --name redis -p 6379:6379 -itd redis
etcd

Documentation

Index

Constants

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

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

func Get added in v2.0.8

func Get() *config

Types

type GoCacheLock added in v2.0.15

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

func (*GoCacheLock) Lock added in v2.0.15

func (m *GoCacheLock) Lock(ctx context.Context) error

获取锁

func (*GoCacheLock) Refresh added in v2.0.15

func (m *GoCacheLock) Refresh(ctx context.Context, ttl time.Duration) error

刷新锁

func (*GoCacheLock) UnLock added in v2.0.15

func (m *GoCacheLock) UnLock(context.Context) error

释放锁

func (*GoCacheLock) WithOpt added in v2.0.15

func (m *GoCacheLock) WithOpt(opt *Options) Lock

锁配置

type GoCacheLockProvider added in v2.0.15

type GoCacheLockProvider struct {
}

func NewGoCacheLockProvider added in v2.0.15

func NewGoCacheLockProvider() *GoCacheLockProvider

func (*GoCacheLockProvider) New added in v2.0.15

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

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) TTLValueString added in v2.0.15

func (l *RedisLock) TTLValueString() string

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