dlock

package
v1.4.65 Latest Latest
Warning

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

Go to latest
Published: Nov 14, 2026 License: MIT Imports: 11 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

dlock

dlock is a distributed lock library based on redsync and etcd. It provides a simple and easy-to-use API for acquiring and releasing locks.


Example of use

Redis Lock
package main

import (
    "context"
    "fmt"
    "time"
    "github.com/18721889353/sunshine/pkg/goredis"
    "github.com/18721889353/sunshine/pkg/dlock"
)

func main() {
    redisCli, err := goredis.Init("default:123456@192.168.3.37:6379")  // single redis instance
    // clusterRedisCli, err := goredis.InitCluster(...)                // or init redis cluster
    // redisCli, err := goredis.InitSentinel(...)                      // or init redis sentinel
    if err != nil {
        panic(err)
    }
    defer redisCli.Close()

    locker, err := NewRedisLock(redisCli, "test_lock")
    if err != nil {
        panic(err)
    }
    ctx, _ := context.WithTimeout(context.Background(), time.Second*10)

    // case 1: try to acquire lock, unblock if failed
    {
        ok, err := locker.TryLock(ctx)
        if err != nil {
            panic(err)
        }
        if !ok {
            fmt.Println("failed to acquire lock")
            return
        }
        defer func() {
            if err := locker.Unlock(ctx); err != nil {
                panic(err)
            }
        }()
        // do something here
    }

    // case 2: lock acquired, block until released, timeout, ctx error
    {
        if err := locker.Lock(ctx); err != nil {
            panic(err)
        }
        defer func() {
            if err := locker.Unlock(ctx); err != nil {
                panic(err)
            }
        }()
        // do something here
    }
}

Etcd Lock
package main

import (
    "context"
    "fmt"
    "time"
    "github.com/18721889353/sunshine/pkg/etcdcli"
    "github.com/18721889353/sunshine/pkg/dlock"
)

func main() {
    endpoints := []string{"192.168.3.37:2379"}
    cli, err := etcdcli.Init(endpoints,  etcdcli.WithConnectTimeout(time.Second*5))
    if err!= nil {
        panic(err)
    }
    defer cli.Close()

    locker, err := dlock.NewEtcd(cli, "sunshine/dlock", 10)
    if err != nil {
        panic(err)
    }
    ctx, _ := context.WithTimeout(context.Background(), time.Second*10)

    // case 1: try to acquire lock, unblock if failed
    {
        ok, err := locker.TryLock(ctx)
        if err != nil {
            panic(err)
        }
        if !ok {
            fmt.Println("failed to acquire lock")
            return
        }
        defer func() {
            if err := locker.Unlock(ctx); err != nil {
                panic(err)
            }
        }()
        // do something here
    }

    // case 2: lock acquired, block until released, timeout, ctx error
    {
        if err := locker.Lock(ctx); err != nil {
            panic(err)
        }
        defer func() {
            if err := locker.Unlock(ctx); err != nil {
                panic(err)
            }
        }()
        // do something here
    }
}

Documentation

Overview

Package dlock 提供分布式锁原语,支持 Redis 和 etcd。

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type EtcdLock

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

EtcdLock 结构体表示一个基于 etcd 的分布式锁

func (*EtcdLock) Close

func (l *EtcdLock) Close() error

Close 释放锁并关闭 etcd 会话 返回值: - error: 如果关闭会话失败则返回错误

func (*EtcdLock) Lock

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

Lock 阻塞直到获取到锁或上下文被取消 参数: - ctx: 上下文,用于控制锁的获取操作 返回值: - error: 如果获取锁失败则返回错误

func (*EtcdLock) TryLock

func (l *EtcdLock) TryLock(ctx context.Context) (bool, error)

TryLock 尝试获取锁而不阻塞 参数: - ctx: 上下文,用于控制锁的获取操作 返回值: - bool: 如果成功获取锁则返回 true,否则返回 false - error: 如果发生错误则返回错误

func (*EtcdLock) Unlock

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

Unlock 释放锁 参数: - ctx: 上下文,用于控制锁的释放操作 返回值: - error: 如果释放锁失败则返回错误

type Locker

type Locker interface {
	// Lock 阻塞直到获取到锁或上下文被取消。
	// 参数:
	// - ctx: 上下文,用于控制锁的获取操作。
	// 返回值:
	// - error: 如果获取锁失败则返回错误。
	Lock(ctx context.Context) error

	// Unlock 释放锁,如果解锁成功,键将被自动删除。
	// 参数:
	// - ctx: 上下文,用于控制锁的释放操作。
	// 返回值:
	// - error: 如果释放锁失败则返回错误。
	Unlock(ctx context.Context) error

	// TryLock 尝试获取锁而不阻塞。
	// 参数:
	// - ctx: 上下文,用于控制锁的获取操作。
	// 返回值:
	// - bool: 如果成功获取锁则返回 true,否则返回 false。
	// - error: 如果发生错误则返回错误。
	TryLock(ctx context.Context) (bool, error)

	// Close 关闭锁资源。
	// 返回值:
	// - error: 如果关闭资源失败则返回错误。
	Close() error
}

Locker 是一个接口,封装了基本的锁定操作。

func NewEtcd

func NewEtcd(client *clientv3.Client, key string, ttl int) (Locker, error)

NewEtcd 创建一个新的 etcd 分布式锁 参数: - client: etcd 客户端实例,不能为空 - key: 锁的键,不能为空 - ttl: 会话的过期时间(秒),如果小于等于 0 则使用默认值 返回值: - Locker: 分布式锁接口实例 - error: 如果创建失败则返回错误

func NewRedisClusterLock

func NewRedisClusterLock(clusterClient *redis.ClusterClient, key string, options ...redsync.Option) (Locker, error)

NewRedisClusterLock 创建一个新的 Redis 集群分布式锁 参数: - clusterClient: Redis 集群客户端实例,不能为空 - key: 锁的键,不能为空 - options: 可选的 redsync 选项 返回值: - Locker: 分布式锁接口实例 - error: 如果创建失败则返回错误

func NewRedisLock

func NewRedisLock(client *redis.Client, key string, options ...redsync.Option) (Locker, error)

NewRedisLock 创建一个新的 Redis 分布式锁 参数: - client: Redis 客户端实例,不能为空 - key: 锁的键,不能为空 - options: 可选的 redsync 选项 返回值: - Locker: 分布式锁接口实例 - error: 如果创建失败则返回错误

type RedisLock

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

RedisLock 使用 Redis 实现的分布式锁

func (*RedisLock) Close

func (l *RedisLock) Close() error

Close 对于 RedisLock 是一个空操作 返回值: - error: 始终返回 nil

func (*RedisLock) Lock

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

Lock 阻塞直到获取到锁或上下文被取消 参数: - ctx: 上下文,用于控制锁的获取操作 返回值: - error: 如果获取锁失败则返回错误

func (*RedisLock) TryLock

func (l *RedisLock) TryLock(ctx context.Context) (bool, error)

TryLock 尝试获取锁而不阻塞 参数: - ctx: 上下文,用于控制锁的获取操作 返回值: - bool: 如果成功获取锁则返回 true,否则返回 false - error: 如果发生错误则返回错误

func (*RedisLock) Unlock

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

Unlock 释放锁,如果解锁成功,键将被自动删除 参数: - ctx: 上下文,用于控制锁的释放操作 返回值: - error: 如果释放锁失败则返回错误

Jump to

Keyboard shortcuts

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