https://github.com/everfore/rddlock
redis distribute lock
https://github.com/everfore/rddlock
distributed-lock redis-distributed-lock redis-lock
Last synced: 5 months ago
JSON representation
redis distribute lock
- Host: GitHub
- URL: https://github.com/everfore/rddlock
- Owner: everfore
- License: apache-2.0
- Created: 2017-08-24T09:58:14.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2017-08-29T01:34:04.000Z (almost 9 years ago)
- Last Synced: 2024-06-20T11:45:20.132Z (about 2 years ago)
- Topics: distributed-lock, redis-distributed-lock, redis-lock
- Language: Go
- Size: 24.4 KB
- Stars: 12
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# rddlock
redis distributed lock
redis 分布式锁实现, 原理:_[redis 分布式锁实现](https://github.com/toukii/mdblog/blob/master/Java/redis-lock.md)_
lock的超时时间:timeout_ms
## Usage
__Lock & UnLock__
```golang
lockkey := "lock-key"
timeout_ms := 3000
locked, ex := rddlock.Lock(rds, lockkey, timeout_ms)
defer reelock.UnLock(rds, lockkey, ex)
```
__LockRetry__
重试retry_times次,尝试获得锁
```golang
retry_times := 10
locked, ex := reelock.LockRetry(rds, lockkey, timeout_ms, retry_times) // get lock by retry
defer reelock.UnLock(rds, lockkey, ex)
```
__UnLockUnsafe__
```golang
locked, _ := rddlock.Lock(rds, lockkey, timeout_ms)
defer reelock.UnLockUnsafe(rds, lockkey)
```
__UnLockSafe__
预留安全时间,释放锁。
```golang
locked, _ := rddlock.Lock(rds, lockkey, timeout_ms)
safeDelTime_ms := 40
defer reelock.UnLockSafe(rds, lockkey, safeDelTime_ms)
```
__SyncDo__
```golang
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
```golang
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)
```