🚨 Shai-Hulud Strikes Again:834 Packages Compromised.Technical Analysis →
Socket
Book a DemoInstallSign in
Socket

github.com/ncghost1/redis-replicalock

Package Overview
Dependencies
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

github.com/ncghost1/redis-replicalock

Source
Go Modules
Version
v0.0.0-20230326150430-b7642096d72c
Version published
Created
Source

Redis-ReplicaLock

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 github.com/ncghost1/Redis-ReplicaLock

The implementation is almost the same as that of RedissonLock,but "pubsub" is not used here. In RedissonLock, a unlocked message is sent through the channel to the thread that has not acquired the lock.The implementation here simply makes a loop to try to acquire the lock.The most important differences is ReplicaLock uses the "WAIT" command to wait for all replicas to synchronize.When all replicas have completed writing the lock key, then we think we acquire the lock.

The following is an abstract example of a client successfully acquiring replicalock: Because redis currently does not support the use of the "WAIT" command in lua scripts,we can only send the "WAIT" command to the redis master node after setting the lock successfully.I know that the current implementation may be a little weird, but I just want to show this idea.

An obvious problem about ReplicaLock: lock acquisition may fail due to network delay between master and replica, or the replicas is blocked due to slow query and cannot be synchronized.

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 redis
	Conn, 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()
}

中文介绍(Blog):https://www.eririspace.cn/2022/06/19/ReplicaLock/

FAQs

Package last updated on 26 Mar 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts