lockx

package
v0.0.0-...-1afac4e Latest Latest
Warning

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

Go to latest
Published: Sep 14, 2022 License: MIT Imports: 10 Imported by: 0

README

lockx

分布式锁

go-redis 实现

https://github.com/go-redis/redis

If you are using Redis 6, install go-redis/v8:

go get github.com/go-redis/redis/v8

If you are using Redis 7, install go-redis/v9:

go get github.com/go-redis/redis/v9
redisgo 实现

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNotObtained is returned when a lock cannot be obtained.
	ErrNotObtained = errors.New("redislock: not obtained")

	// ErrLockNotHeld is returned when trying to release an inactive lock.
	ErrLockNotHeld = errors.New("redislock: lock not held")

	// ErrNotSupport is returned when trying to release an inactive lock.
	ErrNotSupport = errors.New("redislock: redisL2Locker.Unlock() Method 不需调用。释放锁请使用 redisL2Locker.Lock() 返回的 UnlockHandler。")
)

Functions

func Init

func Init(factory LockerFactory)

Types

type LocalLockerFactory

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

LocalLockerFactory 本地锁工厂

func (*LocalLockerFactory) Del

func (llf *LocalLockerFactory) Del(key string)

Del 删除一个锁

func (*LocalLockerFactory) Mutex

func (llf *LocalLockerFactory) Mutex(ctx context.Context, options ...Option) (Locker, error)

Mutex 获取普通锁

func (*LocalLockerFactory) MutexL2

func (llf *LocalLockerFactory) MutexL2(ctx context.Context, options ...Option) (Locker, error)

MutexL2 本地锁没有二级锁概念

func (*LocalLockerFactory) RWMutex

func (llf *LocalLockerFactory) RWMutex(ctx context.Context, options ...Option) (RWLocker, error)

RWMutex 获取读写锁

type Locker

type Locker interface {
	Lock() error
	Unlock() error
}

Locker 锁接口

func Mutex

func Mutex(ctx context.Context, opts ...Option) (Locker, error)

func MutexL2

func MutexL2(ctx context.Context, opts ...Option) (Locker, error)

type LockerFactory

type LockerFactory interface {
	Mutex(context.Context, ...Option) (Locker, error)
	MutexL2(ctx context.Context, options ...Option) (Locker, error)
	RWMutex(context.Context, ...Option) (RWLocker, error)
}

LockerFactory 锁创建工厂实现接口

func NewLocalLockerFactory

func NewLocalLockerFactory() LockerFactory

NewLocalLockerFactory 获取本地所工厂,单例模式

type LockerMeta

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

LockerMeta 锁配置元数据

type Option

type Option func(*LockerMeta)

Option 锁配置元数据设置

func Key

func Key(id string) Option

Key 锁 id

func TTL

func TTL(ttl time.Duration) Option

TTL 锁过期时间

type RWLocker

type RWLocker interface {
	Locker
	RLock() error
	RUnlock() error
	RLocker() Locker
}

RWLocker 读写锁接口

func RWMutex

func RWMutex(ctx context.Context, opts ...Option) (RWLocker, error)

type RedisClient

type RedisClient interface {
	SetNX(ctx context.Context, key string, value interface{}, expiration time.Duration) (bool, error)
	Eval(ctx context.Context, script string, keys []string, args ...interface{}) (interface{}, error)
}

RedisClient is a minimal client interface

type RedisLockerFactory

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

RedisLockerFactory 分布式工厂锁实现

func NewRedisLockerFactory

func NewRedisLockerFactory(client RedisClient) *RedisLockerFactory

NewRedisLockerFactory 新建 redis 分布式锁工厂

func (*RedisLockerFactory) Mutex

func (rlf *RedisLockerFactory) Mutex(ctx context.Context, options ...Option) (Locker, error)

Mutex tries to obtain a new lock using a key with the given TTL. May return ErrNotObtained if not successful.

func (*RedisLockerFactory) MutexL2

func (rlf *RedisLockerFactory) MutexL2(ctx context.Context, options ...Option) (Locker, error)

MutexL2 获取针对相同的 key 优化的二级锁

func (*RedisLockerFactory) RWMutex

func (rlf *RedisLockerFactory) RWMutex(ctx context.Context, options ...Option) (RWLocker, error)

RWMutex tries to obtain a new lock using a key with the given TTL. May return ErrNotObtained if not successful.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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