redis_lock

package module
v0.0.0-...-0f6e672 Latest Latest
Warning

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

Go to latest
Published: May 9, 2024 License: Apache-2.0 Imports: 7 Imported by: 0

README

redis-lock

redis分布式锁

SETNX 命令

  • 命令 SETNX 只有在 key 不存在时,才会设置 key 的值。

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrFailedToPreemptLock = errors.New("rlock: 抢锁失败")
	ErrLockNotHold         = errors.New("rlock: 未持有锁")
)

Functions

func NewOnceCloseChan

func NewOnceCloseChan() *onceCloseChan

Types

type Client

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

func NewClient

func NewClient(client redis.Cmdable) *Client

func (*Client) Lock

func (c *Client) Lock(ctx context.Context, key string,
	expiration time.Duration, retry RetryStrategy, timeout time.Duration) (*Lock, error)

func (*Client) TryLock

func (c *Client) TryLock(ctx context.Context, key string,
	expiration time.Duration) (*Lock, error)

type FixIntervalRetry

type FixIntervalRetry struct {
	// 重试间隔
	Interval time.Duration
	// 最大次数
	Max int
	// contains filtered or unexported fields
}

func (*FixIntervalRetry) Next

func (f *FixIntervalRetry) Next() (time.Duration, bool)

type Lock

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

func (*Lock) AutoRefresh

func (l *Lock) AutoRefresh(interval time.Duration, timeout time.Duration) error

AutoRefresh 自动续约(谨慎使用, 推荐使用手动)

func (*Lock) Refresh

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

Refresh 手动刷新锁的过期时间

func (*Lock) UnLock

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

type ReentrantLock

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

func (*ReentrantLock) Lock

func (r *ReentrantLock) Lock(ctx context.Context, key string,
	expiration time.Duration, timeout time.Duration) (*ReentrantLock, error)

type RetryStrategy

type RetryStrategy interface {
	// Next 返回下一次重试的间隔,如果不需要继续重试,那么第二参数发挥 false
	// 接口没有上下文, 如需根据错误类型排序, 暂不支持
	Next() (time.Duration, bool)
}

Directories

Path Synopsis
Package mocks is a generated GoMock package.
Package mocks is a generated GoMock package.

Jump to

Keyboard shortcuts

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