Documentation
¶
Index ¶
- Variables
- type Option
- type RedisCmd
- type RedisInter
- type RedisLock
- func (l *RedisLock) FairLock(ctx context.Context, requestId string) error
- func (l *RedisLock) FairRenew(ctx context.Context, requestId string) error
- func (l *RedisLock) FairUnLock(ctx context.Context, requestId string) error
- func (l *RedisLock) Lock(ctx context.Context) error
- func (l *RedisLock) MultiLock(ctx context.Context, locks []RedisLockInter) error
- func (l *RedisLock) MultiRenew(ctx context.Context, locks []RedisLockInter) error
- func (l *RedisLock) MultiUnLock(ctx context.Context, locks []RedisLockInter) error
- func (l *RedisLock) RLock(ctx context.Context) error
- func (l *RedisLock) RRenew(ctx context.Context) error
- func (l *RedisLock) RUnLock(ctx context.Context) error
- func (l *RedisLock) Renew(ctx context.Context) error
- func (l *RedisLock) SpinFairLock(ctx context.Context, requestId string, timeout time.Duration) error
- func (l *RedisLock) SpinLock(ctx context.Context, timeout time.Duration) error
- func (l *RedisLock) SpinMultiLock(ctx context.Context, locks []RedisLockInter, timeout time.Duration) error
- func (l *RedisLock) SpinRLock(ctx context.Context, timeout time.Duration) error
- func (l *RedisLock) SpinWLock(ctx context.Context, timeout time.Duration) error
- func (l *RedisLock) UnLock(ctx context.Context) error
- func (l *RedisLock) WLock(ctx context.Context) error
- func (l *RedisLock) WRenew(ctx context.Context) error
- func (l *RedisLock) WUnLock(ctx context.Context) error
- type RedisLockInter
Constants ¶
This section is empty.
Variables ¶
var ( // ErrLockFailed 加锁失败 ErrLockFailed = errors.New("lock failed") // ErrUnLockFailed 解锁失败 ErrUnLockFailed = errors.New("unLock failed") // ErrSpinLockTimeout 自旋锁加锁超时 ErrSpinLockTimeout = errors.New("spin lock timeout") // ErrSpinLockDone 自旋锁加锁超时 ErrSpinLockDone = errors.New("spin lock context done") // ErrLockRenewFailed 锁续期失败 ErrLockRenewFailed = errors.New("lock renew failed") // ErrException 内部异常 ErrException = errors.New("go redis lock internal exception") )
Functions ¶
This section is empty.
Types ¶
type Option ¶
type Option func(lock *RedisLock)
func WithAutoRenew ¶
func WithAutoRenew() Option
WithAutoRenew enables automatic lock renewal WithAutoRenew 是否开启自动续期
func WithRequestTimeout ¶ added in v1.3.0
WithRequestTimeout sets the maximum wait time in the fair lock queue WithRequestTimeout 设置公平锁在队列中的最大等待时间
func WithTimeout ¶
WithTimeout sets the expiration time of the lock WithTimeout 设置锁的过期时间
type RedisInter ¶ added in v1.1.0
type RedisInter interface {
Eval(ctx context.Context, script string, keys []string, args ...interface{}) RedisCmd
}
RedisInter Redis 客户端接口
type RedisLock ¶
type RedisLock struct {
// contains filtered or unexported fields
}
func (*RedisLock) FairLock ¶ added in v1.3.0
FairLock 公平锁尝试加锁(使用指定的 requestId 获取公平锁) FairLock tries to acquire a fair lock using the given requestId. 公平锁确保请求按照顺序获取锁,避免饥饿现象 如果是队首且成功获取锁则返回 nil,否则返回 ErrLockFailed
func (*RedisLock) FairRenew ¶ added in v1.3.0
FairRenew manually extends the expiration of a fair lock. FairRenew 手动延长指定 requestId 的公平锁有效期。
func (*RedisLock) FairUnLock ¶ added in v1.3.0
FairUnLock releases the fair lock held by the given requestId. FairUnLock 根据 requestId 释放公平锁。
func (*RedisLock) Lock ¶
Lock tries to acquire a standard lock. This implementation supports "reentrant locks". If the lock is currently held by the same key+token, reentry is allowed and the count is increased. Unlock() needs to be called a corresponding number of times to release the lock.
Lock 尝试获取普通锁。 该实现支持“可重入锁”,如果当前已由相同 key+token 持有,允许重入并增加计数。需调用相应次数 Unlock() 释放
func (*RedisLock) MultiLock ¶ added in v1.4.0
func (l *RedisLock) MultiLock(ctx context.Context, locks []RedisLockInter) error
func (*RedisLock) MultiRenew ¶ added in v1.4.0
func (l *RedisLock) MultiRenew(ctx context.Context, locks []RedisLockInter) error
func (*RedisLock) MultiUnLock ¶ added in v1.4.0
func (l *RedisLock) MultiUnLock(ctx context.Context, locks []RedisLockInter) error
func (*RedisLock) SpinFairLock ¶ added in v1.3.0
func (l *RedisLock) SpinFairLock(ctx context.Context, requestId string, timeout time.Duration) error
SpinFairLock keeps trying to acquire a fair lock until timeout. SpinFairLock 在指定超时时间内不断尝试获取公平锁。
func (*RedisLock) SpinLock ¶
SpinLock keeps trying to acquire the lock until timeout. SpinLock 在指定超时时间内不断尝试加锁。
func (*RedisLock) SpinMultiLock ¶ added in v1.4.0
type RedisLockInter ¶
type RedisLockInter interface {
// Lock 加锁
Lock(ctx context.Context) error
// SpinLock 自旋锁。
SpinLock(ctx context.Context, timeout time.Duration) error
// UnLock 解锁
UnLock(ctx context.Context) error
// Renew 锁续期
Renew(ctx context.Context) error
// FairLock 公平锁加锁
FairLock(ctx context.Context, requestId string) error
// SpinFairLock 自旋公平锁
SpinFairLock(ctx context.Context, requestId string, timeout time.Duration) error
// FairUnLock 公平锁解锁
FairUnLock(ctx context.Context, requestId string) error
// FairRenew 公平锁续期
FairRenew(ctx context.Context, requestId string) error
// RLock 读锁加锁
RLock(ctx context.Context) error
// RUnLock 读锁解锁
RUnLock(ctx context.Context) error
// SpinRLock 自旋读锁
SpinRLock(ctx context.Context, timeout time.Duration) error
// RRenew 读锁续期
RRenew(ctx context.Context) error
// WLock 写锁加锁
WLock(ctx context.Context) error
// WUnLock 写锁解锁
WUnLock(ctx context.Context) error
// SpinWLock 自旋写锁
SpinWLock(ctx context.Context, timeout time.Duration) error
// WRenew 写锁续期
WRenew(ctx context.Context) error
}
RedisLockInter defines the interface for distributed Redis locks
func New ¶
func New(redisClient RedisInter, lockKey string, options ...Option) RedisLockInter
New creates a RedisLock instance
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
adapter
|
|
|
gf
module
|
|
|
gf/V1
module
|
|
|
gf/V2
module
|
|
|
go-redis
module
|
|
|
go-redis/V8
module
|
|
|
go-redis/V9
module
|
|
|
go-zero
module
|
|
|
go-zero/V1
module
|
|
|
examples
|
|
|
Package mocks is a generated GoMock package.
|
Package mocks is a generated GoMock package. |