dlock

package module
v0.0.0-...-fb4733d Latest Latest
Warning

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

Go to latest
Published: Mar 6, 2023 License: MIT Imports: 17 Imported by: 0

README

dlock

Usage

dlock is used to coordinate the activities of multiple threads or processes or services, by controlling access to a shared resource by locking and unlocking the resource, which provides synchronization semantic for distributed application.

Contributing

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

Support

License

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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CloseZKConn

func CloseZKConn(conn *zk.Conn)

CloseZKConn 关闭TCP连接.

func EstablishZKConn

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

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

func SkipAutoTest

func SkipAutoTest(t *testing.T)

Types

type DlockByRedis

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

DlockByRedis 通过redis实现的分布式锁服务

func NewDlockByRedis

func NewDlockByRedis(rdb RedisConnInterface) *DlockByRedis

NewDlockByRedis 获取DlockByRedis实例.

func (*DlockByRedis) TryLock

func (dlr *DlockByRedis) TryLock(pid string, expire, timeout int64) (token string, acquired bool)

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

func (*DlockByRedis) Unlock

func (dlr *DlockByRedis) Unlock(pid, token 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(pid string, timeout int64) (token string, acquired bool)

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

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

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

watch_event:

goto RETRY

func (*DlockByZookeeper) Unlock

func (dlz *DlockByZookeeper) Unlock(pid, token string)

Unlock 释放分布式锁.

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

type RedisConnInterface

type RedisConnInterface interface {
	Close()

	ExecCmd(cmd string, args ...interface{}) (interface{}, error)
	ExecLuaScript(src string, keyCount int, keysAndArgs ...interface{}) (interface{}, error)
}

type RedisConnPool

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

func EstablishRedisConn

func EstablishRedisConn(cfg *RedisConnPoolConfig) *RedisConnPool

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

func (*RedisConnPool) Close

func (p *RedisConnPool) Close()

CloseRedisConn 释放TCP连接池.

func (*RedisConnPool) ExecCmd

func (p *RedisConnPool) ExecCmd(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 RedisConnPoolConfig

type RedisConnPoolConfig struct {
	RedisEndpoint           string `json:"redis_endpoint"`
	RedisDatabase           int    `json:"redis_database"`
	RedisPassword           string `json:"redis_password"`
	RedisConnectTimeout     int    `json:"redis_connect_timeout_msec"`  // 连接超时
	RedisReadTimeout        int    `json:"redis_read_timeout_msec"`     // 读取超时
	RedisWriteTimeout       int    `json:"redis_write_timeout_msec"`    // 写入超时
	RedisPoolMaxIdleConns   int    `json:"redis_pool_max_idle_conns"`   // 连接池最大空闲连接数
	RedisPoolMaxActiveConns int    `json:"redis_pool_max_active_conns"` // 连接池最大激活连接数
	RedisOpenTLS            bool   `json:"redis_open_tls"`
}

type RedisHAConnPool

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

func EstablishRedisHAConnPool

func EstablishRedisHAConnPool(cfg *RedisHAConnPoolConfig) *RedisHAConnPool

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

func (*RedisHAConnPool) Close

func (p *RedisHAConnPool) Close()

CloseRedisConn 释放TCP连接池.

func (*RedisHAConnPool) ExecCmd

func (p *RedisHAConnPool) ExecCmd(cmd string, args ...interface{}) (interface{}, error)

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

func (*RedisHAConnPool) ExecLuaScript

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

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

type RedisHAConnPoolConfig

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

Jump to

Keyboard shortcuts

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