redissuo

package
v0.0.7 Latest Latest
Warning

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

Go to latest
Published: Sep 12, 2025 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package redissuo: Redis distributed lock implementation with Lua scripting to ensure atomic operations Provides consistent lock acquisition, release, and extension mechanisms with session management Features intelligent timeout handling, backoff logic, and comprehensive logging support Supports high-contention scenarios with precise timing coordination and race condition prevention

redissuo: Redis 分布式锁实现,使用 Lua 脚本确保原子操作 提供一致的锁获取、释放和延期机制,支持会话管理 具有智能超时处理、退避逻辑和完整的日志支持 支持高竞争场景,具备精确的时间协调和竞态条件预防

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Suo

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

Suo represents a Redis distributed lock instance with configurable TTL Contains Redis client connection, lock name ID, and expiration duration settings Provides core locking operations with atomic Lua-based commands Thread-safe when used across multiple goroutines

Suo 代表具有可配置 TTL 的 Redis 分布式锁实例 包含 Redis 客户端连接、锁名标识符和过期时长设置 提供基于 Lua 原子操作的核心锁定命令 在多个 goroutine 中使用时是线程安全的

func NewSuo

func NewSuo(rds redis.UniversalClient, key string, ttl time.Duration) *Suo

NewSuo creates a new Redis distributed lock instance with specified parameters Validates each input setting and returns configured lock instance Settings must be non-empty/non-blank otherwise the function can panic with must.Nice Returns prepared distributed lock suitable to use in production environments

NewSuo 使用指定参数创建新的 Redis 分布式锁实例 验证每个输入设置并返回配置好的锁实例 设置不能为空否则函数会通过 must.Nice 触发 panic 返回适用于生产环境的准备就绪分布式锁

func (*Suo) Acquire

func (o *Suo) Acquire(ctx context.Context) (*Xin, error)

Acquire attempts to acquire the distributed lock with auto-generated session UUID Creates random session ID to enable lock ownership verification Convenient method when doing standard lock acquisition without session management Returns lock session object on success, none if unavailable, issue on failure

Acquire 尝试使用自动生成的会话 UUID 获取分布式锁 创建随机会话标识符来启用锁所有权验证 在进行无需会话管理的标准锁获取时使用的便捷方法 成功时返回锁会话对象,不可用时返回空值,失败时返回问题

func (*Suo) AcquireAgainExtendLock added in v0.0.5

func (o *Suo) AcquireAgainExtendLock(ctx context.Context, xin *Xin) (*Xin, error)

AcquireAgainExtendLock extends the lock through re-acquiring with same session UUID Validates lock name consistent state and extends TTL using existing session ID Returns new lock session with updated expiration time when extension succeeds important when managing long-running operations that need extended lock duration

AcquireAgainExtendLock 通过使用相同会话 UUID 重新获取来延期锁 验证锁名一致性并使用现有会话标识符扩展 TTL 延期成功时返回具有更新过期时间的新锁会话 在管理需要延长锁持有时间的长期运行操作时至关重要

func (*Suo) AcquireLockWithSession

func (o *Suo) AcquireLockWithSession(ctx context.Context, sessionUUID string) (*Xin, error)

AcquireLockWithSession attempts to acquire lock using specified session UUID Calculates conservative expiration time through accounting during acquisition duration Returns lock session object on success, none if lock unavailable, issue on failure Provides precise timing coordination when managing high-performance distributed systems

AcquireLockWithSession 尝试使用指定会话 UUID 获取锁 在获取过程中通过考虑耗时计算保守的过期时间 成功时返回锁会话对象,锁不可用时返回空值,失败时返回问题 在管理高性能分布式系统时提供精确的时间协调

func (*Suo) Release

func (o *Suo) Release(ctx context.Context, xin *Xin) (bool, error)

Release attempts to release the distributed lock using session information Validates lock name consistent state and uses session UUID when checking ownership Returns true if released with success, false if owned from different session required to ensure safe cleanup and prevent unintended lock interference

Release 尝试使用会话信息释放分布式锁 验证锁名一致性并在检查所有权时使用会话 UUID 成功释放时返回 true,被不同会话拥有时返回 false 对确保安全清理和防止意外锁干扰至关重要

func (*Suo) WithLogger added in v0.0.7

func (o *Suo) WithLogger(logger logging.Logger) *Suo

WithLogger sets custom logger during lock operations Modifies the current Suo instance and returns it to support method chaining Enables injection of custom logging implementation with flexible strategies

WithLogger 为锁操作设置自定义日志记录器 修改当前 Suo 实例并返回以支持方法链式调用 允许注入自定义日志实现以实现灵活策略

type Xin

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

Xin represents an acquired distributed lock session with expiration tracking Contains lock identification, session UUID, and conservative expiration estimate Provides session management to ensure safe lock operations and extension Immutable once created, ensuring consistent lock state throughout usage

Xin 代表具有过期时间跟踪的已获取分布式锁会话 包含锁标识、会话 UUID 和保守的过期时间估算 提供会话管理来确保安全锁操作和延期 创建后不可变,确保使用过程中锁状态的一致性

func (*Xin) Expire added in v0.0.5

func (s *Xin) Expire() time.Time

Expire returns the conservative expiration time estimate of this lock Calculated through subtracting acquisition time from the TTL duration Provides safe timing reference when making lock extension decisions

Expire 返回此锁的保守过期时间估算 通过从 TTL 时长中减去获取时间来计算 在做出锁延期决策时提供安全的时间参考

func (*Xin) SessionUUID added in v0.0.5

func (s *Xin) SessionUUID() string

SessionUUID returns the unique session ID of this lock instance Used during lock ownership checks within release and extension operations required to prevent unintended release from different sessions

SessionUUID 返回此锁实例的唯一会话标识符 在释放和延期操作时用于锁所有权检查 对防止不同会话意外释放锁至关重要

Jump to

Keyboard shortcuts

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