forked from ulule/limiter
/
store.go
264 lines (215 loc) · 6.67 KB
/
store.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
package redis
import (
"context"
"fmt"
"time"
libredis "github.com/go-redis/redis"
"github.com/pkg/errors"
"github.com/ulule/limiter"
"github.com/ulule/limiter/drivers/store/common"
)
// Client is an interface thats allows to use a redis cluster or a redis single client seamlessly.
type Client interface {
Ping() *libredis.StatusCmd
Get(key string) *libredis.StringCmd
Set(key string, value interface{}, expiration time.Duration) *libredis.StatusCmd
Watch(handler func(*libredis.Tx) error, keys ...string) error
Del(keys ...string) *libredis.IntCmd
SetNX(key string, value interface{}, expiration time.Duration) *libredis.BoolCmd
Eval(script string, keys []string, args ...interface{}) *libredis.Cmd
}
// Store is the redis store.
type Store struct {
// Prefix used for the key.
Prefix string
// MaxRetry is the maximum number of retry under race conditions.
MaxRetry int
// client used to communicate with redis server.
client Client
}
// NewStore returns an instance of redis store with defaults.
func NewStore(client Client) (limiter.Store, error) {
return NewStoreWithOptions(client, limiter.StoreOptions{
Prefix: limiter.DefaultPrefix,
CleanUpInterval: limiter.DefaultCleanUpInterval,
MaxRetry: limiter.DefaultMaxRetry,
})
}
// NewStoreWithOptions returns an instance of redis store with options.
func NewStoreWithOptions(client Client, options limiter.StoreOptions) (limiter.Store, error) {
store := &Store{
client: client,
Prefix: options.Prefix,
MaxRetry: options.MaxRetry,
}
if store.MaxRetry <= 0 {
store.MaxRetry = 1
}
_, err := store.ping()
if err != nil {
return nil, err
}
return store, nil
}
// Get returns the limit for given identifier.
func (store *Store) Get(ctx context.Context, key string, rate limiter.Rate) (limiter.Context, error) {
key = fmt.Sprintf("%s:%s", store.Prefix, key)
now := time.Now()
lctx := limiter.Context{}
onWatch := func(rtx *libredis.Tx) error {
created, err := store.doSetValue(rtx, key, rate.Period)
if err != nil {
return err
}
if created {
expiration := now.Add(rate.Period)
lctx = common.GetContextFromState(now, rate, expiration, 1)
return nil
}
count, ttl, err := store.doUpdateValue(rtx, key, rate.Period)
if err != nil {
return err
}
expiration := now.Add(rate.Period)
if ttl > 0 {
expiration = now.Add(ttl)
}
lctx = common.GetContextFromState(now, rate, expiration, count)
return nil
}
err := store.client.Watch(onWatch, key)
if err != nil {
err = errors.Wrapf(err, "limiter: cannot get value for %s", key)
return limiter.Context{}, err
}
return lctx, nil
}
// Peek returns the limit for given identifier, without modification on current values.
func (store *Store) Peek(ctx context.Context, key string, rate limiter.Rate) (limiter.Context, error) {
key = fmt.Sprintf("%s:%s", store.Prefix, key)
now := time.Now()
lctx := limiter.Context{}
onWatch := func(rtx *libredis.Tx) error {
count, ttl, err := store.doPeekValue(rtx, key)
if err != nil {
return err
}
expiration := now.Add(rate.Period)
if ttl > 0 {
expiration = now.Add(ttl)
}
lctx = common.GetContextFromState(now, rate, expiration, count)
return nil
}
err := store.client.Watch(onWatch, key)
if err != nil {
err = errors.Wrapf(err, "limiter: cannot peek value for %s", key)
return limiter.Context{}, err
}
return lctx, nil
}
// doPeekValue will execute peekValue with a retry mecanism (optimistic locking) until store.MaxRetry is reached.
func (store *Store) doPeekValue(rtx *libredis.Tx, key string) (int64, time.Duration, error) {
for i := 0; i < store.MaxRetry; i++ {
count, ttl, err := peekValue(rtx, key)
if err == nil {
return count, ttl, nil
}
}
return 0, 0, errors.New("retry limit exceeded")
}
// peekValue will retrieve the counter and its expiration for given key.
func peekValue(rtx *libredis.Tx, key string) (int64, time.Duration, error) {
pipe := rtx.Pipeline()
value := pipe.Get(key)
expire := pipe.PTTL(key)
_, err := pipe.Exec()
if err != nil && err != libredis.Nil {
return 0, 0, err
}
count, err := value.Int64()
if err != nil && err != libredis.Nil {
return 0, 0, err
}
ttl, err := expire.Result()
if err != nil {
return 0, 0, err
}
return count, ttl, nil
}
// doSetValue will execute setValue with a retry mecanism (optimistic locking) until store.MaxRetry is reached.
func (store *Store) doSetValue(rtx *libredis.Tx, key string, expiration time.Duration) (bool, error) {
for i := 0; i < store.MaxRetry; i++ {
created, err := setValue(rtx, key, expiration)
if err == nil {
return created, nil
}
}
return false, errors.New("retry limit exceeded")
}
// setValue will try to initialize a new counter if given key doesn't exists.
func setValue(rtx *libredis.Tx, key string, expiration time.Duration) (bool, error) {
value := rtx.SetNX(key, 1, expiration)
created, err := value.Result()
if err != nil {
return false, err
}
return created, nil
}
// doUpdateValue will execute setValue with a retry mecanism (optimistic locking) until store.MaxRetry is reached.
func (store *Store) doUpdateValue(rtx *libredis.Tx, key string,
expiration time.Duration) (int64, time.Duration, error) {
for i := 0; i < store.MaxRetry; i++ {
count, ttl, err := updateValue(rtx, key, expiration)
if err == nil {
return count, ttl, nil
}
// If ttl is negative and there is an error, do not retry an update.
if ttl < 0 {
return 0, 0, err
}
}
return 0, 0, errors.New("retry limit exceeded")
}
// updateValue will try to increment the counter identified by given key.
func updateValue(rtx *libredis.Tx, key string, expiration time.Duration) (int64, time.Duration, error) {
pipe := rtx.Pipeline()
value := pipe.Incr(key)
expire := pipe.PTTL(key)
_, err := pipe.Exec()
if err != nil {
return 0, 0, err
}
count, err := value.Result()
if err != nil {
return 0, 0, err
}
ttl, err := expire.Result()
if err != nil {
return 0, 0, err
}
// If ttl is -1ms, we have to define key expiration.
// PTTL return values changed as of Redis 2.8
// Now the command returns -2ms if the key does not exist, and -1ms if the key exists, but there is no expiry set
// We shouldn't try to set an expiry on a key that doesn't exist
if ttl == (-1 * time.Millisecond) {
expire := rtx.Expire(key, expiration)
ok, err := expire.Result()
if err != nil {
return count, ttl, err
}
if !ok {
return count, ttl, errors.New("cannot configure timeout on key")
}
}
return count, ttl, nil
}
// ping checks if redis is alive.
func (store *Store) ping() (bool, error) {
cmd := store.client.Ping()
pong, err := cmd.Result()
if err != nil {
return false, errors.Wrap(err, "limiter: cannot ping redis server")
}
return (pong == "PONG"), nil
}