-
Notifications
You must be signed in to change notification settings - Fork 1
/
rate_limiter.go
198 lines (165 loc) · 5.15 KB
/
rate_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
package gocache
import (
"errors"
"fmt"
"time"
)
// NewRateLimiter creates an instance of *RateLimiter
func NewRateLimiter(cache Cache) *RateLimiter {
return &RateLimiter{
cache: cache,
}
}
type (
// RateLimiter is a struct whose purpose is to limit the rate at which something is executed or accessed. The
// underlying logic of this implementation allows for a max number of hits x for a given duration y. If x is
// exceeded during the y timeframe the RateLimiter will limit further calls until duration y expires. Once
// duration y is expired calls will reset back to 0
RateLimiter struct {
cache Cache
}
// ThrottleResponse contains relevant information with respect to calls that are meant to be rate limited
ThrottleResponse struct {
retryAfter time.Duration
remainingAttempts int64
maxAttempts int64
}
)
// TooManyAttempts determines if the given key has been "accessed" too many times
func (l *RateLimiter) TooManyAttempts(key string, maxAttempts int64) (bool, error) {
left, err := l.AttemptsLeft(key, maxAttempts)
if err != nil {
return false, err
}
return left == 0, nil
}
// Hit increments the counter for a given key for a given decay time
func (l *RateLimiter) Hit(key string, decay time.Duration) (hits int64, err error) {
// The cache will manage the decay as the timer will be expired by the cache
if _, err = l.cache.Add(l.formatKey(key, "timer"), l.availableAt(decay).Unix(), decay); err != nil {
return 0, err
}
if _, err = l.cache.Add(l.formatKey(key, "counter"), int64(0), decay); err != nil {
return 0, err
}
hits, err = l.cache.Increment(l.formatKey(key, "counter"), 1)
if err != nil {
return 0, err
}
return hits, nil
}
// Attempts gets the number of attempts for the given key
func (l *RateLimiter) Attempts(key string) (int64, error) {
val, err := l.cache.GetInt64(l.formatKey(key, "counter"))
if err != nil && !errors.Is(err, ErrNotFound) {
return val, err
}
return val, nil
}
// AttemptsLeft gets the number of attempts left for a given key
func (l *RateLimiter) AttemptsLeft(key string, maxAttempts int64) (int64, error) {
attempts, err := l.Attempts(key)
if err != nil {
return 0, err
}
left := maxAttempts - attempts
if left > 0 {
return left, nil
}
val, err := l.cache.GetInt64(l.formatKey(key, "timer"))
if err != nil && !errors.Is(err, ErrNotFound) {
return 0, err
}
if val > 0 {
return 0, nil
}
// If the timer is already at 0 we can clear the counter and timer
// and return max attempts
if err = l.Clear(key); err != nil {
return 0, err
}
return maxAttempts, nil
}
// Clear clears the hits and lockout timer for the given key
func (l *RateLimiter) Clear(key string) error {
if _, err := l.cache.Forget(l.formatKey(key, "counter")); err != nil {
return err
}
_, err := l.cache.Forget(l.formatKey(key, "timer"))
return err
}
// AvailableIn gets the number of seconds until the "key" is accessible again
func (l *RateLimiter) AvailableIn(key string) (time.Duration, error) {
unixTime, err := l.cache.GetInt64(l.formatKey(key, "timer"))
if err != nil && !errors.Is(err, ErrNotFound) {
return 0, err
}
in := unixTime - time.Now().Unix()
if in < 0 {
in = 0
}
return time.Duration(in) * time.Second, nil
}
// Throttle rate limits the calls that made to fn
func (l *RateLimiter) Throttle(key string, maxCalls int64, decay time.Duration, fn func() error) (*ThrottleResponse, error) {
tooMany, err := l.TooManyAttempts(key, maxCalls)
if err != nil {
return nil, err
}
if tooMany {
return l.buildResponse(key, maxCalls, tooMany)
}
attempts, err := l.Hit(key, decay)
if err != nil {
return nil, err
}
if tooMany = attempts > maxCalls; tooMany {
return l.buildResponse(key, maxCalls, tooMany)
}
if err = fn(); err != nil {
return nil, err
}
return l.buildResponse(key, maxCalls, tooMany)
}
func (l *RateLimiter) buildResponse(key string, maxAttempts int64, tooMany bool) (*ThrottleResponse, error) {
retryAfter, err := l.AvailableIn(key)
if err != nil {
return nil, err
}
var remainingAttempts int64
if !tooMany {
attempts, err := l.AttemptsLeft(key, maxAttempts)
if err != nil {
return nil, err
}
remainingAttempts = attempts + 1
}
return &ThrottleResponse{
retryAfter: retryAfter,
remainingAttempts: remainingAttempts,
maxAttempts: maxAttempts,
}, nil
}
// availableAt returns the current time plus a decay duration
func (*RateLimiter) availableAt(decay time.Duration) time.Time {
return time.Now().Add(decay)
}
func (*RateLimiter) formatKey(p1, p2 string) string {
return fmt.Sprintf("%s:%s", p1, p2)
}
// RetryAfter returns the duration that one should wait before executing the next call
func (r *ThrottleResponse) RetryAfter() time.Duration {
return r.retryAfter
}
// RemainingAttempts returns the remaining calls that can be made to a function before it is throttled
func (r *ThrottleResponse) RemainingAttempts() int64 {
return r.remainingAttempts
}
// MaxAttempts returns the max attempts for a given call
func (r *ThrottleResponse) MaxAttempts() int64 {
return r.maxAttempts
}
// IsThrottled returns if the number of calls have been throttled
func (r *ThrottleResponse) IsThrottled() bool {
return r.remainingAttempts == 0
}