-
Notifications
You must be signed in to change notification settings - Fork 0
xdlock
omeyang edited this page Sep 17, 2026
·
1 revision
稳定性:Stable · 覆盖率:94.6% · 源码:
pkg/distributed/xdlock
分布式锁。两套后端:Redis(NX + Lua 释放)与 etcd(concurrency.Mutex)。
- 短期互斥(操作幂等保护、初始化串行化)
- 多副本中只让一个执行某操作(一次性任务)
- 选主(长期持有):用 xelection
- 限流/限并发:用 xsemaphore
import "github.com/omeyang/xkit/pkg/distributed/xdlock"
l, err := xdlock.NewRedis(redisClient, "lock:my-resource",
xdlock.WithTTL(30*time.Second),
)
defer l.Close()
held, err := l.Lock(ctx)
if err != nil { /* ... */ }
if !held { return nil }
defer l.Unlock(ctx)| 名称 | 说明 |
|---|---|
Locker interface |
Lock / TryLock / Unlock / Renew / Close |
NewRedis(client, key, opts...) Locker |
Redis 实现 |
NewEtcd(client, key, opts...) Locker |
etcd 实现 |
WithTTL / WithTries / WithRetryDelay / WithFailFast |
选项 |
- Redis NX + Lua 释放:避免误释放他人持有的锁
-
etcd v3.6 移除了
ErrLockReleased:1.23 兼容分支需移除该分支 -
Renew由调用方主动续期,库内不内置心跳 goroutine