Help us shape the future of Go! Take the 2026 Go Developer Survey

lock

package
v0.4.4 Latest Latest
Warning

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

Go to latest
Published: Sep 27, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DistLock

type DistLock interface {
	TryLock(ctx context.Context, key string) (bool, error)
	TryLockWithTTL(ctx context.Context, key string, ttl time.Duration) (bool, error)
	Lock(ctx context.Context, key string) error
	Unlock(ctx context.Context, key string) error
}

type LocalLock

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

func NewLocalLock

func NewLocalLock() *LocalLock

func (*LocalLock) Lock

func (l *LocalLock) Lock(ctx context.Context, key string) error

func (*LocalLock) TryLock

func (l *LocalLock) TryLock(ctx context.Context, key string) (bool, error)

func (*LocalLock) TryLockWithTTL

func (l *LocalLock) TryLockWithTTL(ctx context.Context, key string, ttl time.Duration) (bool, error)

func (*LocalLock) Unlock

func (l *LocalLock) Unlock(ctx context.Context, key string) error

type Option

type Option func(*redis.Options)

Option functional option for NewRedisLock.

func WithPoolOptions

func WithPoolOptions(pool RedisPoolOptions) Option

WithPoolOptions 设置连接池参数。

type RedisLock

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

func NewRedisLock

func NewRedisLock(addr, password string, db int, opts ...Option) *RedisLock

func (*RedisLock) Client

func (l *RedisLock) Client() *redis.Client

func (*RedisLock) Close

func (l *RedisLock) Close() error

func (*RedisLock) ExtendLock

func (l *RedisLock) ExtendLock(ctx context.Context, key string, ttl time.Duration) error

ExtendLock 续期:从 lockValues 取出持有值,用 Lua 脚本安全续期。 若 lockValues 中无记录(非本进程持有),返回错误而非盲目续期。

func (*RedisLock) ExtendLockWithValue

func (l *RedisLock) ExtendLockWithValue(ctx context.Context, key, value string, ttl time.Duration) (bool, error)

ExtendLockWithValue 续期:仅当 value 匹配(自己持有)才延长 TTL,返回是否成功续期。 供 watchdog 在长任务期间周期性续期,避免锁过期被别人抢走。

func (*RedisLock) Lock

func (l *RedisLock) Lock(ctx context.Context, key string) error

func (*RedisLock) LockWithTTL

func (l *RedisLock) LockWithTTL(ctx context.Context, key string, ttl time.Duration) (bool, string, error)

func (*RedisLock) Ping

func (l *RedisLock) Ping(ctx context.Context) error

func (*RedisLock) TryLock

func (l *RedisLock) TryLock(ctx context.Context, key string) (bool, error)

func (*RedisLock) TryLockWithTTL

func (l *RedisLock) TryLockWithTTL(ctx context.Context, key string, ttl time.Duration) (bool, error)

func (*RedisLock) Unlock

func (l *RedisLock) Unlock(ctx context.Context, key string) error

func (*RedisLock) UnlockWithValue

func (l *RedisLock) UnlockWithValue(ctx context.Context, key, value string) error

type RedisPoolOptions

type RedisPoolOptions struct {
	PoolSize        int // 连接池大小,默认 10*GOMAXPROCS
	MinIdleConns    int // 最小空闲连接数,默认 0(冷启动无预热)
	DialTimeoutSec  int // 建连超时秒数,默认使用 OS TCP 超时
	ReadTimeoutSec  int // 读超时秒数
	WriteTimeoutSec int // 写超时秒数
}

RedisPoolOptions 连接池可选配置,零值使用 go-redis 默认。

type Semaphore

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

func NewSemaphore

func NewSemaphore(client *redis.Client, key string, max int, slotTTL time.Duration) *Semaphore

NewSemaphore 创建一个基于 redis ZSET 的分布式信号量。 slotTTL 为单个槽的存活时长:持有者需周期性 Renew,进程崩溃后槽在 slotTTL 后自动释放。

func (*Semaphore) Acquire

func (s *Semaphore) Acquire(ctx context.Context, identifier string) (bool, error)

func (*Semaphore) Cleanup

func (s *Semaphore) Cleanup(ctx context.Context, olderThan time.Duration) error

func (*Semaphore) Release

func (s *Semaphore) Release(ctx context.Context, identifier string) error

func (*Semaphore) Renew

func (s *Semaphore) Renew(ctx context.Context, identifier string) error

Renew 续期持有的槽(把 score 推到 now+slotTTL),仅当成员已存在才续期。

Jump to

Keyboard shortcuts

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