lock

package
v0.0.0-...-2a88403 Latest Latest
Warning

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

Go to latest
Published: Apr 5, 2024 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	Prefix    string   `json:",optional"` // 锁前缀,如 /xlock/
	Endpoints []string `json:",optional"` // 节点列表
	Username  string   `json:",optional"` // 用户名
	Password  string   `json:",optional"` // 密码
}

Config 分布式锁配置

type Lock

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

Lock 分布式锁

func (*Lock) Close

func (l *Lock) Close()

Close 关闭锁

注意:不关闭会导致 session 内存泄漏

在以下几种情况会自动调用 Close 方法:

  1. TryLock 发生失败时
  2. Lock 发生失败时
  3. 执行 Unlock 时

其余情况需要自行显示调用 Close 方法

func (*Lock) Lock

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

Lock 上锁,若不能获取锁会阻塞并等待获取锁

ctx 最好带有 timeout

func (*Lock) TryLock

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

TryLock 尝试上锁,若不能获取锁会立刻返回

ctx 最好带有 timeout

func (*Lock) Unlock

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

Unlock 解锁

ctx 最好带有 timeout

type Locker

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

Locker 分布式锁客户端

func MustNewLocker

func MustNewLocker(c Config, opts ...Option) *Locker

MustNewLocker 新建分布式锁客户端

func NewLocker

func NewLocker(c Config, opts ...Option) (*Locker, error)

NewLocker 新建分布式锁客户端

func (*Locker) NewLock

func (l *Locker) NewLock(key string, ttl ...int) (*Lock, error)

NewLock 新建分布式锁

key 为将要上锁的键,ttl 为键的租约到期时间,默认为 10s

type Option

type Option func(l *Locker)

Option 可选配置

func WithClient

func WithClient(client *clientv3.Client) Option

WithClient 使用指定 etcd 客户端

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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