Documentation
¶
Overview ¶
Package dlock 提供 Genesis 的分布式锁组件。
`dlock` 的定位是为常见的“跨实例互斥”场景提供一层克制、统一的锁接口, 当前支持 Redis 和 Etcd 两种后端。调用方通过 `Locker` 使用阻塞式 `Lock`、 非阻塞式 `TryLock`、显式 `Unlock` 和 `Close`,不需要直接处理 Redis 的 token 校验或 Etcd 的 session / mutex 细节。
这个组件的核心目标不是抽象出一个“万能锁框架”,而是把最常见、最稳定的 分布式锁能力收敛成少量接口,并把几个容易出错的边界收紧:
- Redis 使用 token + Lua 脚本,避免误删别人的锁。
- Redis 和 Etcd 都会在锁持有期间自动续期。
- `Close()` 会停止续期,并尽力释放当前 `Locker` 已持有的锁。
- 同一个 `Locker` 不允许本地重入同一个 key。
`dlock` 不负责可重入锁、读写锁、公平锁、锁诊断平台或死锁检测。它更适合 任务竞选、资源互斥、短事务串行化这类“需要一把简单分布式锁”的场景。
需要注意的是,Redis 与 Etcd 并不是完全等价的协议实现。尤其在 TTL 语义上, Etcd 依赖 lease,精度为秒级,因此 `DefaultTTL` 和 `WithTTL(...)` 都要求 至少 1 秒且必须是整秒;Redis 为毫秒精度,最小 TTL 为 1 毫秒。
Locker does not issue fencing tokens. Callers protecting irreversible writes must use a downstream compare-and-set/version mechanism in addition to monitoring Locker.Lost; mutual exclusion alone cannot fence a paused former owner after its lease expires.
Example ¶
package main
import (
"context"
"time"
"github.com/ceyewan/genesis/connector"
"github.com/ceyewan/genesis/dlock"
)
func main() {
ctx := context.Background()
conn, err := connector.NewRedis(&connector.RedisConfig{Addr: "localhost:6379"})
if err != nil {
return
}
defer conn.Close()
if err := conn.Connect(ctx); err != nil {
return
}
locker, err := dlock.New(&dlock.Config{Driver: dlock.DriverRedis}, dlock.WithRedisConnector(conn))
if err != nil {
return
}
defer locker.Close()
if err := locker.Lock(ctx, "orders:42", dlock.WithTTL(10*time.Second)); err != nil {
return
}
lost := locker.Lost("orders:42")
_ = lost
_ = locker.Unlock(ctx, "orders:42")
}
Output:
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrInvalidConfig 表示 dlock 配置无效。 ErrInvalidConfig = xerrors.New("dlock: invalid config") // ErrConfigNil 配置为空。它同时匹配 ErrInvalidConfig。 ErrConfigNil error = &classifiedSentinel{ message: "dlock: config is nil", class: ErrInvalidConfig, } // ErrConnectorNil 表示所需连接器缺失或尚未就绪。 // 它同时匹配 connector.ErrClientNil。 ErrConnectorNil error = &classifiedSentinel{ message: "dlock: connector is nil", class: connector.ErrClientNil, } // ErrInvalidKey 表示锁 key 无效。 ErrInvalidKey = xerrors.New("dlock: invalid key") // ErrLockNotHeld 锁未持有 ErrLockNotHeld = xerrors.New("dlock: lock not held") // ErrLockAlreadyHeld 锁已在本地持有 ErrLockAlreadyHeld = xerrors.New("dlock: lock already held locally") // ErrOwnershipLost 锁所有权丢失 ErrOwnershipLost = xerrors.New("dlock: ownership lost") // ErrInvalidTTL TTL 配置非法。它同时匹配 ErrInvalidConfig。 ErrInvalidTTL error = &classifiedSentinel{ message: "dlock: invalid ttl", class: ErrInvalidConfig, } // ErrClosed Locker 已关闭。 ErrClosed = xerrors.New("dlock: locker closed") )
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct {
// Driver 选择使用的后端 (redis | etcd)
Driver DriverType `json:"driver" yaml:"driver" mapstructure:"driver"`
// Prefix 锁 Key 的全局前缀,例如 "dlock:"
Prefix string `json:"prefix" yaml:"prefix" mapstructure:"prefix"`
// DefaultTTL 默认锁超时时间
// Redis 会启动 Watchdog 自动续期;Etcd 使用 Session KeepAlive 自动续期。
DefaultTTL time.Duration `json:"default_ttl" yaml:"default_ttl" mapstructure:"default_ttl"`
// RetryInterval 加锁重试间隔 (仅 Lock 模式有效)
RetryInterval time.Duration `json:"retry_interval" yaml:"retry_interval" mapstructure:"retry_interval"`
}
Config 组件静态配置
type DriverType ¶
type DriverType string
DriverType 定义支持的后端类型
const ( DriverRedis DriverType = "redis" DriverEtcd DriverType = "etcd" )
type LockOption ¶
type LockOption func(*lockOptions)
LockOption Lock 操作的选项函数
func WithTTL ¶
func WithTTL(d time.Duration) LockOption
WithTTL 设置锁的 TTL(超时时间) 用于覆盖配置中的 DefaultTTL。
需要注意:
- Redis 的最小 TTL 为 1 毫秒,更短的值返回 ErrInvalidTTL
- Etcd 的 TTL 基于 lease,必须至少 1 秒且为整秒,否则返回 ErrInvalidTTL
使用示例:
locker.Lock(ctx, "key", dlock.WithTTL(10*time.Second))
type Locker ¶
type Locker interface {
// Lock 阻塞式加锁
// 成功返回 nil,失败返回错误
// 如果上下文取消,返回 context.Canceled 或 context.DeadlineExceeded
// 空字符串 key 返回 ErrInvalidKey。
//
// opts 支持的选项:
// - WithTTL(duration): 设置锁的超时时间
Lock(ctx context.Context, key string, opts ...LockOption) error
// TryLock 非阻塞式尝试加锁
// 成功获取锁返回 true, nil
// 锁已被占用返回 false, nil
// 发生错误返回 false, err
// 空字符串 key 返回 false, ErrInvalidKey。
//
// opts 支持的选项:
// - WithTTL(duration): 设置锁的超时时间
TryLock(ctx context.Context, key string, opts ...LockOption) (bool, error)
// Unlock 释放锁
// 只有锁的持有者才能成功释放
// 空字符串 key 返回 ErrInvalidKey。
Unlock(ctx context.Context, key string) error
// Lost reports asynchronous ownership loss for a held key. The channel
// yields ErrOwnershipLost once and then closes if renewal/session ownership
// is lost. A normal Unlock closes the channel without a value. Call Lost
// immediately after acquiring the lock and monitor it for the whole critical
// section. An empty key yields ErrInvalidKey and closes the channel. Lookup
// history after ownership loss is bounded; a channel obtained while the lock
// is live remains valid even if that diagnostic history is later evicted.
Lost(key string) <-chan error
// Close 关闭 Locker 的持有状态。
// 它会停止自动续期,并尽力释放当前 Locker 已持有的锁。
// 底层 Redis / Etcd 连接器仍由调用方负责关闭。
Close() error
}
Locker 定义了分布式锁的核心行为
type Option ¶
type Option func(*options)
Option DLock 组件初始化选项函数
func WithEtcdConnector ¶
func WithEtcdConnector(conn connector.EtcdConnector) Option
WithEtcdConnector 注入 Etcd 连接器
func WithLogger ¶
WithLogger 注入日志记录器 组件会自动添加 component=dlock 字段
func WithRedisConnector ¶
func WithRedisConnector(conn connector.RedisConnector) Option
WithRedisConnector 注入 Redis 连接器