-
Notifications
You must be signed in to change notification settings - Fork 159
/
Copy pathredislock.go
347 lines (290 loc) · 8.23 KB
/
redislock.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
package redislock
import (
"context"
"crypto/rand"
_ "embed"
"encoding/base64"
"errors"
"io"
"strconv"
"sync"
"sync/atomic"
"time"
"github.com/redis/go-redis/v9"
)
//go:embed release.lua
var luaReleaseScript string
//go:embed refresh.lua
var luaRefreshScript string
//go:embed pttl.lua
var luaPTTLScript string
//go:embed obtain.lua
var luaObtainScript string
var (
luaRefresh = redis.NewScript(luaRefreshScript)
luaRelease = redis.NewScript(luaReleaseScript)
luaPTTL = redis.NewScript(luaPTTLScript)
luaObtain = redis.NewScript(luaObtainScript)
)
var (
// ErrNotObtained is returned when a lock cannot be obtained.
ErrNotObtained = errors.New("redislock: not obtained")
// ErrLockNotHeld is returned when trying to release an inactive lock.
ErrLockNotHeld = errors.New("redislock: lock not held")
)
// RedisClient is a minimal client interface.
type RedisClient interface {
redis.Scripter
}
// Client wraps a redis client.
type Client struct {
client RedisClient
tmp []byte
tmpMu sync.Mutex
}
// New creates a new Client instance with a custom namespace.
func New(client RedisClient) *Client {
return &Client{client: client}
}
// Obtain tries to obtain a new lock using a key with the given TTL.
// May return ErrNotObtained if not successful.
func (c *Client) Obtain(ctx context.Context, key string, ttl time.Duration, opt *Options) (*Lock, error) {
return c.ObtainMulti(ctx, []string{key}, ttl, opt)
}
// ObtainMulti tries to obtain new locks using keys with the given TTL.
// If any of requested key are already locked, no additional keys are
// locked and ErrNotObtained is returned.
// May return ErrNotObtained if not successful.
func (c *Client) ObtainMulti(ctx context.Context, keys []string, ttl time.Duration, opt *Options) (*Lock, error) {
token := opt.getToken()
// Create a random token
if token == "" {
var err error
if token, err = c.randomToken(); err != nil {
return nil, err
}
}
value := token + opt.getMetadata()
ttlVal := strconv.FormatInt(int64(ttl/time.Millisecond), 10)
retry := opt.getRetryStrategy()
// make sure we don't retry forever
if _, ok := ctx.Deadline(); !ok {
var cancel context.CancelFunc
ctx, cancel = context.WithDeadline(ctx, time.Now().Add(ttl))
defer cancel()
}
var ticker *time.Ticker
for {
ok, err := c.obtain(ctx, keys, value, len(token), ttlVal)
if err != nil {
return nil, err
} else if ok {
return &Lock{Client: c, keys: keys, value: value, tokenLen: len(token)}, nil
}
backoff := retry.NextBackoff()
if backoff < 1 {
return nil, ErrNotObtained
}
if ticker == nil {
ticker = time.NewTicker(backoff)
defer ticker.Stop()
} else {
ticker.Reset(backoff)
}
select {
case <-ctx.Done():
return nil, ctx.Err()
case <-ticker.C:
}
}
}
func (c *Client) obtain(ctx context.Context, keys []string, value string, tokenLen int, ttlVal string) (bool, error) {
_, err := luaObtain.Run(ctx, c.client, keys, value, tokenLen, ttlVal).Result()
if err != nil {
if errors.Is(err, redis.Nil) {
return false, nil
}
return false, err
}
return true, nil
}
func (c *Client) randomToken() (string, error) {
c.tmpMu.Lock()
defer c.tmpMu.Unlock()
if len(c.tmp) == 0 {
c.tmp = make([]byte, 16)
}
if _, err := io.ReadFull(rand.Reader, c.tmp); err != nil {
return "", err
}
return base64.RawURLEncoding.EncodeToString(c.tmp), nil
}
// --------------------------------------------------------------------
// Lock represents an obtained, distributed lock.
type Lock struct {
*Client
keys []string
value string
tokenLen int
}
// Obtain is a short-cut for New(...).Obtain(...).
func Obtain(ctx context.Context, client RedisClient, key string, ttl time.Duration, opt *Options) (*Lock, error) {
return New(client).Obtain(ctx, key, ttl, opt)
}
// ObtainMulti is a short-cut for New(...).ObtainMulti(...).
func ObtainMulti(ctx context.Context, client RedisClient, keys []string, ttl time.Duration, opt *Options) (*Lock, error) {
return New(client).ObtainMulti(ctx, keys, ttl, opt)
}
// Key returns the redis key used by the lock.
// If the lock hold multiple key, only the first is returned.
func (l *Lock) Key() string {
return l.keys[0]
}
// Keys returns the redis keys used by the lock.
func (l *Lock) Keys() []string {
return l.keys
}
// Token returns the token value set by the lock.
func (l *Lock) Token() string {
return l.value[:l.tokenLen]
}
// Metadata returns the metadata of the lock.
func (l *Lock) Metadata() string {
return l.value[l.tokenLen:]
}
// TTL returns the remaining time-to-live. Returns 0 if the lock has expired.
// In case lock is holding multiple keys, TTL returns the min ttl among those keys.
func (l *Lock) TTL(ctx context.Context) (time.Duration, error) {
if l == nil {
return 0, ErrLockNotHeld
}
res, err := luaPTTL.Run(ctx, l.client, l.keys, l.value).Result()
if err != nil {
if errors.Is(err, redis.Nil) {
return 0, nil
}
return 0, err
}
if num := res.(int64); num > 0 {
return time.Duration(num) * time.Millisecond, nil
}
return 0, nil
}
// Refresh extends the lock with a new TTL.
// May return ErrNotObtained if refresh is unsuccessful.
func (l *Lock) Refresh(ctx context.Context, ttl time.Duration, opt *Options) error {
if l == nil {
return ErrNotObtained
}
ttlVal := strconv.FormatInt(int64(ttl/time.Millisecond), 10)
_, err := luaRefresh.Run(ctx, l.client, l.keys, l.value, ttlVal).Result()
if err != nil {
if errors.Is(err, redis.Nil) {
return ErrNotObtained
}
return err
}
return nil
}
// Release manually releases the lock.
// May return ErrLockNotHeld.
func (l *Lock) Release(ctx context.Context) error {
if l == nil {
return ErrLockNotHeld
}
_, err := luaRelease.Run(ctx, l.client, l.keys, l.value).Result()
if err != nil {
if errors.Is(err, redis.Nil) {
return ErrLockNotHeld
}
return err
}
return nil
}
// --------------------------------------------------------------------
// Options describe the options for the lock
type Options struct {
// RetryStrategy allows to customise the lock retry strategy.
// Default: do not retry
RetryStrategy RetryStrategy
// Metadata string.
Metadata string
// Token is a unique value that is used to identify the lock. By default, a random tokens are generated. Use this
// option to provide a custom token instead.
Token string
}
func (o *Options) getMetadata() string {
if o != nil {
return o.Metadata
}
return ""
}
func (o *Options) getToken() string {
if o != nil {
return o.Token
}
return ""
}
func (o *Options) getRetryStrategy() RetryStrategy {
if o != nil && o.RetryStrategy != nil {
return o.RetryStrategy
}
return NoRetry()
}
// --------------------------------------------------------------------
// RetryStrategy allows to customise the lock retry strategy.
type RetryStrategy interface {
// NextBackoff returns the next backoff duration.
NextBackoff() time.Duration
}
type linearBackoff time.Duration
// LinearBackoff allows retries regularly with customized intervals
func LinearBackoff(backoff time.Duration) RetryStrategy {
return linearBackoff(backoff)
}
// NoRetry acquire the lock only once.
func NoRetry() RetryStrategy {
return linearBackoff(0)
}
func (r linearBackoff) NextBackoff() time.Duration {
return time.Duration(r)
}
type limitedRetry struct {
s RetryStrategy
cnt int64
max int64
}
// LimitRetry limits the number of retries to max attempts.
func LimitRetry(s RetryStrategy, max int) RetryStrategy {
return &limitedRetry{s: s, max: int64(max)}
}
func (r *limitedRetry) NextBackoff() time.Duration {
if atomic.LoadInt64(&r.cnt) >= r.max {
return 0
}
atomic.AddInt64(&r.cnt, 1)
return r.s.NextBackoff()
}
type exponentialBackoff struct {
cnt uint64
min, max time.Duration
}
// ExponentialBackoff strategy is an optimization strategy with a retry time of 2**n milliseconds (n means number of times).
// You can set a minimum and maximum value, the recommended minimum value is not less than 16ms.
func ExponentialBackoff(min, max time.Duration) RetryStrategy {
return &exponentialBackoff{min: min, max: max}
}
func (r *exponentialBackoff) NextBackoff() time.Duration {
cnt := atomic.AddUint64(&r.cnt, 1)
ms := 2 << 25
if cnt < 25 {
ms = 2 << cnt
}
if d := time.Duration(ms) * time.Millisecond; d < r.min {
return r.min
} else if r.max != 0 && d > r.max {
return r.max
} else {
return d
}
}