lock

package
v1.2.2 Latest Latest
Warning

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

Go to latest
Published: Oct 6, 2026 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Overview

Package lock 提供基于 Redis 的持有者令牌租约锁。

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrClientRequired 表示没有提供 Redis 客户端。
	ErrClientRequired = errx.New("redis lock client must not be nil")
	// ErrNamespaceRequired 表示锁命名空间为空。
	ErrNamespaceRequired = errx.New("redis lock namespace must not be empty")
	// ErrKeyRequired 表示锁业务键为空。
	ErrKeyRequired = errx.New("redis lock key must not be empty")
	// ErrInvalidTTL 表示租约时长小于 Redis 支持的毫秒精度。
	ErrInvalidTTL = errx.New("redis lock ttl must be at least one millisecond")
	// ErrNotAcquired 表示锁已被其他持有者占用。
	ErrNotAcquired = errx.New("redis lock is already held")
	// ErrLeaseLost 表示当前持有者已失去租约。
	ErrLeaseLost = errx.New("redis lock lease is no longer owned")
)

Functions

This section is empty.

Types

type Lease

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

Lease 表示一次成功获取的 Redis 租约。

func (*Lease) Renew

func (l *Lease) Renew(ctx context.Context, ttl time.Duration) error

Renew 仅在当前持有者仍拥有锁时原子延长租约。

func (*Lease) Unlock

func (l *Lease) Unlock(ctx context.Context) error

Unlock 仅在当前持有者仍拥有锁时原子释放租约。

type Manager

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

Manager 使用统一命名空间创建 Redis 租约锁。

func New

func New(client rdx.Client, namespace string) (*Manager, error)

New 创建 Redis 租约锁管理器。

func (*Manager) Acquire

func (m *Manager) Acquire(ctx context.Context, key string, ttl time.Duration) (*Lease, error)

Acquire 尝试获取指定业务键的租约;锁被占用时返回 ErrNotAcquired。

Jump to

Keyboard shortcuts

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