This repository has been archived by the owner on Jun 24, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathlock.go
217 lines (179 loc) · 4.88 KB
/
lock.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package lock
import (
"context"
"crypto/rand"
"encoding/base64"
"errors"
"strconv"
"sync"
"time"
"github.com/go-redis/redis"
)
var luaRefresh = redis.NewScript(`if redis.call("get", KEYS[1]) == ARGV[1] then return redis.call("pexpire", KEYS[1], ARGV[2]) else return 0 end`)
var luaRelease = redis.NewScript(`if redis.call("get", KEYS[1]) == ARGV[1] then return redis.call("del", KEYS[1]) else return 0 end`)
var emptyCtx = context.Background()
// ErrLockNotObtained may be returned by Obtain() and Run()
// if a lock could not be obtained.
var (
ErrLockUnlockFailed = errors.New("lock unlock failed")
ErrLockNotObtained = errors.New("lock not obtained")
ErrLockDurationExceeded = errors.New("lock duration exceeded")
)
// RedisClient is a minimal client interface.
type RedisClient interface {
SetNX(key string, value interface{}, expiration time.Duration) *redis.BoolCmd
Eval(script string, keys []string, args ...interface{}) *redis.Cmd
EvalSha(sha1 string, keys []string, args ...interface{}) *redis.Cmd
ScriptExists(scripts ...string) *redis.BoolSliceCmd
ScriptLoad(script string) *redis.StringCmd
}
// Locker allows (repeated) distributed locking.
type Locker struct {
client RedisClient
key string
opts Options
token string
mutex sync.Mutex
}
// Run runs a callback handler with a Redis lock. It may return ErrLockNotObtained
// if a lock was not successfully acquired.
func Run(client RedisClient, key string, opts *Options, handler func()) error {
locker, err := Obtain(client, key, opts)
if err != nil {
return err
}
sem := make(chan struct{})
go func() {
handler()
close(sem)
}()
select {
case <-sem:
return locker.Unlock()
case <-time.After(locker.opts.LockTimeout):
return ErrLockDurationExceeded
}
}
// Obtain is a shortcut for New().Lock(). It may return ErrLockNotObtained
// if a lock was not successfully acquired.
func Obtain(client RedisClient, key string, opts *Options) (*Locker, error) {
locker := New(client, key, opts)
if ok, err := locker.Lock(); err != nil {
return nil, err
} else if !ok {
return nil, ErrLockNotObtained
}
return locker, nil
}
// New creates a new distributed locker on a given key.
func New(client RedisClient, key string, opts *Options) *Locker {
var o Options
if opts != nil {
o = *opts
}
o.normalize()
return &Locker{client: client, key: key, opts: o}
}
// IsLocked returns true if a lock is still being held.
func (l *Locker) IsLocked() bool {
l.mutex.Lock()
locked := l.token != ""
l.mutex.Unlock()
return locked
}
// Lock applies the lock, don't forget to defer the Unlock() function to release the lock after usage.
func (l *Locker) Lock() (bool, error) {
return l.LockWithContext(emptyCtx)
}
// LockWithContext is like Lock but allows to pass an additional context which allows cancelling
// lock attempts prematurely.
func (l *Locker) LockWithContext(ctx context.Context) (bool, error) {
l.mutex.Lock()
defer l.mutex.Unlock()
if l.token != "" {
return l.refresh(ctx)
}
return l.create(ctx)
}
// Unlock releases the lock
func (l *Locker) Unlock() error {
l.mutex.Lock()
err := l.release()
l.mutex.Unlock()
return err
}
// Helpers
func (l *Locker) create(ctx context.Context) (bool, error) {
l.reset()
// Create a random token
token, err := randomToken()
if err != nil {
return false, err
}
token = l.opts.TokenPrefix + token
// Calculate the timestamp we are willing to wait for
attempts := l.opts.RetryCount + 1
var retryDelay *time.Timer
for {
// Try to obtain a lock
ok, err := l.obtain(token)
if err != nil {
return false, err
} else if ok {
l.token = token
return true, nil
}
if attempts--; attempts <= 0 {
return false, nil
}
if retryDelay == nil {
retryDelay = time.NewTimer(l.opts.RetryDelay)
defer retryDelay.Stop()
} else {
retryDelay.Reset(l.opts.RetryDelay)
}
select {
case <-ctx.Done():
return false, ctx.Err()
case <-retryDelay.C:
}
}
}
func (l *Locker) refresh(ctx context.Context) (bool, error) {
ttl := strconv.FormatInt(int64(l.opts.LockTimeout/time.Millisecond), 10)
status, err := luaRefresh.Run(l.client, []string{l.key}, l.token, ttl).Result()
if err != nil {
return false, err
} else if status == int64(1) {
return true, nil
}
return l.create(ctx)
}
func (l *Locker) obtain(token string) (bool, error) {
ok, err := l.client.SetNX(l.key, token, l.opts.LockTimeout).Result()
if err == redis.Nil {
err = nil
}
return ok, err
}
func (l *Locker) release() error {
defer l.reset()
res, err := luaRelease.Run(l.client, []string{l.key}, l.token).Result()
if err == redis.Nil {
return ErrLockUnlockFailed
}
if i, ok := res.(int64); !ok || i != 1 {
return ErrLockUnlockFailed
}
return err
}
func (l *Locker) reset() {
l.token = ""
}
func randomToken() (string, error) {
buf := make([]byte, 16)
if _, err := rand.Read(buf); err != nil {
return "", err
}
return base64.URLEncoding.EncodeToString(buf), nil
}