disgo

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Jan 20, 2022 License: BSD-2-Clause Imports: 12 Imported by: 0

README

English | 中文

DisGo Introduce

DisGo is a distributed lock based on Redis, developed in Golang language. The name comes from Distributed, Disco and Golang, I wish you all to write code as easy as dancing disco.

Features of DisGo

Reentrant Lock

DisGo is a reentrant lock. It uses the Redis's Hash type as the lock, the hash-name is the lock name, the hash-key stores the unique id of the current lock-holding thread, and the hash-value stores the current number of locks.

Fair Lock

Golang itself does not have thread-safe queues to use. For convenience, DisGo uses Redis's ZSet to simulate queues, which ensures first-in first-out(FIFO) to a certain extent and provides a fair lock.

Auto-Renew

DisGo provides an automatic renewal function to prevent data errors caused by early release of locks before business execution is completed.

Spin Lock

DisGo is a spin lock, which will automatically retry the lock grab within the set waiting time until the lock is grabbed or the wait times out.

More Efficient

DisGo uses Redis's publish and subscribe, and will receive the message as soon as the lock is released, and then execute the lock according to the order of the waiting queue.

DisGo locking process

Click Me

API Introduction

Acquire lock object
    redisClient := redis.NewClient(&redis.Options{
        Network: "tcp",
        Addr:    "127.0.0.1:6379",
    })
    lock, err := disgo.GetLock(redisClient, "test")
Ordinary lock (no spin lock and automatic renewal required)
    success, err := lock.Lock(ctx, 5*time.Second, 10*time.Second)
Spin lock (no auto-renewal required)
    success, err := lock.TryLock(ctx, 5*time.Second, 10*time.Second)
Spin lock (auto-renew)
    success, err := lock.TryLockWithSchedule(ctx, 5*time.Second)
Unlock (Universal)
    success, err := lock.Release(ctx)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ConfigOption

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

type DistLock

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

type DistributedLock

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

func GetLock

func GetLock(redisClient RedisClient, lockName string) (*DistributedLock, error)

GetLock is an initialization object that needs to pass in redisClient and the name of the lock. The return value is a DistributedLock object, you need to use this object to perform lock and unlock operations, or set related properties.

func (*DistributedLock) Lock

func (dl *DistributedLock) Lock(ctx context.Context, expiryTime time.Duration) (bool, error)

Lock is a normal lock and will not have any retry mechanism. Notice! Because there is no retry mechanism, there is a high probability that the lock will fail under high concurrency. This is a reentrant lock.

func (*DistributedLock) Release

func (dl *DistributedLock) Release(ctx context.Context) (bool, error)

Release is a general release lock method, and all three locks above can be used.

func (*DistributedLock) SetExpiry added in v1.0.1

func (dl *DistributedLock) SetExpiry(expiry time.Duration)

SetExpiry sets the expiration time for TryLockWithSchedule, the default is 30 seconds.

func (*DistributedLock) SetLockKeyPrefix added in v1.0.1

func (dl *DistributedLock) SetLockKeyPrefix(prefix string)

SetLockKeyPrefix set the prefix name of the lock, which is convenient for classifying and managing locks of the same type. It has default values: "GoDistRL"

func (*DistributedLock) TryLock

func (dl *DistributedLock) TryLock(ctx context.Context, expiryTime, waitTime time.Duration) (bool, error)

TryLock is a relatively fair lock with a waiting queue and a retry mechanism. If the lock is successful, it will return true. If the lock fails, it will enter the queue and wait to be woken up, or it will return false if it times out. This is a reentrant lock.

func (*DistributedLock) TryLockWithSchedule

func (dl *DistributedLock) TryLockWithSchedule(ctx context.Context, waitTime time.Duration) (bool, error)

TryLockWithSchedule is the same as TryLock, but it will open an additional thread to ensure that the lock will not expire in advance, which means that you must release the lock manually, otherwise a deadlock will occur. This is a reentrant lock.

type RedisClient

type RedisClient interface {
	Eval(ctx context.Context, script string, keys []string, args ...interface{}) *redis.Cmd
	EvalSha(ctx context.Context, sha1 string, keys []string, args ...interface{}) *redis.Cmd
	ScriptExists(ctx context.Context, hashes ...string) *redis.BoolSliceCmd
	ScriptLoad(ctx context.Context, script string) *redis.StringCmd
	Subscribe(ctx context.Context, channels ...string) *redis.PubSub
	ZRevRange(ctx context.Context, key string, start, stop int64) *redis.StringSliceCmd
	ZRem(ctx context.Context, key string, members ...interface{}) *redis.IntCmd
}

Jump to

Keyboard shortcuts

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