lock

package
v2.1.0 Latest Latest
Warning

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

Go to latest
Published: Jul 27, 2022 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package lock 分布式锁实现 分布式锁需要有客户端信息,只可以获得锁的客户端自己解锁或者等待锁自己过期 当未能获得锁需要等待锁释放时可以通过wait接口实现

Index

Constants

View Source
const MiniCheckPeriod = 100 * time.Microsecond

MiniCheckPeriod 等待的轮询间隔最低100微秒

Variables

View Source
var ErrAlreadyLocked = errors.New("already locked")

ErrAlreadyLocked 该锁已经被锁定

View Source
var ErrAlreadyUnLocked = errors.New("already unlocked")

ErrAlreadyUnLocked 该锁已经被解锁

View Source
var ErrCheckPeriodLessThan100Microsecond = errors.New("checkperiod less than 100 microsecond")

ErrCheckPeriodLessThan100Microsecond checkperiod小于100微秒

View Source
var ErrNoRightToUnLock = errors.New("no right to unlock")

ErrNoRightToUnLock 无权解锁该锁

Functions

func WithAutoRefreshInterval added in v2.0.1

func WithAutoRefreshInterval(autoRefreshInterval string) optparams.Option[Options]

WithAutoRefreshInterval 设置自动刷新过期时间的设置

func WithCheckPeriod added in v2.0.1

func WithCheckPeriod(checkperiod time.Duration) optparams.Option[Options]

WithCheckPeriod 锁的设置项,设置检查锁状态的轮询间隔时间

func WithClientID added in v2.0.1

func WithClientID(key string) optparams.Option[Options]

WithSpecifiedKey 中间件通用设置,指定使用的键,注意设置key后namespace将失效

func WithKey added in v2.0.1

func WithKey(key string) optparams.Option[Options]

WithKey 中间件通用设置,指定使用的键,注意设置后namespace依然有效

func WithMaxTTL added in v2.0.1

func WithMaxTTL(maxTTL time.Duration) optparams.Option[Options]

WithMaxTTL 设置token消减间隔时长,单位s

func WithNamespace added in v2.0.1

func WithNamespace(ns ...string) optparams.Option[Options]

WithNamespace 中间件通用设置,指定锁的命名空间

func WithSpecifiedKey added in v2.0.1

func WithSpecifiedKey(key string) optparams.Option[Options]

WithSpecifiedKey 中间件通用设置,指定使用的键,注意设置key后namespace将失效

func WithTaskCron added in v2.0.1

func WithTaskCron(taskCron *cron.Cron) optparams.Option[Options]

WithTaskCron 设置定时器

Types

type Lock

type Lock struct {
	*middlewarehelper.MiddleWareAbc
	*clientIdhelper.ClientIDAbc
	// contains filtered or unexported fields
}

Lock 分布式锁结构

func New

func New(cli redis.UniversalClient, opts ...optparams.Option[Options]) (*Lock, error)

New 新建一个锁对象

func (*Lock) Check

func (l *Lock) Check(ctx context.Context) (bool, error)

Check 检测是否是锁定状态,true为锁定状态,false为非锁定状态

func (*Lock) Lock

func (l *Lock) Lock(ctx context.Context) error

Lock 设置锁

func (*Lock) Unlock

func (l *Lock) Unlock(ctx context.Context) error

Unlock 释放锁,已经释放锁或无权释放锁时报错

func (*Lock) Wait

func (l *Lock) Wait(ctx context.Context) error

Wait 等待锁释放

type LockInterface added in v2.0.1

type LockInterface interface {
	Lock(context.Context) error
	Unlock(context.Context) error
	Wait(context.Context) error
}

LockInterface 锁对象的接口

type Options added in v2.0.1

type Options struct {
	CheckPeriod    time.Duration                                //等待的轮询间隔
	MiddlewareOpts []optparams.Option[middlewarehelper.Options] //初始化Middleware的配置
	ClientIDOpts   []optparams.Option[clientIdhelper.Options]   //初始化clientID的配置
}

Jump to

Keyboard shortcuts

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