All Projects → everfore → rddlock

everfore / rddlock

Licence: Apache-2.0 license
redis distribute lock

Programming Languages

go
31211 projects - #10 most used programming language
shell
77523 projects

Projects that are alternatives of or similar to rddlock

sherlock-distributed-lock
Distributed locking library for JVM
Stars: ✭ 17 (+41.67%)
Mutual labels:  distributed-lock
distlock
The universal component of distributed locks in golang , support redis and postgresql
Stars: ✭ 60 (+400%)
Mutual labels:  distributed-lock
distributed-dev-learning
汇总、整理常用的分布式开发技术,给出demo,方便学习。包括数据分片、共识算法、一致性hash、分布式事务、非侵入的分布式链路追踪实现原理等内容。
Stars: ✭ 39 (+225%)
Mutual labels:  distributed-lock
consul-cluster-manager
Consul - based cluster manager that can be plugged into Vert.x ecosystem.
Stars: ✭ 17 (+41.67%)
Mutual labels:  distributed-lock
java-sdk
一些常用的java sdk和工具类(日期工具类,分布式锁,redis缓存,二叉树,反射工具类,线程池,对称/非对称/分段加解密,json序列化,http工具,雪花算法,字符串相似度,集合操作工具,xml解析,重试Retry工具类,Jvm监控等)
Stars: ✭ 26 (+116.67%)
Mutual labels:  distributed-lock
spindle
A distributed locking library built on top of Cloud Spanner and TrueTime.
Stars: ✭ 47 (+291.67%)
Mutual labels:  distributed-lock
mutexsafe
MutexSafe will help you use mutex more effectively. Different mutex for different components are presented. In addition, you can add your own lockers and use within the library.
Stars: ✭ 15 (+25%)
Mutual labels:  redis-lock
juice
Java后端开发库,涵盖:常用工具类、SPI扩展、分布式锁、限流、分布式链路追踪等。
Stars: ✭ 32 (+166.67%)
Mutual labels:  distributed-lock
Dramatiq
A fast and reliable background task processing library for Python 3.
Stars: ✭ 2,844 (+23600%)
Mutual labels:  distributed-lock
Atomix
A reactive Java framework for building fault-tolerant distributed systems
Stars: ✭ 2,182 (+18083.33%)
Mutual labels:  distributed-lock
ring-election
A node js library with a distributed leader/follower algorithm ready to be used
Stars: ✭ 92 (+666.67%)
Mutual labels:  distributed-lock
spring-boot-starter-lock
spring 分布式锁
Stars: ✭ 12 (+0%)
Mutual labels:  distributed-lock
slow-down
A centralized Redis-based lock to help you wait on throttled resources
Stars: ✭ 21 (+75%)
Mutual labels:  redis-lock

rddlock

redis distributed lock

redis 分布式锁实现, 原理:redis 分布式锁实现

lock的超时时间:timeout_ms

Usage

Lock & UnLock

lockkey := "lock-key"
timeout_ms := 3000

locked, ex := rddlock.Lock(rds, lockkey, timeout_ms)
defer reelock.UnLock(rds, lockkey, ex)

LockRetry

重试retry_times次,尝试获得锁

retry_times := 10
locked, ex := reelock.LockRetry(rds, lockkey, timeout_ms, retry_times) // get lock by retry
defer reelock.UnLock(rds, lockkey, ex)

UnLockUnsafe

locked, _ := rddlock.Lock(rds, lockkey, timeout_ms)
defer reelock.UnLockUnsafe(rds, lockkey)

UnLockSafe

预留安全时间,释放锁。

locked, _ := rddlock.Lock(rds, lockkey, timeout_ms)
safeDelTime_ms := 40
defer reelock.UnLockSafe(rds, lockkey, safeDelTime_ms)

SyncDo

err := SyncDo(rds, lockkey, timeout_ms, func(timeout chan bool) chan bool {
		ret := make(chan bool, 1)
		go func() {
			fmt.Println("doing...")
			// TODO SOMETHING
			select {
			case <-timeout:
				// do the rollback
				break
			case ret <- true:
				fmt.Println("success end.")
			}
		}()
		return ret
	})

Example

package main

import (
	"fmt"

	"github.com/everfore/rddlock"
	"gopkg.in/ezbuy/redis-orm.v1/orm"
	redis "gopkg.in/redis.v5"
)

func main() {
	// rds, err := orm.NewRedisClient("localhost", 32768, "", 0)
	rds, err := orm.NewRedisClusterClient(&redis.ClusterOptions{
		Addrs: []string{"XXX", "XXX", "XXX"},
	})
	if err != nil {
		panic(err)
	}

	lock_key := "lock-key"

	locked, ex := rddlock.Lock(rds, lock_key, 5)
	if locked {
		fmt.Printf("LOCK %s: %+v\n", lock_key, locked)
		unlocked := rddlock.UnLock(rds, lock_key, ex)
		if unlocked {
			fmt.Printf("UNLOCK %s: %+v\n", lock_key, unlocked)
		} else {
			unlocked = rddlock.UnLockUnsafe(rds, lock_key)
			fmt.Printf("UNLOCK-UNSAFE %s: %+v\n", lock_key, unlocked)
		}
	}

	// retry lock

	// 1. lock the key first
	locked, _ = rddlock.Lock(rds, lock_key, 5)
	fmt.Printf("FIRST step, LOCK %s:%+v\n", lock_key, locked)
	// 2. retry to lock the locked key
	locked, _ = rddlock.LockRetry(rds, lock_key, 100, 100)
	fmt.Printf("SECOND step, LOCK-RETRY %s:%+v\n", lock_key, locked)
}

// Output
// LOCK lock-key: true
// UNLOCK lock-key: true
// FIRST step, LOCK lock-key:true
// SECOND step, LOCK-RETRY lock-key:true

test

success:200, avg:1.1074123 ms
failed:0, avg:NaN ms
--- PASS: TestLockTime (10.59s)

#local-redis
=== RUN   TestLockRetryTime
success:200, avg:1.1741205 ms
failed:0, avg:NaN ms
--- PASS: TestLockRetryTime (10.58s)

#uat-redis
=== RUN   TestLockRetryTime
success:200, avg:12.572702 ms
failed:0, avg:NaN ms
--- PASS: TestLockRetryTime (10.59s)
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].