locker

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Nov 3, 2025 License: Apache-2.0 Imports: 14 Imported by: 0

README

lock_manager

基于Go+Redis实现的分布式锁集中管理器,以下是性能压测结果:

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrLockExisted = errors.New("lock already exists")

Functions

This section is empty.

Types

type CallBackFunc

type CallBackFunc func(key string, err error)

CallBackFunc 锁的回调函数,包含处理结果

type Executor

type Executor interface {
}

Executor 分布式锁的执行器,真正执行锁的申请、释放、续约等操作

type LocalShardStore

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

LocalShardStore 锁的本地分片存储器

type Lock

type Lock struct {
	Key   string
	Token string
}

type LockType

type LockType string

LockType 锁的类型

const (
	LockTypeExclusive LockType = "exclusive"
	LockTypeShared    LockType = "shared"
)

func (LockType) String

func (l LockType) String() string

type Locker

type Locker interface {
	// Acquire 申请分布式锁
	Acquire(ctx context.Context, resource string, expiration time.Duration, callback CallBackFunc) (Lock, error)
	// Release 释放分布式锁
	Release(ctx context.Context, key, token string) error
	// Stop 停止分布式锁管理器
	Stop()
}

type ManagerConfig

type ManagerConfig struct {
	// 锁的前缀
	Prefix string
	// 管理器节点标识
	NodeID string
	// 分片的数量
	ShardCount int
	// 每一个分片下的Worker数量,这个Worker是处理同一批
	// 的锁的续约的功能,这里定义的是最多有几个
	ShardWorkers int
	// 每一个分片下的优先级队列的长度,每一个分片有一个
	QueueSize int
	// 每一个分片批次续约的锁数量
	BatchSize int
	// 单节点的最大锁数量
	MaxLocks int
	// 批次续约的时间间隔,比如每一轮次续约需要间隔(休眠)100毫秒
	RefreshInterval time.Duration
}

type ManagerStatus

type ManagerStatus bool
const (
	ManagerStatusRunning ManagerStatus = true
	ManagerStatusStopped ManagerStatus = false
)

func (ManagerStatus) Bool

func (ms ManagerStatus) Bool() bool

func (ManagerStatus) String

func (ms ManagerStatus) String() string

type MutexEntry

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

type MutexRefreshTask

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

type NodeLockManager

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

func NewNodeLockManagerWithClient

func NewNodeLockManagerWithClient(cfg *ManagerConfig, l logger.Logger, client redis.Cmdable) (*NodeLockManager, error)

NewNodeLockManagerWithClient 创建一个锁管理器,基于单机的Redis客户端

func NewNodeLockManagerWithCluster

func NewNodeLockManagerWithCluster(cfg *ManagerConfig, l logger.Logger, client redis.Cmdable) (*NodeLockManager, error)

NewNodeLockManagerWithCluster 创建一个锁管理器,基于Redis集群客户端

func (*NodeLockManager) Acquire

func (lm *NodeLockManager) Acquire(ctx context.Context, resource string, expiration time.Duration, callback CallBackFunc) (Lock, error)

func (*NodeLockManager) Release

func (lm *NodeLockManager) Release(ctx context.Context, key, token string) error

func (*NodeLockManager) Stop

func (lm *NodeLockManager) Stop()

type Priority

type Priority int

Priority 锁的优先级,分为极危、高危、中危、低危,根据锁的剩余时间来评级

const (
	PriorityCritical Priority = iota + 1
	PriorityHigh
	PriorityMedium
	PriorityLow
)

func (Priority) String

func (p Priority) String() string

type RefreshPriorityQueue

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

RefreshPriorityQueue 基于下次续约时间的优先级队列

func (*RefreshPriorityQueue) BatchPop

func (pq *RefreshPriorityQueue) BatchPop(batchSize int) []*RefreshTask

BatchPop 批量获取到期的任务

func (*RefreshPriorityQueue) Clear

func (pq *RefreshPriorityQueue) Clear()

Clear 清空队列

func (*RefreshPriorityQueue) Contains

func (pq *RefreshPriorityQueue) Contains(key string) bool

Contains 检查key是否存在

func (*RefreshPriorityQueue) GetNextRefreshTime

func (pq *RefreshPriorityQueue) GetNextRefreshTime() (int64, bool)

GetNextRefreshTime 获取下一个任务的刷新时间

func (*RefreshPriorityQueue) Len

func (pq *RefreshPriorityQueue) Len() int

Len 返回队列长度

func (*RefreshPriorityQueue) Peek

func (pq *RefreshPriorityQueue) Peek() (*RefreshTask, bool)

Peek 查看最早到期的任务(不移除)

func (*RefreshPriorityQueue) Push

func (pq *RefreshPriorityQueue) Push(key string, nextRefreshAt int64)

Push 添加或更新任务

func (*RefreshPriorityQueue) Remove

func (pq *RefreshPriorityQueue) Remove(key string) bool

Remove 移除指定key的任务

type RefreshTask

type RefreshTask struct {
	Key           string // 锁的key
	NextRefreshAt int64  // 下次续约时间戳(毫秒)
	// contains filtered or unexported fields
}

RefreshTask 续约任务

type Shard

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

Directories

Path Synopsis
Package mocks is a generated GoMock package.
Package mocks is a generated GoMock package.
pkg

Jump to

Keyboard shortcuts

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