ReplicaLock

package module
v0.0.0-...-b764209 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 26, 2023 License: MIT Imports: 8 Imported by: 0

README

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/

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func SetRenewExpirationOption

func SetRenewExpirationOption(option bool)

SetRenewExpirationOption Enabled renewExpiration if 'option' is true. Disabled renewExpiration if 'option' is false.

Types

type ReplicaLock

type ReplicaLock struct {
	// contains filtered or unexported fields
}

func New

func New(Conn redis.Conn) (ReplicaLock, error)

New return a ReplicaLock.

func NewWithRawKeyName

func NewWithRawKeyName(Conn redis.Conn, RawKeyName string) (ReplicaLock, error)

NewWithRawKeyName return a ReplicaLock, this allows the user to set the key name for a more granular locking.

func (*ReplicaLock) ForceUnlock

func (RepLock *ReplicaLock) ForceUnlock() bool

ForceUnlock forces the deletion of the key (if the key exists), whether it has a lock or not.Please be aware of the risks.

func (*ReplicaLock) Lock

func (RepLock *ReplicaLock) Lock(timeout int64, leaseTime int64, TimeUnit string) error

Lock will keep trying to acquire the lock until it succeeds. 'timeout' is the maximum time we wait for all replicas to finish synchronizing. 'leaseTime' is the existence time of the lock. TimeUnit is a unit of time for 'waitTime','timeout','leaseTime'. Note that we will convert the input parameters to make it in the legal range.

func (*ReplicaLock) SetRawKeyName

func (RepLock *ReplicaLock) SetRawKeyName(RawKeyName string)

SetRawKeyName allow users to change the name after creating a lock, or to use the same ReplicaLock again, but lock other things.

func (*ReplicaLock) TryLock

func (RepLock *ReplicaLock) TryLock(waitTime int64, timeout int64, leaseTime int64, TimeUnit string) (bool, error)

TryLock attempt to acquire the lock during the 'waitTime' time. 'timeout' is the maximum time we wait for all replicas to finish synchronizing. 'leaseTime' is the existence time of the lock. TimeUnit is a unit of time for 'waitTime','timeout','leaseTime'. the bool returned by Trylock tells the user whether the lock was successful(true) or not(false). Note that we will convert the input parameters to make it in the legal range.

func (*ReplicaLock) Unlock

func (RepLock *ReplicaLock) Unlock()

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL