disgo

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Feb 17, 2025 License: BSD-2-Clause Imports: 12 Imported by: 0

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 *redis.Client, 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

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

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

func (*DistributedLock) SetLockKeyPrefix

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