基于 etcd 实现的分布式锁

2023年 9月 21日 58.2k 0

etcd 的分布式锁结构?

  • Session 用于标识 etcd 与客户端的连接,每一个 Session 都有一个唯一的 LeaseID 来实现租约机制
  • Mutex 通过客户端传入的 pfx 标识同一把分布式锁,
  • 使用 pfx + LeaseID 得到 myKey 标识持有该锁的客户端
  • 使用 myRev 标识这个前缀 pfx 下的 revision 版本号(revision 在每次用户修改数据时都会递增)
// NewLocker creates a sync.Locker backed by an etcd mutex.
func NewLocker(s *Session, pfx string) sync.Locker {
return &lockerMutex{NewMutex(s, pfx)}
}

// Mutex implements the sync Locker interface with etcd
type Mutex struct {
s *Session

pfx string
myKey string
myRev int64
hdr *pb.ResponseHeader
}

// Session represents a lease kept alive for the lifetime of a client.
// Fault-tolerant applications may use sessions to reason about liveness.
type Session struct {
client *v3.Client
opts *sessionOptions
id v3.LeaseID

cancel context.CancelFunc
donec

相关文章

JavaScript2024新功能:Object.groupBy、正则表达式v标志
PHP trim 函数对多字节字符的使用和限制
新函数 json_validate() 、randomizer 类扩展…20 个PHP 8.3 新特性全面解析
使用HTMX为WordPress增效:如何在不使用复杂框架的情况下增强平台功能
为React 19做准备:WordPress 6.6用户指南
如何删除WordPress中的所有评论

发布评论