-
Notifications
You must be signed in to change notification settings - Fork 1
/
redislock.go
270 lines (218 loc) · 6.38 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
package redislock
import (
"context"
"crypto/rand"
"encoding/base64"
"errors"
"io"
"strconv"
"sync"
"time"
)
//lua scripts which should be loaded to redis client when implementing RedisClient interface
const (
LuaRefreshScript = `if redis.call("get", KEYS[1]) == ARGV[1] then return redis.call("pexpire", KEYS[1], ARGV[2]) else return 0 end`
LuaReleaseScript = `if redis.call("get", KEYS[1]) == ARGV[1] then return redis.call("del", KEYS[1]) else return 0 end`
LuaPTTLScript = `if redis.call("get", KEYS[1]) == ARGV[1] then return redis.call("pttl", KEYS[1]) else return -3 end`
)
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")
)
//Implement the interface with which every redis client you wish to use
type RedisClient interface {
SetNX(key, value string, ttl time.Duration) (bool, error)
Refresh(key, value string, ttl string) error
Release(key, value string) error
TTL(key, value string) (int64, error)
}
type Client struct {
redisClient RedisClient
tmp []byte
tmpMu sync.Mutex
}
// // New creates a new Client instance with a custom namespace.
func New(redisClient RedisClient) *Client {
return &Client{redisClient: redisClient}
}
// Obtain tries to obtain a new lock using a key with the given TTL.
// May return ErrNotObtained if not successful.
func (c *Client) Obtain(key string, ttl time.Duration, opt *Options) (*Lock, error) {
// Create a random token
token, err := c.randomToken()
if err != nil {
return nil, err
}
value := token + opt.getMetadata()
ctx := opt.getContext()
retry := opt.getRetryStrategy()
var timer *time.Timer
for deadline := time.Now().Add(ttl); time.Now().Before(deadline); {
ok, err := c.obtain(key, value, ttl)
if err != nil {
return nil, err
} else if ok {
return &Lock{client: c, key: key, value: value}, nil
}
backoff := retry.NextBackoff()
if backoff < 1 {
break
}
if timer == nil {
timer = time.NewTimer(backoff)
defer timer.Stop()
} else {
timer.Reset(backoff)
}
select {
case <-ctx.Done():
return nil, ctx.Err()
case <-timer.C:
}
}
return nil, ErrNotObtained
}
func (c *Client) obtain(key, value string, ttl time.Duration) (bool, error) {
return c.redisClient.SetNX(key, value, ttl)
}
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
}
// --------------------------------------------------------------------
type Lock struct {
client *Client
key string
value string
}
// Obtain is a short-cut for New(...).Obtain(...).
func Obtain(redisClient RedisClient, key string, ttl time.Duration, opt *Options) (*Lock, error) {
return New(redisClient).Obtain(key, ttl, opt)
}
// Key returns the redis key used by the lock.
func (l *Lock) Key() string {
return l.key
}
// Token returns the token value set by the lock.
func (l *Lock) Token() string {
return l.value[:22]
}
// Metadata returns the metadata of the lock.
func (l *Lock) Metadata() string {
return l.value[22:]
}
func (l *Lock) TTL() (time.Duration, error) {
res, err := l.client.redisClient.TTL(l.key, l.value)
if err != nil {
return 0, err
}
if res > 0 {
//expire for key is stored in milliseconds so convert
return time.Duration(res) * 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(ttl time.Duration, opt *Options) error {
return l.client.redisClient.Refresh(l.key, l.value, strconv.FormatInt(int64(ttl/time.Millisecond), 10))
}
// Release manually releases the lock.
// May return ErrLockNotHeld.
func (l *Lock) Release() error {
return l.client.redisClient.Release(l.key, l.value)
}
// --------------------------------------------------------------------
// 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 is appended to the lock token.
Metadata string
// Optional context for Obtain timeout and cancellation control.
Context context.Context
}
func (o *Options) getMetadata() string {
if o != nil {
return o.Metadata
}
return ""
}
func (o *Options) getContext() context.Context {
if o != nil && o.Context != nil {
return o.Context
}
return context.Background()
}
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, max int
}
// LimitRetry limits the number of retries to max attempts.
func LimitRetry(s RetryStrategy, max int) RetryStrategy {
return &limitedRetry{s: s, max: max}
}
func (r *limitedRetry) NextBackoff() time.Duration {
if r.cnt >= r.max {
return 0
}
r.cnt++
return r.s.NextBackoff()
}
type exponentialBackoff struct {
cnt uint
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 {
r.cnt++
ms := 2 << 25
if r.cnt < 25 {
ms = 2 << r.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
}
}