A distributed lock idea implemented by redis and golang.It is more secure than a simple redis distributed lock.It is still in the experimental stage.

Welcome to testing ReplicaLock:

go get

The implementation is almost the same as that of RedissonLock.But ReplicaLock uses the “WAIT” command to wait for all replicas to synchronize.When all replicas have completed writing the lock key, we think we got the lock.

An obvious problem about ReplicaLock: lock acquisition may fail due to network delay between master and slave.

This is to prevent lock loss after failover because the replica has not yet completed synchronization with the master node.

A simple example of using ReplicaLock:

func main() {    // We use "redigo" to connect to redisConn, err := redis.Dial("tcp", ":6379")if err != nil {panic(err)}Replock, err := ReplicaLock.New(Conn)if err != nil {panic(err)}    // 'WAIT' Command 'timeout' is 1s, ReplicaLock's lease time is 30s.err = Replock.Lock(1, 30, "s")if err != nil {panic(err)}Replock.Unlock()}


View Github