Documentation
¶
Overview ¶
Package lock 提供同步任务的并发控制原语:
- 跨实例互斥锁:oekit redisx/distlock(SetNX + Lua 原子解锁 + 自动续期), 本包只负责固定 key 前缀与客户端构造;
- Semaphore:基于 redis ZSET 的分布式信号量,限制全局并发上限;
- LocalLock:单实例(未配 redis)时的进程内互斥。
Index ¶
- Constants
- func NewLocker(client redis.UniversalClient, opts ...distlock.RedisLockerOption) *distlock.RedisLocker
- func NewRedisClient(addr, password string, db int, opts ...Option) *redis.Client
- type LocalLock
- func (l *LocalLock) Lock(ctx context.Context, key string) error
- func (l *LocalLock) TryLock(ctx context.Context, key string) (bool, error)
- func (l *LocalLock) TryLockWithTTL(ctx context.Context, key string, ttl time.Duration) (bool, error)
- func (l *LocalLock) Unlock(ctx context.Context, key string) error
- type Option
- type RedisPoolOptions
- type Semaphore
- func (s *Semaphore) Acquire(ctx context.Context, identifier string) (bool, error)
- func (s *Semaphore) Cleanup(ctx context.Context, olderThan time.Duration) error
- func (s *Semaphore) Release(ctx context.Context, identifier string) error
- func (s *Semaphore) Renew(ctx context.Context, identifier string) error
Constants ¶
View Source
const ( // LockKeyPrefix 是跨实例互斥锁的 key 前缀。distlock 以 "<prefix>:<key>" 落 key, // 与迁移前自写实现的 "git-sync:lock:<key>" 布局一致:滚动升级期间新老实例 // 看到的是同一把锁,不会出现两边各持一把而同时执行的窗口。 LockKeyPrefix = "git-sync:lock" )
Variables ¶
This section is empty.
Functions ¶
func NewLocker ¶ added in v0.10.0
func NewLocker(client redis.UniversalClient, opts ...distlock.RedisLockerOption) *distlock.RedisLocker
NewLocker 构造跨实例互斥锁,key 前缀固定为 LockKeyPrefix;续期等策略由调用方按场景追加。
Types ¶
type LocalLock ¶
type LocalLock struct {
// contains filtered or unexported fields
}
func NewLocalLock ¶
func NewLocalLock() *LocalLock
func (*LocalLock) TryLockWithTTL ¶
type RedisPoolOptions ¶
type RedisPoolOptions struct {
PoolSize int // 连接池大小,默认 10*GOMAXPROCS
MinIdleConns int // 最小空闲连接数,默认 0(冷启动无预热)
DialTimeoutSec int // 建连超时秒数,默认使用 OS TCP 超时
ReadTimeoutSec int // 读超时秒数
WriteTimeoutSec int // 写超时秒数
}
RedisPoolOptions 连接池可选配置,零值使用 go-redis 默认。
Click to show internal directories.
Click to hide internal directories.