redis 是 AP 模型, 需要定一致性的场景最好使用 etcd
- redis setnx
- redis redlock
- etcd
go 1.15.6
// REDIS
addr := "localhost:6379"
password := ""
db := 0
ttl := 2*time.Second
interval := 1*time.Second
lockName := "redis"
redisLock := &RedisMutex{}
redisLock.Init(addr, password, db, ttl, interval)
managerLock := &ManagerMutex{IMutex: redisLock}
err := managerLock.Lock(lockName)
if err != nil {
....
}
managerLock.RenewLock(lockName) // if need to renew lock
do_your_work()
err := managerLock.UnLock(lockName)
if err != nil {
...
// network error, lock expire
// rollback
}
// ETCD
etcdMutex := &EtcdMutex{}
etcdMutex.Init([]string{"0.0.0.0:2379"}, 2*time.Second)
managerLock := &ManagerMutex{IMutex: etcdMutex}
err := managerLock.Lock(lockName)
if err != nil {
....
}
do_your_work()
err := managerLock.UnLock(lockName)
if err != nil {
...
// network error, lock expire
// rollback
}