rlock

package module
v0.0.0-...-6cb212c Latest Latest
Warning

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

Go to latest
Published: Apr 1, 2024 License: MIT Imports: 10 Imported by: 0

README

RLock - Redis 分布式锁

概述

RLock 是一个使用 Redis 实现的分布式锁库。它允许多个进程或线程在分布式系统中安全地协同工作,以确保一次只有一个执行者可以持有锁。

功能

  • 分布式锁定:通过 Redis 实现跨多个节点的锁定机制。
  • 可配置的锁定时间:可以设置最大锁定时间,防止死锁。
  • 时钟漂移考虑:考虑到时钟漂移,增加锁定的稳定性。
  • 重试机制:在无法获得锁时,提供了重试获取锁的机制。
  • 随机字符串生成:为每个锁生成唯一标识,确保锁的唯一性。

使用方法

创建 RLock 实例

首先,需要创建一个 RLock 实例。可以通过提供一组 Redis 客户端实例来完成。

clients := []*redis.Client{...} // Redis 客户端实例列表
rlock, err := NewRLock(clients)
if err != nil {
    // 处理错误
}
配置选项

可以通过选项函数来配置 RLock 实例:

  • WithMaxLockTime(maxLockTime time.Duration) 设置最大锁定时间。
  • WithLimit(limit int) 设置并发限制。
  • WithLockID(lockID string) 设置锁的唯一标识。
  • WithClockDrift(drift float64) 设置时钟漂移容忍度。
  • WithRetryTimes(retry int) 设置重试次数。
获取锁
ttl := 10 * time.Second // 锁定时间
ctx := context.Background()
leftTTL, err := rlock.Lock(ctx, "my-lock-key", ttl)
if err != nil {
    // 处理错误
}
// 成功获取锁,leftTTL 是剩余的锁定时间
释放锁
err := rlock.Unlock(ctx, "my-lock-key")
if err != nil {
    // 处理错误
}
// 成功释放锁

注意事项

确保 Redis 客户端列表的数量为奇数,以避免脑裂问题。 锁定时间不应该过长,以免影响系统性能。 使用分布式锁时,务必确保所有操作都是幂等的。

许可

RLock 是在 MIT 许可下发布的。有关详细信息,请查看 LICENSE 文件。

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Opt

type Opt func(*RLock)

func WithClockDrift

func WithClockDrift(drift float64) Opt

WithClockDrift sets the tolerance for clock drift between different nodes.

func WithLimit

func WithLimit(limit int) Opt

WithLimit sets the concurrency limit for lock operations.

func WithLockID

func WithLockID(lockID string) Opt

WithLockID sets the identifier for the lock.

func WithMaxLockTime

func WithMaxLockTime(maxLockTime time.Duration) Opt

WithMaxLockTime sets the maximum amount of time a lock can be held.

func WithRandomFunc

func WithRandomFunc(randomFunc func(int) string) Opt

WithRandomFunc sets the function used to generate random strings.

func WithRetryTimes

func WithRetryTimes(retry int) Opt

WithRetryTimes sets the number of times to retry acquiring the lock.

type RLock

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

func NewRLock

func NewRLock(clients []*redis.Client, opts ...Opt) (*RLock, error)

NewRLock creates a new distributed lock instance.

func (*RLock) GetLockID

func (r *RLock) GetLockID() string

func (*RLock) Lock

func (r *RLock) Lock(ctx context.Context, key string, ttl time.Duration) (time.Duration, error)

Lock attempts to acquire the lock and returns the remaining time the lock will be held.

func (*RLock) Unlock

func (r *RLock) Unlock(ctx context.Context, key string) error

Unlock attempts to release the lock.

Jump to

Keyboard shortcuts

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