-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlimiter.go
336 lines (282 loc) · 8.1 KB
/
limiter.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
package limiter
import (
"context"
"fmt"
"strconv"
"strings"
"time"
)
const (
formatCurrentLimitKey = "limiter:%s:current_limit"
formatCurrentLimitValue = "%d-%d"
formatTimeWindow = "limiter:%s:window:%d"
)
var (
defaultOpts = []Option{
WithMinLimit(150),
WithMaxLimit(30000),
WithEstimatePeriod(10 * time.Minute),
WithErrorHandler(func(err error) {
fmt.Printf("[err][limiter] %s\n", err.Error())
}),
WithRevaluateLimit(func(currentlyLimit, estimatedTimeWindowSize, estimatedTimeWindowUsedCount int64) int64 {
var newLimit int64
switch {
case float64(currentlyLimit*estimatedTimeWindowSize)*0.9 <= float64(estimatedTimeWindowUsedCount):
newLimit = currentlyLimit * 2
case float64(currentlyLimit*estimatedTimeWindowSize)*0.5 >= float64(estimatedTimeWindowUsedCount):
newLimit = currentlyLimit / 2
default:
newLimit = currentlyLimit
}
return newLimit
}),
}
)
// RevaluateLimit is to revaluate sending limit per 1 minute.
// currentlyLimit is currently applied limit.
// estimatedTimeWindowSize is the size of time-window for the duration of an estimated period.
// estimatedTimeWindowUsedCount is the sum of sending push count of time-window for the duration of an estimated period.
// Its return value is new limit per 1 minute.
type RevaluateLimit func(currentlyLimit, estimatedTimeWindowSize, estimatedTimeWindowUsedCount int64) int64
type RateLimiter interface {
Acquire() (bool, error)
CurrentLimit() (int64, error)
HealthCheck(ctx context.Context) bool
Close() error
}
type limiter struct {
serviceName string
minLimit int64
maxLimit int64
estimatePeriod time.Duration
errorHandler ErrorHandler
ticker *time.Ticker
revaluateLimit RevaluateLimit
cache Cache
}
// IsAcquire returns a value which exceeds limit or not currently.
func (l *limiter) Acquire() (bool, error) {
used, err := l.getCurrentTimeWindow()
if err != nil {
return false, err
}
quota, _, err := l.getCurrentLimit()
if err != nil {
return false, err
}
// if quota is already full.
if used >= quota {
return false, nil
}
// increase count
if err := l.increaseCurrentTimeWindow(); err != nil {
return false, err
}
return true, nil
}
// CurrentLimit returns currently limit.
func (l *limiter) CurrentLimit() (int64, error) {
limit, _, err := l.getCurrentLimit()
if err != nil {
return 0, err
}
return limit, nil
}
// HealthCheck checks to redis condition.
func (l *limiter) HealthCheck(ctx context.Context) bool {
return l.cache.Ping(ctx)
}
// Close closes limiter.
func (l *limiter) Close() error {
l.ticker.Stop()
return l.cache.Close()
}
// getCurrentLimit returns current limit.
func (l *limiter) getCurrentLimit() (int64, time.Time, error) {
result, ok, err := l.cache.Get(l.keyToCurrentLimit())
if err != nil {
return 0, time.Time{}, err
}
// if key to limit doesn't exist, default apply to a minimum limit.
if !ok {
return l.minLimit, time.Now().Add(-1 * time.Minute), nil
}
seps := strings.Split(result.(string), "-")
if len(seps) != 2 {
return 0, time.Time{}, ErrInvalidParams
}
limit, err := strconv.ParseInt(seps[0], 10, 64)
if err != nil {
return 0, time.Time{}, err
}
deadline, err := strconv.ParseInt(seps[1], 10, 64)
if err != nil {
return 0, time.Time{}, err
}
return limit, time.Unix(deadline, 0), nil
}
// setCurrentLimit sets current limit.
func (l *limiter) setCurrentLimit(limit int64) (int64, time.Time, error) {
if limit <= 0 {
return 0, time.Time{}, ErrInvalidParams
}
deadline := time.Now().UTC().Add(l.estimatePeriod)
value := l.valueToCurrentLimit(limit, deadline)
// cache expire time is twice as much as a estimated period.
// it's not critical, but must grater than a estimated period.
expire := l.estimatePeriod * 2
if err := l.cache.Set(l.keyToCurrentLimit(), value, expire); err != nil {
return 0, time.Time{}, err
}
return limit, deadline, nil
}
// getCurrentTimeWindow returns count of current time window.
func (l *limiter) getCurrentTimeWindow() (int64, error) {
result, ok, err := l.cache.Get(l.keyToTimeWindow(time.Now().UTC().Truncate(time.Minute)))
if err != nil {
return 0, err
}
// if key to limit doesn't exist, directly return 0, nil
if !ok {
return 0, nil
}
return interfaceToInt64(result)
}
// increaseTimeWindow is to increase current time window.
func (l *limiter) increaseCurrentTimeWindow() error {
currentWindow := time.Now().UTC().Truncate(time.Minute)
// cache expire time is twice as much as a estimated period.
// it's not critical, but must grater than a estimated period.
key := l.keyToTimeWindow(currentWindow)
expire := l.estimatePeriod * 2
return l.cache.Increment(key, 1, expire)
}
// getEstimateTimeWindow returns total time window count and list size.
func (l *limiter) getEstimateTimeWindow() (int64, int64, error) {
// truncate seconds
endWindow := time.Now().UTC().Add(-time.Minute).Truncate(time.Minute)
startWindow := endWindow.Add(-l.estimatePeriod).Truncate(time.Minute)
// extract time windows
var keys []string
var windowSize int64
for ; startWindow.Unix() <= endWindow.Unix(); startWindow = startWindow.Add(time.Minute) {
keys = append(keys, l.keyToTimeWindow(startWindow))
windowSize += 1
}
// summary time window count
var used int64
result, ok, err := l.cache.MGet(keys...)
if err != nil {
return 0, 0, err
}
// if getting time windows don't exist.
if !ok {
used = 0
} else {
for _, e := range result {
if e != nil {
count, err := interfaceToInt64(e)
if err != nil {
return 0, 0, err
}
used += count
}
}
}
return used, windowSize, nil
}
// estimateCurrentLimit estimates current limit.
func (l *limiter) estimateCurrentLimit() error {
now := time.Now()
quota, deadline, err := l.getCurrentLimit()
if err != nil {
return err
}
// currently not estimate time.
if now.Before(deadline) {
return nil
}
// get estimate time window information.
used, windowSize, err := l.getEstimateTimeWindow()
if err != nil {
return err
}
newQuota := l.revaluateLimit(quota, windowSize, used)
// newQuota must be between min and max limit.
if newQuota < l.minLimit {
newQuota = l.minLimit
} else if newQuota > l.maxLimit {
newQuota = l.maxLimit
}
// set current limit
if _, _, err := l.setCurrentLimit(newQuota); err != nil {
return err
}
return nil
}
// estimateLoop runs loop for estimating threshold.
func (l *limiter) estimateLoop() {
l.ticker = time.NewTicker(time.Minute)
go func() {
for _ = range l.ticker.C {
if err := l.estimateCurrentLimit(); err != nil {
l.errorHandler(err)
}
}
}()
}
// keyToTimeWindow returns time window string
func (l *limiter) keyToTimeWindow(t time.Time) string {
return fmt.Sprintf(formatTimeWindow, l.serviceName, t.Unix())
}
// keyToCurrentLimit returns current limit key.
func (l *limiter) keyToCurrentLimit() string {
return fmt.Sprintf(formatCurrentLimitKey, l.serviceName)
}
// valueToCurrentLimit returns current limit value.
func (l *limiter) valueToCurrentLimit(v int64, t time.Time) string {
return fmt.Sprintf(formatCurrentLimitValue, v, t.Unix())
}
// NewRateLimiter returns RateLimiter interface.
func NewRateLimiter(serviceName string, cache Cache, opts ...Option) (RateLimiter, error) {
if serviceName == "" || cache == nil {
return nil, ErrInvalidParams
}
l := &limiter{serviceName: serviceName, cache: cache}
var mergeOpt []Option
mergeOpt = append(mergeOpt, defaultOpts...)
mergeOpt = append(mergeOpt, opts...)
for _, opt := range mergeOpt {
opt.apply(l)
}
// minimal estimate period 1 minute.
if l.estimatePeriod < time.Minute {
l.estimatePeriod = time.Minute
}
// run estimate loop
l.estimateLoop()
return l, nil
}
// interfaceToInt64 converts value having interface type to value having int64.
func interfaceToInt64(i interface{}) (int64, error) {
if i == nil {
return 0, ErrInvalidParams
}
switch i.(type) {
case int:
return int64(i.(int)), nil
case int64:
return i.(int64), nil
case int32:
return int64(i.(int32)), nil
case float32:
return int64(i.(float32)), nil
case float64:
return int64(i.(float64)), nil
case string:
return strconv.ParseInt(i.(string), 10, 64)
default:
return 0, ErrUnknown
}
}