-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlimiter_fixed_truncated_window.go
236 lines (188 loc) · 5.74 KB
/
limiter_fixed_truncated_window.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
package pacemaker
import (
"context"
"sync"
"time"
)
type fixedTruncatedWindowStorage interface {
Inc(
ctx context.Context,
args FixedWindowIncArgs,
) (int64, error)
Get(
ctx context.Context,
window time.Time,
) (int64, error)
}
type FixedTruncatedWindowArgs struct {
Capacity int64
Rate Rate
Clock clock
DB fixedTruncatedWindowStorage
}
// FixedTruncatedWindowRateLimiter limits how many requests check be make in a time window. This window is calculated
// by truncating the first request's time of to the limit rate in order to adjust to real time passing. E.g:
// First request time: 2022-02-05 10:23:23
// Rate limit interval: new window every 10 seconds
// First request window: from 2022-02-05 10:23:20 to 2022-02-05 10:23:30
type FixedTruncatedWindowRateLimiter struct {
db fixedTruncatedWindowStorage
clock clock
validateTokens func(int64) int64
mu sync.Mutex
rate Rate
window time.Time
capacity int64
rateLimitReached bool
}
// Try returns how much time to wait to perform the request and an error indicating whether the rate limit
// was exhausted or any kind or error happened when updating the backend. Typically, you would do
//
// ttw, err := limiter.Try(ctx)
//
// if errors.Is(ErrRateLimitExceeded) {
// <-time.After(ttw) // Wait, or enqueue your request
// }
func (l *FixedTruncatedWindowRateLimiter) Try(ctx context.Context) (Result, error) {
return l.try(ctx, 1)
}
// Check return how many free slots remain without increasing the token counter. This testMethod is typically used
// to assert there are available requests prior try an increase the counter
func (l *FixedTruncatedWindowRateLimiter) Check(ctx context.Context) (Result, error) {
return l.check(ctx, 1)
}
func (l *FixedTruncatedWindowRateLimiter) Dump(ctx context.Context) (r Result, err error) {
l.mu.Lock()
defer l.mu.Unlock()
now := l.clock.Now()
if TimeGTE(l.window.Add(l.rate.Duration()), now) {
l.rateLimitReached = false
l.window = now.Truncate(l.rate.TruncateDuration())
}
c, err := l.db.Get(ctx, l.window)
if c >= l.capacity {
// rate limit exceeded
r = res(l.window.Add(l.rate.Duration()).Sub(now), l.capacity-c)
} else {
r = res(0, l.capacity-c)
}
return
}
func (l *FixedTruncatedWindowRateLimiter) try(ctx context.Context, tokens int64) (Result, error) {
tokens = l.validateTokens(tokens)
if tokens > l.capacity {
return nores, ErrTokensGreaterThanCapacity
}
l.mu.Lock()
defer l.mu.Unlock()
now := l.clock.Now()
if TimeGTE(l.window.Add(l.rate.Duration()), now) {
l.rateLimitReached = false
l.window = now.Truncate(l.rate.TruncateDuration())
}
ttw := l.window.Add(l.rate.Duration()).Sub(now)
if l.rateLimitReached {
return res(ttw, 0), ErrRateLimitExceeded
}
c, err := l.db.Inc(ctx, FixedWindowIncArgs{
Window: l.window,
Tokens: tokens,
Capacity: l.capacity,
TTL: ttw,
})
if err != nil {
// TODO: make configurable
return nores, err
}
if c > l.capacity {
l.rateLimitReached = true
return res(ttw, 0), ErrRateLimitExceeded
}
free := l.capacity - c
if free >= 0 {
return res(0, l.capacity-c), nil
}
return res(ttw, l.capacity-c), ErrRateLimitExceeded
}
func (l *FixedTruncatedWindowRateLimiter) check(ctx context.Context, tokens int64) (Result, error) {
l.mu.Lock()
defer l.mu.Unlock()
now := l.clock.Now()
if TimeGTE(l.window.Add(l.rate.Duration()), now) {
// new window so no rate Limit
l.rateLimitReached = false
l.window = now.Truncate(l.rate.TruncateDuration())
return res(0, l.capacity), nil
}
ttw := l.window.Add(l.rate.Duration()).Sub(now)
if l.rateLimitReached {
return res(ttw, 0), ErrRateLimitExceeded
}
c, err := l.db.Get(ctx, l.window)
if err != nil {
return nores, err
}
if c >= l.capacity {
l.rateLimitReached = true
return res(ttw, l.capacity-c), ErrRateLimitExceeded
}
free := l.capacity - c - tokens
if free >= 0 {
return res(0, l.capacity-c), nil
}
return res(ttw, l.capacity-c), ErrRateLimitExceeded
}
func (l *FixedTruncatedWindowRateLimiter) fixedWindow() {}
// NewFixedTruncatedWindowRateLimiter returns a new instance of FixedTruncatedWindowRateLimiter from struct of args
func NewFixedTruncatedWindowRateLimiter(
args FixedTruncatedWindowArgs,
) *FixedTruncatedWindowRateLimiter {
return &FixedTruncatedWindowRateLimiter{
capacity: args.Capacity,
rate: args.Rate,
clock: args.Clock,
db: args.DB,
rateLimitReached: false,
validateTokens: AtLeast(1),
}
}
// FixedTruncatedWindowMemoryStorage is an in-memory storage for the rate limit state. Preferred option when testing and
// working with standalone instances of your program and do not care about it restarting and not being exactly compliant
// with the state of rate limits at the server
type FixedTruncatedWindowMemoryStorage struct {
mu sync.Mutex
previousWindow time.Time
counter int64
ttl time.Duration
}
func (s *FixedTruncatedWindowMemoryStorage) Inc(
ctx context.Context,
args FixedWindowIncArgs,
) (int64, error) {
s.mu.Lock()
defer s.mu.Unlock()
if !s.previousWindow.Equal(args.Window) {
s.previousWindow = args.Window
s.counter = 0
}
counter := args.Tokens + s.counter
s.counter = min(counter, args.Capacity)
s.ttl = args.TTL
return counter, ctx.Err()
}
func (s *FixedTruncatedWindowMemoryStorage) Get(
ctx context.Context,
window time.Time,
) (int64, error) {
s.mu.Lock()
defer s.mu.Unlock()
if !s.previousWindow.Equal(window) {
s.previousWindow = window
s.counter = 0
}
return s.counter, ctx.Err()
}
// NewFixedTruncatedWindowMemoryStorage returns a new instance of FixedTruncatedWindowMemoryStorage
func NewFixedTruncatedWindowMemoryStorage() *FixedTruncatedWindowMemoryStorage {
return &FixedTruncatedWindowMemoryStorage{}
}