lock

package
v1.3.1 Latest Latest
Warning

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

Go to latest
Published: Oct 19, 2022 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

View Source
const (
	Error = iota + 1
	Warn
	Info
	Debug
)

Variables

View Source
var (
	KeyEmptyError       = errors.New("lock key is empty")
	RedisClientNilError = errors.New("redis client is nil")
	TimeoutError        = errors.New("acquire lock timeout")
	NotFoundLockError   = errors.New("lock is not found")
)

Functions

func CustomTimeEncoder

func CustomTimeEncoder(t time.Time, enc zapcore.PrimitiveArrayEncoder)

自定义日志输出时间格式

func Zap

func Zap() (logger *zap.Logger)

Types

type BoolResult

type BoolResult struct {
	Result
}

func (*BoolResult) BoolVal

func (r *BoolResult) BoolVal() bool

type Config

type Config struct {
	Timeout        time.Duration //lock Timeout for acquiring locks
	Expire         time.Duration //lock expiration time
	EnableWatchdog bool          //Whether to enable the watchdog to renew the lock
	Logger         Logger
}

type DefaultLogger

type DefaultLogger struct {
	Level LogLevel
	Log   *zap.Logger
}

func (*DefaultLogger) Debug

func (l *DefaultLogger) Debug(msg string, data ...interface{})

func (*DefaultLogger) Error

func (l *DefaultLogger) Error(msg string, data ...interface{})

func (*DefaultLogger) Info

func (l *DefaultLogger) Info(msg string, data ...interface{})

func (*DefaultLogger) Warn

func (l *DefaultLogger) Warn(msg string, data ...interface{})

type LockContext

type LockContext context.Context

LockContext goroutine lock context

func Context

func Context() LockContext

Context Obtain the goroutine lock context, which is required when locking. When using reentrant locks, the context of multiple locking must be the same.

type LockOperator

type LockOperator interface {
	GetLock(key string, config Config) (lock Locker, err error)
	Lock(key string, ctx LockContext) error
	TryLock(key string, ctx LockContext, timeout time.Duration) error
	Unlock(key string) (bool, error)
}

type Locker

type Locker interface {
	Lock(ctx LockContext) error
	TryLock(ctx LockContext, time time.Duration) error //Attempt to lock for a fixed duration
	Unlock() (bool, error)
}

type LogLevel

type LogLevel uint8

type Logger

type Logger interface {
	Debug(msg string, data ...interface{})
	Info(msg string, data ...interface{})
	Warn(msg string, data ...interface{})
	Error(msg string, data ...interface{})
}

type RedisClientAdapter

type RedisClientAdapter interface {
	RedisOps
}

type RedisLock

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

RedisLock Redis distributed lock structure, implementing Locker interface

func NewRedisLock

func NewRedisLock(lockKey string, redisClient RedisClientAdapter, config *Config) (*RedisLock, error)

NewRedisLock lockKey and redisClient are required parameters, and options are optional parameters.

func (*RedisLock) Lock

func (r *RedisLock) Lock(ctx LockContext) error

Lock ctx is the context of the current goroutine, which can be obtained through Context()

func (*RedisLock) TryLock

func (r *RedisLock) TryLock(ctx LockContext, timeout time.Duration) error

TryLock ctx is the context of the current goroutine, which can be obtained through Context(). timeout is the maximum wait time to acquire a lock.

func (*RedisLock) Unlock

func (r *RedisLock) Unlock() (bool, error)

type RedisLockOperator

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

func NewRedisLockOperator

func NewRedisLockOperator(client RedisClientAdapter, config Config) (*RedisLockOperator, error)

func (*RedisLockOperator) GetLock

func (operator *RedisLockOperator) GetLock(key string, config Config) (lock Locker, err error)

GetLock Get the lock object from locks and return it. If it does not exist, first create the lock object according to the key, then save the lock object to locks, and finally return the lock object.

func (*RedisLockOperator) Lock

func (operator *RedisLockOperator) Lock(key string, ctx LockContext) error

func (*RedisLockOperator) TryLock

func (operator *RedisLockOperator) TryLock(key string, ctx LockContext, timeout time.Duration) error

func (*RedisLockOperator) Unlock

func (operator *RedisLockOperator) Unlock(key string) (bool, error)

type RedisOps

type RedisOps interface {
	Expire(ctx context.Context, key string, expiration time.Duration) *BoolResult
	Eval(ctx context.Context, script string, keys []string, args ...interface{}) *Result
	HGet(ctx context.Context, key, field string) *StringResult
}

type Result

type Result struct {
	Val interface{}
	Err error
}

type StringResult

type StringResult struct {
	Result
}

func (*StringResult) StringVal

func (r *StringResult) StringVal() string

Jump to

Keyboard shortcuts

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