forked from go-redsync/redsync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmutex.go
142 lines (116 loc) · 3.02 KB
/
mutex.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package redsync
import (
"crypto/rand"
"encoding/base64"
"sync"
"time"
"github.com/garyburd/redigo/redis"
)
// A Mutex is a distributed mutual exclusion lock.
type Mutex struct {
name string
expiry time.Duration
tries int
delay time.Duration
factor float64
quorum int
value string
until time.Time
nodem sync.Mutex
connWrappers []RedisConnWrapper
}
// Lock locks m. In case it returns an error on failure, you may retry to acquire the lock by calling this method again.
func (m *Mutex) Lock() error {
m.nodem.Lock()
defer m.nodem.Unlock()
value, err := m.genValue()
if err != nil {
return err
}
for i := 0; i < m.tries; i++ {
if i != 0 {
time.Sleep(m.delay)
}
start := time.Now()
n := 0
for _, connWrapper := range m.connWrappers {
ok := m.acquire(connWrapper, value)
if ok {
n++
}
}
until := time.Now().Add(m.expiry - time.Now().Sub(start) - time.Duration(int64(float64(m.expiry)*m.factor)) + 2*time.Millisecond)
if n >= m.quorum && time.Now().Before(until) {
m.value = value
m.until = until
return nil
}
for _, connWrapper := range m.connWrappers {
m.release(connWrapper, value)
}
}
return ErrFailed
}
// Unlock unlocks m and returns the status of unlock. It is a run-time error if m is not locked on entry to Unlock.
func (m *Mutex) Unlock() bool {
m.nodem.Lock()
defer m.nodem.Unlock()
n := 0
for _, connWrapper := range m.connWrappers {
ok := m.release(connWrapper, m.value)
if ok {
n++
}
}
return n >= m.quorum
}
// Extend resets the mutex's expiry and returns the status of expiry extension. It is a run-time error if m is not locked on entry to Extend.
func (m *Mutex) Extend() bool {
m.nodem.Lock()
defer m.nodem.Unlock()
n := 0
for _, connWrapper := range m.connWrappers {
ok := m.touch(connWrapper, m.value, int(m.expiry/time.Millisecond))
if ok {
n++
}
}
return n >= m.quorum
}
func (m *Mutex) genValue() (string, error) {
b := make([]byte, 32)
_, err := rand.Read(b)
if err != nil {
return "", err
}
return base64.StdEncoding.EncodeToString(b), nil
}
func (m *Mutex) acquire(connWrapper RedisConnWrapper, value string) bool {
conn := connWrapper.Get()
reply, err := redis.String(conn.Do("SET", m.name, value, "NX", "PX", int(m.expiry/time.Millisecond)))
return err == nil && reply == "OK"
}
var deleteScript = redis.NewScript(1, `
if redis.call("GET", KEYS[1]) == ARGV[1] then
return redis.call("DEL", KEYS[1])
else
return 0
end
`)
func (m *Mutex) release(connWrapper RedisConnWrapper, value string) bool {
conn := connWrapper.Get()
status, err := deleteScript.Do(conn, m.name, value)
return err == nil && status != 0
}
var touchScript = redis.NewScript(1, `
if redis.call("GET", KEYS[1]) == ARGV[1] then
return redis.call("SET", KEYS[1], ARGV[1], "XX", "PX", ARGV[2])
else
return "ERR"
end
`)
func (m *Mutex) touch(connWrapper RedisConnWrapper, value string, expiry int) bool {
conn := connWrapper.Get()
status, err := redis.String(touchScript.Do(conn, m.name, value, expiry))
return err == nil && status != "ERR"
}