-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsimplelock.go
53 lines (43 loc) · 1.41 KB
/
simplelock.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package simplelock
import (
"time"
"github.com/go-redis/redis"
)
type LockManager struct {
redisClient *redis.Client
}
func New(r *redis.Client) *LockManager {
return &LockManager{redisClient: r}
}
//lockDuration = the total effective period for the lock
//waitTime = if the lock is already hold by someone, the period of time that current thread should wait for
func (m *LockManager) GetLock(name string, lockDuration, maxWaitTime time.Duration) (isSuccessful bool, token string) {
temp, _ := time.Now().UTC().MarshalText()
token = string(temp)
parts := []float64{0.25, 0.2, 0.15, 0.1, 0.1, 0.1, 0.05, 0.05, 0} //the last one should always be zero
for _, p := range parts {
ok, err := m.redisClient.SetNX(name, token, lockDuration).Result()
if err != nil {
panic(err)
}
if ok {
return true, token
} else {
if t := time.Duration(p * float64(maxWaitTime)); t >= 0 {
time.Sleep(t)
} else {
//to allow quit exit if maxWaitTime = 0
break
}
}
}
return false, ``
}
func (m *LockManager) ReleaseLock(name, token string) {
//the lua script to perform atomic compare value and then delete
//aim: check for the token, to avoid deleting the lock owned by others after one's lock expired
script := `if redis.call('GET', KEYS[1]) == KEYS[2] then return redis.call('DEL', KEYS[1]) end return 0`
if err := m.redisClient.Eval(script, []string{name, token}).Err(); err != nil {
panic(err)
}
}