redislock

package module
v0.0.0-...-0027f7a Latest Latest
Warning

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

Go to latest
Published: May 15, 2020 License: MIT Imports: 8 Imported by: 0

README

A distributed lock base on redis

Usage

Choose a redis client you like and write an implementation of IRedisClient.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type FactoryOptions

type FactoryOptions struct {
	// IDGenerator a function generate unique id for each lock, if not
	IDGenerator func() string
	// DefaultTimeout default lock release time, if not passed `DefaultTimeout` will be 5 minutes.
	DefaultTimeout time.Duration
}

FactoryOptions custom params to replace default value

type ILock

type ILock interface {
	// Lock lock a resource, if failed will wait until success or reach the max times.
	Lock(ctx context.Context)
	// Realease release lock if lock id is matched.
	Release(ctx context.Context)
	// TryLock try to lock resource once.
	TryLock(ctx context.Context) (err error, success bool)
	// Extend extend lock time.
	Extend(ctx context.Context) (err error, success bool)
	// Get lock left seconds.
	TTL(ctx context.Context)
}

ILock Lock client interface.

type ILockFactory

type ILockFactory interface {
	GetLock(ctx context.Context, resourceID string) (ILock, error)
}

ILockFactory A lock factory interface.

func NewLockFactory

func NewLockFactory(ctx context.Context, redisClient IRedisClient, options *FactoryOptions) (ILockFactory, error)

NewLockFactory generate a lock factory.

type IRedisClient

type IRedisClient interface {
	RunExtendCmd(script, key, lockID string, expiration time.Duration) (err error, success bool)
	RunUnlockCmd(script, key, lockID string) (err error, success bool)
	SetNX(key, lockID string, expiration time.Duration) (err error, success bool)
}

IRedisClient Redis client interface.

Jump to

Keyboard shortcuts

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