ggglock

package module
v0.0.5 Latest Latest
Warning

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

Go to latest
Published: Jul 20, 2023 License: Apache-2.0 Imports: 8 Imported by: 0

README

分布式锁

介绍

Golang Redis实现的分布式锁

单元测试
mkdir -p mocks
mockgen -destination ./mocks/mock_cmdable.go -package mocks github.com/redis/go-redis/v9 Cmdable

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrFailedToLock = errors.New("加锁失败")
	ErrNotLock      = errors.New("没有持有锁")
)

Functions

This section is empty.

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,
	timeout time.Duration, retry *Retry) (*Lock, error)

Lock 获取锁(超时重试) expiration锁过期时间 timeout单次设置锁超时时间 retry重试设置结构体

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 自动续约 interval 间隔多长时间续约 timeOut 超时时间

func (*Lock) Refresh

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

Refresh 续约

func (*Lock) UnLock

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

UnLock 解锁

type Retry

type Retry struct {
	//重试间隔
	Interval time.Duration
	//最大重试次数,0表示不重试
	Max int
	// contains filtered or unexported fields
}

func (*Retry) Next

func (r *Retry) 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