lease

package
v0.6.1 Latest Latest
Warning

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

Go to latest
Published: Apr 29, 2026 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Lease

type Lease struct {
	Key        LeaseKey
	Token      LeaseToken
	TTL        time.Duration
	Owner      *LeaseOwner
	AcquiredAt time.Time
}

Lease 表示一个已获取的锁租约。

type LeaseAttempt

type LeaseAttempt struct {
	Lease    Lease
	Acquired bool
}

LeaseAttempt 描述一次 acquire 是否成功。

type LeaseKey

type LeaseKey string

LeaseKey 是不可变的锁键值对象。

func MustLeaseKey

func MustLeaseKey(key string) LeaseKey

MustLeaseKey 创建租约锁键,输入非法时 panic。

func NewLeaseKey

func NewLeaseKey(key string) (LeaseKey, error)

NewLeaseKey 创建经过校验的租约锁键。

func (LeaseKey) String

func (k LeaseKey) String() string

String 返回原始锁键字符串。

type LeaseOwner

type LeaseOwner struct {
	ID    string
	Label string
}

LeaseOwner 是锁观测与调试使用的可选拥有者元数据。

func (*LeaseOwner) Identity

func (o *LeaseOwner) Identity() string

Identity 返回最适合作为拥有者标识的字符串。

type LeaseToken

type LeaseToken string

LeaseToken 是不可变的锁 token 值对象。

func MustLeaseToken

func MustLeaseToken(token string) LeaseToken

MustLeaseToken 创建租约 token,输入非法时 panic。

func NewLeaseToken

func NewLeaseToken(token string) (LeaseToken, error)

NewLeaseToken 创建经过校验的租约 token。

func (LeaseToken) String

func (t LeaseToken) String() string

String 返回原始 token 字符串。

type Option

type Option func(*Service)

func WithObserver

func WithObserver(observer observability.LeaseObserver) Option

WithObserver 配置租约锁观测器。

func WithTokenSource

func WithTokenSource(source TokenSource) Option

WithTokenSource 配置自定义 token 生成器。

type Service

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

func NewService

func NewService(client goredis.UniversalClient, opts ...Option) *Service

NewService 基于 Redis 客户端创建租约锁服务。

func (*Service) Acquire

func (s *Service) Acquire(ctx context.Context, key LeaseKey, ttl time.Duration, owner *LeaseOwner) (LeaseAttempt, error)

Acquire 使用 SET NX 语义尝试获取租约锁。

func (*Service) CheckOwnership

func (s *Service) CheckOwnership(ctx context.Context, lease Lease) (bool, error)

CheckOwnership 检查给定租约是否仍然持有该锁键。

func (*Service) Release

func (s *Service) Release(ctx context.Context, lease Lease) error

Release 仅在 token 仍匹配时释放租约锁。

func (*Service) Renew

func (s *Service) Renew(ctx context.Context, lease Lease, ttl time.Duration) (Lease, bool, error)

Renew 使用 compare-and-expire 语义续约当前租约。

type TokenSource

type TokenSource interface {
	NewToken(ctx context.Context) (LeaseToken, error)
}

Jump to

Keyboard shortcuts

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