-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathratelimit_test.go
More file actions
489 lines (409 loc) · 13.1 KB
/
Copy pathratelimit_test.go
File metadata and controls
489 lines (409 loc) · 13.1 KB
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
package rhttp_test
import (
"context"
"errors"
"net/http"
"strings"
"sync"
"sync/atomic"
"testing"
"time"
"github.com/oswaldom-code/rhttp"
)
func TestTokenBucket_Basic(t *testing.T) {
tb := rhttp.NewTokenBucket(10, 5) // 10 req/s, burst of 5
// Should be able to acquire 5 tokens immediately (burst)
for i := 0; i < 5; i++ {
if !tb.TryAcquire() {
t.Fatalf("expected to acquire token %d", i)
}
}
// 6th should fail
if tb.TryAcquire() {
t.Fatal("expected 6th acquire to fail")
}
}
func TestNewTokenBucket_ZeroRateIsUnlimited(t *testing.T) {
tb := rhttp.NewTokenBucket(0, 1)
// An invalid rate must not limit: without the guard, only the initial burst
// token is granted and WaitContext then busy-loops on a negative wait time.
for i := 0; i < 10; i++ {
if !tb.TryAcquire() {
t.Fatalf("attempt %d: invalid rate must not limit", i)
}
}
ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
defer cancel()
if err := tb.WaitContext(ctx); err != nil {
t.Fatalf("WaitContext on unlimited bucket returned error: %v", err)
}
}
func TestNewTokenBucket_ZeroBurstIsUnlimited(t *testing.T) {
tb := rhttp.NewTokenBucket(10, 0)
// Zero burst must not block forever (maxTokens == 0 → TryAcquire never true).
if !tb.TryAcquire() {
t.Fatal("zero burst must not block forever")
}
ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
defer cancel()
if err := tb.WaitContext(ctx); err != nil {
t.Fatalf("WaitContext on unlimited bucket returned error: %v", err)
}
}
func TestTokenBucket_Refill(t *testing.T) {
tb := rhttp.NewTokenBucket(100, 1) // 100 req/s, burst of 1
// Consume the token
if !tb.TryAcquire() {
t.Fatal("expected to acquire initial token")
}
// Should fail immediately
if tb.TryAcquire() {
t.Fatal("expected acquire to fail immediately after drain")
}
// Wait for refill (10ms for 1 token at 100/s)
time.Sleep(15 * time.Millisecond)
// Should succeed after refill
if !tb.TryAcquire() {
t.Fatal("expected to acquire token after refill")
}
}
func TestTokenBucket_WaitContextBlocksUntilToken(t *testing.T) {
tb := rhttp.NewTokenBucket(100, 1) // 100 req/s, burst of 1
// Consume the token
tb.TryAcquire()
start := time.Now()
err := tb.WaitContext(context.Background())
elapsed := time.Since(start)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
// Should have waited ~10ms
if elapsed < 5*time.Millisecond {
t.Errorf("expected to wait at least 5ms, waited %v", elapsed)
}
}
// stubLimiter mirrors the method set of x/time/rate.Limiter without importing it.
type stubLimiter struct{}
func (stubLimiter) Allow() bool { return true }
func (stubLimiter) Wait(_ context.Context) error { return nil }
// xRateAdapter shows that adapting an x/time/rate style limiter to
// rhttp.RateLimiter takes a struct and two one-line methods.
type xRateAdapter struct{ l stubLimiter }
func (a xRateAdapter) TryAcquire() bool { return a.l.Allow() }
func (a xRateAdapter) WaitContext(ctx context.Context) error { return a.l.Wait(ctx) }
func TestRateLimiter_XTimeRateAdapter(t *testing.T) {
var limiter rhttp.RateLimiter = xRateAdapter{}
rt := rhttp.RoundTripperFunc(func(*http.Request) (*http.Response, error) {
return &http.Response{StatusCode: http.StatusOK, Body: http.NoBody}, nil
})
c := rhttp.New(
rhttp.WithTransport(rt),
rhttp.WithMiddleware(rhttp.RateLimit(rhttp.RateLimitConfig{Limiter: limiter})),
)
req, _ := http.NewRequest(http.MethodGet, "http://example.com", http.NoBody)
resp, err := c.Do(context.Background(), req)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
resp.Body.Close()
}
func TestTokenBucket_Concurrent(t *testing.T) {
tb := rhttp.NewTokenBucket(1000, 100)
var acquired int64
var wg sync.WaitGroup
for i := 0; i < 100; i++ {
wg.Add(1)
go func() {
defer wg.Done()
if tb.TryAcquire() {
atomic.AddInt64(&acquired, 1)
}
}()
}
wg.Wait()
if acquired != 100 {
t.Errorf("expected 100 acquired, got %d", acquired)
}
}
func TestRateLimit_Middleware(t *testing.T) {
var calls int32
rt := rhttp.RoundTripperFunc(func(req *http.Request) (*http.Response, error) {
atomic.AddInt32(&calls, 1)
return &http.Response{StatusCode: http.StatusOK, Request: req}, nil
})
limiter := rhttp.NewTokenBucket(1000, 10)
c := rhttp.New(
rhttp.WithTransport(rt),
rhttp.WithMiddleware(rhttp.RateLimit(rhttp.RateLimitConfig{
Limiter: limiter,
WaitOnLimit: true,
})),
)
// Should succeed within burst
for i := 0; i < 10; i++ {
req, _ := http.NewRequest(http.MethodGet, "http://example.com", http.NoBody)
_, err := c.Do(context.Background(), req)
if err != nil {
t.Fatalf("request %d failed: %v", i, err)
}
}
if calls != 10 {
t.Errorf("expected 10 calls, got %d", calls)
}
}
func TestRateLimit_NoWait(t *testing.T) {
rt := rhttp.RoundTripperFunc(func(req *http.Request) (*http.Response, error) {
return &http.Response{StatusCode: http.StatusOK, Request: req}, nil
})
limiter := rhttp.NewTokenBucket(1, 1) // 1 req/s, burst of 1
c := rhttp.New(
rhttp.WithTransport(rt),
rhttp.WithMiddleware(rhttp.RateLimit(rhttp.RateLimitConfig{
Limiter: limiter,
WaitOnLimit: false,
})),
)
// First should succeed
req, _ := http.NewRequest(http.MethodGet, "http://example.com", http.NoBody)
_, err := c.Do(context.Background(), req)
if err != nil {
t.Fatalf("first request failed: %v", err)
}
// Second should fail immediately
req, _ = http.NewRequest(http.MethodGet, "http://example.com", http.NoBody)
_, err = c.Do(context.Background(), req)
if !errors.Is(err, rhttp.ErrRateLimited) {
t.Fatalf("expected ErrRateLimited, got %v", err)
}
}
func TestRateLimit_RespectRetryAfter(t *testing.T) {
callCount := 0
rt := rhttp.RoundTripperFunc(func(req *http.Request) (*http.Response, error) {
callCount++
if callCount == 1 {
resp := &http.Response{
StatusCode: http.StatusTooManyRequests,
Header: make(http.Header),
Request: req,
}
resp.Header.Set("Retry-After", "1") // 1 second
return resp, nil
}
return &http.Response{StatusCode: http.StatusOK, Request: req}, nil
})
limiter := rhttp.NewTokenBucket(1000, 100)
c := rhttp.New(
rhttp.WithTransport(rt),
rhttp.WithMiddleware(rhttp.RateLimit(rhttp.RateLimitConfig{
Limiter: limiter,
WaitOnLimit: true,
RespectRetryAfter: true,
})),
)
// First request gets 429
req, _ := http.NewRequest(http.MethodGet, "http://example.com", http.NoBody)
resp, _ := c.Do(context.Background(), req)
if resp.StatusCode != http.StatusTooManyRequests {
t.Fatalf("expected 429, got %d", resp.StatusCode)
}
// Second request should wait for Retry-After
start := time.Now()
req, _ = http.NewRequest(http.MethodGet, "http://example.com", http.NoBody)
resp, _ = c.Do(context.Background(), req)
elapsed := time.Since(start)
if resp.StatusCode != http.StatusOK {
t.Fatalf("expected 200, got %d", resp.StatusCode)
}
// Should have waited ~1 second
if elapsed < 900*time.Millisecond {
t.Errorf("expected to wait ~1s for Retry-After, waited %v", elapsed)
}
}
func TestRateLimit_NilLimiter(t *testing.T) {
rt := rhttp.RoundTripperFunc(func(req *http.Request) (*http.Response, error) {
return &http.Response{StatusCode: http.StatusOK, Request: req}, nil
})
c := rhttp.New(
rhttp.WithTransport(rt),
rhttp.WithMiddleware(rhttp.RateLimit(rhttp.RateLimitConfig{
Limiter: nil,
})),
)
req, _ := http.NewRequest(http.MethodGet, "http://example.com", http.NoBody)
resp, err := c.Do(context.Background(), req)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if resp.StatusCode != http.StatusOK {
t.Fatalf("expected 200, got %d", resp.StatusCode)
}
}
func BenchmarkTokenBucket_TryAcquire(b *testing.B) {
tb := rhttp.NewTokenBucket(1000000, 1000000) // high limits
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
tb.TryAcquire()
}
}
func BenchmarkTokenBucket_Concurrent(b *testing.B) {
tb := rhttp.NewTokenBucket(1000000, 1000000)
b.ResetTimer()
b.ReportAllocs()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
tb.TryAcquire()
}
})
}
func TestRateLimit_FailFastClosesRequestBody(t *testing.T) {
rt := rhttp.RoundTripperFunc(func(req *http.Request) (*http.Response, error) {
return &http.Response{StatusCode: http.StatusOK, Body: http.NoBody, Request: req}, nil
})
limiter := rhttp.NewTokenBucket(1, 1)
limiter.TryAcquire()
c := rhttp.New(
rhttp.WithTransport(rt),
rhttp.WithMiddleware(rhttp.RateLimit(rhttp.RateLimitConfig{Limiter: limiter})),
)
rec := &closeRecorder{Reader: strings.NewReader("payload")}
req, _ := http.NewRequest(http.MethodPut, "http://example.com", http.NoBody)
req.Body = rec
_, err := c.Do(context.Background(), req)
if !errors.Is(err, rhttp.ErrRateLimited) {
t.Fatalf("expected ErrRateLimited, got %v", err)
}
if !rec.closed {
t.Error("request body was not closed on rate-limit short-circuit")
}
}
func TestTokenBucket_TokensReportsAvailability(t *testing.T) {
tb := rhttp.NewTokenBucket(1, 5) // 1 token/s: refill drift is negligible
if got := tb.Tokens(); got != 5 {
t.Fatalf("expected a full bucket of 5 tokens, got %v", got)
}
tb.TryAcquire()
tb.TryAcquire()
got := tb.Tokens()
if got < 3 || got >= 4 {
t.Fatalf("expected ~3 tokens after two acquires, got %v", got)
}
}
func TestTokenBucket_WaitContextAlreadyCanceled(t *testing.T) {
tb := rhttp.NewTokenBucket(1, 1)
tb.TryAcquire()
ctx, cancel := context.WithCancel(context.Background())
cancel()
start := time.Now()
err := tb.WaitContext(ctx)
if !errors.Is(err, context.Canceled) {
t.Fatalf("expected context.Canceled, got %v", err)
}
if elapsed := time.Since(start); elapsed > 100*time.Millisecond {
t.Errorf("expected immediate return on canceled ctx, took %v", elapsed)
}
}
func TestRateLimit_RetryAfterHTTPDate(t *testing.T) {
var calls int32
rt := rhttp.RoundTripperFunc(func(req *http.Request) (*http.Response, error) {
if atomic.AddInt32(&calls, 1) == 1 {
resp := &http.Response{
StatusCode: http.StatusTooManyRequests,
Header: make(http.Header),
Request: req,
}
resp.Header.Set("Retry-After", time.Now().Add(2*time.Second).UTC().Format(http.TimeFormat))
return resp, nil
}
return &http.Response{StatusCode: http.StatusOK, Request: req}, nil
})
limiter := rhttp.NewTokenBucket(1000, 100)
c := rhttp.New(
rhttp.WithTransport(rt),
rhttp.WithMiddleware(rhttp.RateLimit(rhttp.RateLimitConfig{
Limiter: limiter,
RespectRetryAfter: true,
})),
)
req, _ := http.NewRequest(http.MethodGet, "http://example.com", http.NoBody)
_, _ = c.Do(context.Background(), req)
start := time.Now()
req, _ = http.NewRequest(http.MethodGet, "http://example.com", http.NoBody)
resp, err := c.Do(context.Background(), req)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if resp.StatusCode != http.StatusOK {
t.Fatalf("expected 200, got %d", resp.StatusCode)
}
if elapsed := time.Since(start); elapsed < 900*time.Millisecond {
t.Errorf("expected to honor the HTTP-date Retry-After (~1-2s), waited %v", elapsed)
}
}
func TestRateLimit_CtxCanceledDuringRetryAfterWait(t *testing.T) {
var calls int32
rt := rhttp.RoundTripperFunc(func(req *http.Request) (*http.Response, error) {
atomic.AddInt32(&calls, 1)
resp := &http.Response{
StatusCode: http.StatusTooManyRequests,
Header: make(http.Header),
Request: req,
}
resp.Header.Set("Retry-After", "2")
return resp, nil
})
limiter := rhttp.NewTokenBucket(1000, 100)
c := rhttp.New(
rhttp.WithTransport(rt),
rhttp.WithMiddleware(rhttp.RateLimit(rhttp.RateLimitConfig{
Limiter: limiter,
RespectRetryAfter: true,
})),
)
req, _ := http.NewRequest(http.MethodGet, "http://example.com", http.NoBody)
_, _ = c.Do(context.Background(), req)
ctx, cancel := context.WithTimeout(context.Background(), 50*time.Millisecond)
defer cancel()
start := time.Now()
req, _ = http.NewRequest(http.MethodGet, "http://example.com", http.NoBody)
_, err := c.Do(ctx, req)
if !errors.Is(err, context.DeadlineExceeded) {
t.Fatalf("expected DeadlineExceeded during Retry-After wait, got %v", err)
}
if elapsed := time.Since(start); elapsed > 1*time.Second {
t.Errorf("expected the canceled ctx to cut the 2s wait, took %v", elapsed)
}
}
func TestRateLimit_RespectRetryAfterConcurrent(t *testing.T) {
var calls int32
rt := rhttp.RoundTripperFunc(func(req *http.Request) (*http.Response, error) {
if atomic.AddInt32(&calls, 1)%3 == 0 {
resp := &http.Response{
StatusCode: http.StatusTooManyRequests,
Header: make(http.Header),
Request: req,
}
resp.Header.Set("Retry-After", "0")
return resp, nil
}
return &http.Response{StatusCode: http.StatusOK, Request: req}, nil
})
limiter := rhttp.NewTokenBucket(100000, 1000)
c := rhttp.New(
rhttp.WithTransport(rt),
rhttp.WithMiddleware(rhttp.RateLimit(rhttp.RateLimitConfig{
Limiter: limiter,
RespectRetryAfter: true,
})),
)
var wg sync.WaitGroup
for i := 0; i < 50; i++ {
wg.Add(1)
go func() {
defer wg.Done()
req, _ := http.NewRequest(http.MethodGet, "http://example.com", http.NoBody)
_, _ = c.Do(context.Background(), req)
}()
}
wg.Wait()
}