distlock

package module
v0.0.0-...-0806b73 Latest Latest
Warning

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

Go to latest
Published: Oct 24, 2020 License: MIT Imports: 12 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

Distributed Lock Service

Usage

Distributed Lock Service can make sure that only one work unit (process| service | machine | ...) has access to the lock, which provides synchronization semantic for distributed application.

Contribution

Step 1
  • 🍴 Fork this repo!
Step 2
  • 🔨 HACK AWAY!
Step 3

License

  • This project is licensed under the MIT License - see the MIT license for details.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrDeadlock = errors.New("trying to acquire a lock twice by the same process")
)

Functions

func CloseRedisConn

func CloseRedisConn(p *RedisConnPool)

CloseRedisConn 释放TCP连接池.

func CloseZKConn

func CloseZKConn(conn *zk.Conn)

CloseZKConn 关闭TCP连接.

func EstablishZKConn

func EstablishZKConn(endpoints []string) (*zk.Conn, <-chan zk.Event)

EstablishZKConn 建立一条连接zookeeper集群的TCP连接.

Types

type DLockByRedis

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

DLockByRedis 通过redis实现的分布式锁

func NewDLockByRedis

func NewDLockByRedis(p *RedisConnPool) *DLockByRedis

NewDLockByRedis 获取DLockByRedis实例.

func (*DLockByRedis) TryLock

func (dlr *DLockByRedis) TryLock(timeoutInSecs int64) (string, bool)

TryLock 尝试获取分布式锁, 超时后就放弃 (不可重入锁).

func (*DLockByRedis) Unlock

func (dlr *DLockByRedis) Unlock(value string)

Unlock 释放分布式锁.

type DLockByZookeeper

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

DLockByZookeeper 通过zookeeper实现的分布式锁

func NewDLockByZookeeper

func NewDLockByZookeeper(conn *zk.Conn) *DLockByZookeeper

NewDLockByZookeeper 获取DLockByZookeeper实例.

func (*DLockByZookeeper) TryLock

func (dlz *DLockByZookeeper) TryLock(timeoutInSecs int) bool

TryLock 尝试获取分布式锁, 超时后就放弃 (不可重入锁).

==> acquire lock n = create("/distlock/fast-lock/request-", "", ephemeral|sequence) RETRY:

children = getChildren("/distlock/fast-lock", watch=False)
if n is lowest znode in children:
    return
else:
    exist("/distlock/fast-lock/request-" % (n - 1), watch=True)

watch_event:

goto RETRY

func (*DLockByZookeeper) Unlock

func (dlz *DLockByZookeeper) Unlock()

Unlock 释放分布式锁.

==> release lock (voluntarily or session timeout) delete("/distlock/fast-lock/request-" % n)

type RedisConnPool

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

func EstablishRedisConn

func EstablishRedisConn(cfg *RedisServiceConfig) *RedisConnPool

EstablishRedisConn 建立连接redis服务的TCP连接池.

func (*RedisConnPool) ExecCommand

func (p *RedisConnPool) ExecCommand(cmd string, args ...interface{}) (interface{}, error)

ExecCommand 执行redis命令, 完成后自动归还连接.

func (*RedisConnPool) ExecLuaScript

func (p *RedisConnPool) ExecLuaScript(src string, keyCount int, keysAndArgs ...interface{}) (interface{}, error)

ExecLuaScript 执行lua脚本, 完成后自动归还连接.

type RedisServiceConfig

type RedisServiceConfig struct {
	SentinelEndpoints       []string `json:"sentinel_endpoints"`
	SentinelMasterName      string   `json:"sentinel_master_name"`
	SentinelPassword        string   `json:"sentinel_password"`
	RedisDatabase           int      `json:"redis_database"`
	RedisMasterPassword     string   `json:"redis_master_password"`
	RedisPoolMaxIdleConns   int      `json:"redis_pool_max_idle_conns"`   // 连接池最大空闲连接数
	RedisPoolMaxActiveConns int      `json:"redis_pool_max_active_conns"` // 连接池最大激活连接数
	RedisConnectTimeoutMsec int      `json:"redis_connect_timeout_msec"`  // 连接超时
	RedisReadTimeoutMsec    int      `json:"redis_read_timeout_msec"`     // 读取超时
	RedisWriteTimeoutMsec   int      `json:"redis_write_timeout_msec"`    // 写入超时
}

Jump to

Keyboard shortcuts

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