-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathmiddleware_test.go
More file actions
959 lines (889 loc) · 28 KB
/
middleware_test.go
File metadata and controls
959 lines (889 loc) · 28 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
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
/*
Copyright © 2024 Acronis International GmbH.
Released under MIT license.
*/
package throttle
import (
"bytes"
"fmt"
"net/http"
"net/http/httptest"
"strconv"
"strings"
"sync"
"testing"
"time"
"github.com/stretchr/testify/require"
"go.uber.org/atomic"
"github.com/acronis/go-appkit/config"
"github.com/acronis/go-appkit/httpserver/middleware"
"github.com/acronis/go-appkit/log"
)
const testErrDomain = "TestService"
func TestLimitHandler_ServeHTTP(t *testing.T) {
matchedPrefixedRoutes := []string{"POST /aaa", "PUT /aaa", "DELETE /aaa", "POST /aaa/", "PUT /aaa/bbb", "DELETE /aaa/b/c"}
matchedExactRoutes := []string{"GET /bbb", "POST /bbb"}
var matchedRoutes []string
matchedRoutes = append(matchedRoutes, matchedPrefixedRoutes...)
matchedRoutes = append(matchedRoutes, matchedExactRoutes...)
unmatchedPrefixedRoutes := []string{"GET /aaa", "HEAD /aaa", "GET /aaa/b"}
unmatchedExactRoutes := []string{"GET /bbb/", "POST /bbb/", "GET /bbb/a"}
unmatchedOtherRoutes := []string{"POST /a", "PUT /b", "DELETE /c"}
var unmatchedRoutes []string
unmatchedRoutes = append(unmatchedRoutes, unmatchedPrefixedRoutes...)
unmatchedRoutes = append(unmatchedRoutes, unmatchedExactRoutes...)
unmatchedRoutes = append(unmatchedRoutes, unmatchedOtherRoutes...)
tests := []struct {
Name string
CfgData string
Func func(t *testing.T, cfg *Config)
}{
{
Name: "rate limiting, leaky bucket",
CfgData: `
rateLimitZones:
rl_zone:
rateLimit: 1/m
burstLimit: 10
responseStatusCode: 503
responseRetryAfter: 5s
rules:
- routes:
- path: "/aaa"
methods: POST,PUT,DELETE
- path: "= /bbb"
rateLimits:
- zone: rl_zone
`,
Func: func(t *testing.T, cfg *Config) {
const burst = 10
// Prefixed path matching.
reqsGen := makeReqsGenerator(matchedPrefixedRoutes)
checkRateLimiting(t, cfg, reqsGen, burst+1, 30, 503, time.Second*5)
// Prefixed path unmatching.
reqsGen = makeReqsGenerator(unmatchedPrefixedRoutes)
checkNoRateLimiting(t, cfg, reqsGen, 30)
// Exact path matching.
reqsGen = makeReqsGenerator(matchedExactRoutes)
checkRateLimiting(t, cfg, reqsGen, burst+1, 30, 503, time.Second*5)
// Exact path unmatching.
reqsGen = makeReqsGenerator(unmatchedExactRoutes)
checkNoRateLimiting(t, cfg, reqsGen, 30)
// Other endpoints should NOT be throttled.
reqsGen = makeReqsGenerator(unmatchedOtherRoutes)
checkNoRateLimiting(t, cfg, reqsGen, 30)
// Paths with dotes are normalised before throttling.
reqsGen = makeReqsGenerator([]string{"GET /bbb/.", "GET /bbb/cc/..", "GET /bbb/cc/../cc/..", "GET /bbb/cc/../././."})
checkRateLimiting(t, cfg, reqsGen, burst+1, 30, 503, time.Second*5)
},
},
{
Name: "rate limiting, sliding window",
CfgData: `
rateLimitZones:
rl_zone:
alg: sliding_window
rateLimit: 10/m
responseStatusCode: 503
responseRetryAfter: 5s
rules:
- routes:
- path: "/aaa"
methods: POST,PUT,DELETE
- path: "= /bbb"
rateLimits:
- zone: rl_zone
`,
Func: func(t *testing.T, cfg *Config) {
const ratePerMinute = 10
// Prefixed path matching.
reqsGen := makeReqsGenerator(matchedPrefixedRoutes)
checkRateLimiting(t, cfg, reqsGen, ratePerMinute, 30, 503, time.Second*5)
// Prefixed path unmatching.
reqsGen = makeReqsGenerator(unmatchedPrefixedRoutes)
checkNoRateLimiting(t, cfg, reqsGen, 30)
// Exact path matching.
reqsGen = makeReqsGenerator(matchedExactRoutes)
checkRateLimiting(t, cfg, reqsGen, ratePerMinute, 30, 503, time.Second*5)
// Exact path unmatching.
reqsGen = makeReqsGenerator(unmatchedExactRoutes)
checkNoRateLimiting(t, cfg, reqsGen, 30)
// Other endpoints should NOT be throttled.
reqsGen = makeReqsGenerator(unmatchedOtherRoutes)
checkNoRateLimiting(t, cfg, reqsGen, 30)
// Paths with dotes are normalised before throttling.
reqsGen = makeReqsGenerator([]string{"GET /bbb/.", "GET /bbb/cc/..", "GET /bbb/cc/../cc/..", "GET /bbb/cc/../././."})
checkRateLimiting(t, cfg, reqsGen, ratePerMinute, 30, 503, time.Second*5)
},
},
{
Name: "rate limiting, leaky bucket, backlogLimit > 0",
CfgData: `
rateLimitZones:
rl_zone:
rateLimit: 1/s
backlogLimit: 3
responseStatusCode: 503
responseRetryAfter: 5s
rules:
- routes:
- path: "/aaa"
methods: POST,PUT,DELETE
- path: "= /bbb"
rateLimits:
- zone: rl_zone
`,
Func: func(t *testing.T, cfg *Config) {
reqsGen := makeReqsGenerator(matchedPrefixedRoutes)
checkRateLimiting(t, cfg, reqsGen, 3, 3, 503, time.Second*5)
},
},
{
Name: "rate limiting, leaky bucket, dry-run mode",
CfgData: `
rateLimitZones:
rl_zone:
rateLimit: 1/m
burstLimit: 10
responseStatusCode: 503
responseRetryAfter: 5s
dryRun: true
rules:
- routes:
- path: "/aaa"
methods: POST,PUT,DELETE
- path: "= /bbb"
rateLimits:
- zone: rl_zone
`,
Func: func(t *testing.T, cfg *Config) {
checkRateLimitingInDryRun(t, cfg, makeReqsGenerator(matchedRoutes), 10, 30)
checkNoRateLimiting(t, cfg, makeReqsGenerator(unmatchedRoutes), 30)
},
},
{
Name: "rate limiting, leaky bucket, by http header",
CfgData: `
rateLimitZones:
rl_zone:
key:
type: header
headerName: x-client-id
noBypassEmpty: true
excludedKeys: ["good-client1", "good-client2", "very-good-client*"]
rateLimit: 1/m
burstLimit: 10
responseStatusCode: 429
responseRetryAfter: 30s
rules:
- routes:
- path: "/aaa"
methods: POST,PUT,DELETE
- path: "= /bbb"
rateLimits:
- zone: rl_zone
`,
Func: func(t *testing.T, cfg *Config) {
const burst = 10
// Many requests with the same X-Client-ID. Should be throttled.
reqsGen := makeReqsGenerator(matchedRoutes)
reqsGenWithHeader := func() *http.Request {
r := reqsGen()
r.Header.Set("X-Client-ID", "client-id")
return r
}
checkRateLimiting(t, cfg, reqsGenWithHeader, burst+1, 30, 429, time.Second*30)
// Many requests with missing X-Client-ID. Should be throttled since noBypassEmpty is true.
checkRateLimiting(t, cfg, makeReqsGenerator(matchedRoutes), burst+1, 30, 429, time.Second*30)
// Many requests with the different X-Client-ID.
reqsGen = makeReqsGenerator(matchedRoutes)
clientIDsGen := makeFmtGenerator("client-%d")
reqsGenWithHeader = func() *http.Request {
r := reqsGen()
r.Header.Set("X-Client-ID", clientIDsGen())
return r
}
checkNoRateLimiting(t, cfg, reqsGenWithHeader, 100)
// Excluded clients should NOT be throttled.
reqsGen = makeReqsGenerator(matchedRoutes)
clientIDsGen = makeStrsGenerator([]string{"good-client1", "good-client2", "very-good-client1", "very-good-client777"})
reqsGenWithHeader = func() *http.Request {
r := reqsGen()
r.Header.Set("X-Client-ID", clientIDsGen())
return r
}
checkNoRateLimiting(t, cfg, reqsGenWithHeader, 100)
},
},
{
Name: "rate limiting, leaky bucket, by identity",
CfgData: `
rateLimitZones:
rl_zone:
key:
type: identity
includedKeys: ["bad-user1", "bad-user2", "very-bad-user*"]
rateLimit: 1/m
burstLimit: 10
responseStatusCode: 429
responseRetryAfter: 60s
rules:
- routes:
- path: "/aaa"
methods: POST,PUT,DELETE
- path: "= /bbb"
rateLimits:
- zone: rl_zone
`,
Func: func(t *testing.T, cfg *Config) {
const burst = 10
// Included clients should be throttled.
for _, identity := range []string{"bad-user1", "bad-user2", "very-bad-user1", "very-bad-user777"} {
reqsGen := makeReqsGenerator(matchedRoutes)
reqsGenWithBasicAuth := func() *http.Request {
r := reqsGen()
r.SetBasicAuth(identity, identity+"-password")
return r
}
checkRateLimiting(t, cfg, reqsGenWithBasicAuth, burst+1, 30, 429, time.Second*60)
}
// Other clients should NOT be throttled.
reqsGen := makeReqsGenerator(matchedRoutes)
reqsGenWithBasicAuth := func() *http.Request {
r := reqsGen()
r.SetBasicAuth("good-user", "good-user-password")
return r
}
checkNoRateLimiting(t, cfg, reqsGenWithBasicAuth, 30)
},
},
{
Name: "rate limiting, leaky bucket, by remote addr",
CfgData: `
rateLimitZones:
rl_zone:
key:
type: remote_addr
rateLimit: 1/m
burstLimit: 10
responseStatusCode: 429
responseRetryAfter: 5s
rules:
- routes:
- path: "/aaa"
methods: POST,PUT,DELETE
rateLimits:
- zone: rl_zone
`,
Func: func(t *testing.T, cfg *Config) {
const burst = 10
// Prefixed path matching.
reqsGen := makeReqsGenerator(matchedPrefixedRoutes)
checkRateLimiting(t, cfg, reqsGen, burst+1, 30, 429, time.Second*5)
// Other endpoints should NOT be throttled.
reqsGen = makeReqsGenerator(unmatchedOtherRoutes)
checkNoRateLimiting(t, cfg, reqsGen, 30)
},
},
{
Name: "in-flight limiting",
CfgData: `
inFlightLimitZones:
ifl_zone:
inFlightLimit: 5
backlogLimit: 5
backlogTimeout: 30s
responseStatusCode: 503
responseRetryAfter: 5s
rules:
- routes:
- path: "/aaa"
methods: POST,PUT,DELETE
- path: "= /bbb"
inFlightLimits:
- zone: ifl_zone
`,
Func: func(t *testing.T, cfg *Config) {
checkInFlightLimiting(t, cfg, checkInFlightLimitingParams{
reqsGen: makeReqsGenerator(matchedRoutes),
totalLimit: 10,
reqsNum: 20,
unblockDelay: time.Second,
wantRespCode: 503,
wantRetryAfter: time.Second * 5,
})
checkNoInFlightLimiting(t, cfg, makeReqsGenerator(unmatchedRoutes), 20, time.Second)
},
},
{
Name: "in-flight limiting, build handler at init",
CfgData: `
inFlightLimitZones:
ifl_zone:
inFlightLimit: 5
backlogLimit: 5
backlogTimeout: 30s
responseStatusCode: 503
responseRetryAfter: 5s
rules:
- routes:
- path: "/aaa"
methods: POST,PUT,DELETE
- path: "= /bbb"
inFlightLimits:
- zone: ifl_zone
`,
Func: func(t *testing.T, cfg *Config) {
checkInFlightLimiting(t, cfg, checkInFlightLimitingParams{
reqsGen: makeReqsGenerator(matchedRoutes),
totalLimit: 10,
reqsNum: 20,
unblockDelay: time.Second,
wantRespCode: 503,
wantRetryAfter: time.Second * 5,
buildHandlerAtInit: true,
})
},
},
{
Name: "in-flight limiting, dry-run mode",
CfgData: `
inFlightLimitZones:
ifl_zone:
inFlightLimit: 10
backlogTimeout: 30s
responseStatusCode: 503
responseRetryAfter: 5s
dryRun: true
rules:
- routes:
- path: "/aaa"
methods: POST,PUT,DELETE
- path: "= /bbb"
inFlightLimits:
- zone: ifl_zone
`,
Func: func(t *testing.T, cfg *Config) {
checkInFlightLimitingInDryRun(t, cfg, makeReqsGenerator(matchedRoutes), 10, 20, time.Second)
checkNoInFlightLimiting(t, cfg, makeReqsGenerator(unmatchedRoutes), 20, time.Second)
},
},
{
Name: "in-flight limiting by http header",
CfgData: `
inFlightLimitZones:
ifl_zone:
key:
type: header
headerName: x-client-id
excludedKeys: ["good-client1", "good-client2", "very-good-client*"]
inFlightLimit: 5
backlogLimit: 5
backlogTimeout: 30s
responseStatusCode: 429
responseRetryAfter: 30s
rules:
- routes:
- path: "/aaa"
methods: POST,PUT,DELETE
- path: "= /bbb"
inFlightLimits:
- zone: ifl_zone
`,
Func: func(t *testing.T, cfg *Config) {
// Many requests with the same X-Client-ID. Should be throttled.
reqsGen := makeReqsGenerator(matchedRoutes)
reqsGenWithHeader := func() *http.Request {
r := reqsGen()
r.Header.Set("X-Client-ID", "client-id")
return r
}
checkInFlightLimiting(t, cfg, checkInFlightLimitingParams{
reqsGen: reqsGenWithHeader,
totalLimit: 10,
reqsNum: 20,
unblockDelay: time.Second,
wantRespCode: 429,
wantRetryAfter: time.Second * 30,
})
// Many requests with missing X-Client-ID should NOT be throttled since noBypassEmpty is false (by default).
checkNoInFlightLimiting(t, cfg, makeReqsGenerator(matchedRoutes), 20, time.Second)
// Many requests with the different X-Client-ID should NOT be throttled.
reqsGen = makeReqsGenerator(matchedRoutes)
clientIDsGen := makeFmtGenerator("client-%d")
reqsGenWithHeader = func() *http.Request {
r := reqsGen()
r.Header.Set("X-Client-ID", clientIDsGen())
return r
}
checkNoInFlightLimiting(t, cfg, reqsGenWithHeader, 20, time.Second)
// Excluded clients should NOT be throttled.
reqsGen = makeReqsGenerator(matchedRoutes)
clientIDsGen = makeStrsGenerator([]string{"good-client1", "good-client2", "very-good-client1", "very-good-client777"})
reqsGenWithHeader = func() *http.Request {
r := reqsGen()
r.Header.Set("X-Client-ID", clientIDsGen())
return r
}
checkNoInFlightLimiting(t, cfg, reqsGenWithHeader, 40, time.Second)
},
},
{
Name: "in-flight limiting by identity",
CfgData: `
inFlightLimitZones:
ifl_zone:
key:
type: identity
noBypassEmpty: true
includedKeys: ["", "bad-user1", "bad-user2", "very-bad-user*"]
inFlightLimit: 5
backlogLimit: 5
backlogTimeout: 30s
responseStatusCode: 429
responseRetryAfter: 60s
rules:
- routes:
- path: "/aaa"
methods: POST,PUT,DELETE
- path: "= /bbb"
inFlightLimits:
- zone: ifl_zone
`,
Func: func(t *testing.T, cfg *Config) {
// Included clients should be throttled.
for _, identity := range []string{"bad-user1", "bad-user2", "very-bad-user1", "very-bad-user777"} {
reqsGen := makeReqsGenerator(matchedRoutes)
reqsGenWithBasicAuth := func() *http.Request {
r := reqsGen()
r.SetBasicAuth(identity, identity+"-password")
return r
}
checkInFlightLimiting(t, cfg, checkInFlightLimitingParams{
reqsGen: reqsGenWithBasicAuth,
totalLimit: 10,
reqsNum: 30,
unblockDelay: time.Second,
wantRespCode: 429,
wantRetryAfter: time.Second * 60,
})
}
// Other clients should NOT be throttled.
reqsGen := makeReqsGenerator(matchedRoutes)
reqsGenWithBasicAuth := func() *http.Request {
r := reqsGen()
r.SetBasicAuth("good-user", "good-user-password")
return r
}
checkNoInFlightLimiting(t, cfg, reqsGenWithBasicAuth, 30, time.Second)
},
},
{
Name: "tags usage",
CfgData: `
rateLimitZones:
rl_zone:
rateLimit: 1/m
burstLimit: 10
responseStatusCode: 503
responseRetryAfter: 5s
rules:
- routes:
- path: "/aaa"
methods: POST, PUT,DELETE
- path: "= /bbb"
rateLimits:
- zone: rl_zone
tags: tag_a,tag_b
`,
Func: func(t *testing.T, cfg *Config) {
const burst = 10
reqsMatchGen := makeReqsGenerator(matchedPrefixedRoutes)
reqsMismatchGen := makeReqsGenerator(unmatchedPrefixedRoutes)
// No tags, all rules should be used.
checkRateLimiting(t, cfg, reqsMatchGen, burst+1, 30, 503, time.Second*5)
// Tags match.
checkRateLimiting(t, cfg, reqsMatchGen, burst+1, 30, 503, time.Second*5, "tag_a", "tag_c")
checkRateLimiting(t, cfg, reqsMatchGen, burst+1, 30, 503, time.Second*5, "tag_c", "tag_b")
// Tags mismatch.
checkNoRateLimiting(t, cfg, reqsMatchGen, 30, "tag_c", "tag_d")
// Tags match, routes mismatch.
checkNoRateLimiting(t, cfg, reqsMismatchGen, 30)
checkNoRateLimiting(t, cfg, reqsMismatchGen, 30, "tag_a")
},
},
{
Name: "tags usage (zone level)",
CfgData: `
rateLimitZones:
rl_zone1:
rateLimit: 1/m
burstLimit: 10
responseStatusCode: 503
responseRetryAfter: 5s
rl_zone2:
rateLimit: 2/m
burstLimit: 5
responseStatusCode: 429
responseRetryAfter: 10s
rules:
- routes:
- path: "/aaa"
methods: POST, PUT,DELETE
- path: "= /bbb"
rateLimits:
- zone: rl_zone1
tags: tag_a
- zone: rl_zone2
tags: tag_b
`,
Func: func(t *testing.T, cfg *Config) {
reqsMatchGen := makeReqsGenerator(matchedPrefixedRoutes)
// Tags match only rl_zone1 (tag_a).
checkRateLimiting(t, cfg, reqsMatchGen, 11, 30, 503, time.Second*5, "tag_a")
// Tags match only rl_zone2 (tag_b).
checkRateLimiting(t, cfg, reqsMatchGen, 6, 30, 429, time.Second*10, "tag_b")
// Tags mismatch - no rate limiting should be applied.
checkNoRateLimiting(t, cfg, reqsMatchGen, 30, "tag_c")
},
},
{
Name: "tags usage (rule-level tags take precedence)",
CfgData: `
rateLimitZones:
rl_zone_with_zone_tag:
rateLimit: 1/m
burstLimit: 10
responseStatusCode: 503
responseRetryAfter: 5s
rules:
- routes:
- path: "/aaa"
methods: POST, PUT,DELETE
- path: "= /bbb"
rateLimits:
- zone: rl_zone_with_zone_tag
tags: tag_zone_mismatch
tags: tag_rule
`,
Func: func(t *testing.T, cfg *Config) {
const burst = 10
reqsMatchGen := makeReqsGenerator(matchedPrefixedRoutes)
// Middleware provides "tag_rule" which matches rule-level tag.
// Zone should be included even though its zone-level tag "tag_zone_mismatch" doesn't match.
// This proves rule-level tag match takes precedence.
checkRateLimiting(t, cfg, reqsMatchGen, burst+1, 30, 503, time.Second*5, "tag_rule")
// Middleware provides "tag_zone_mismatch" which matches zone-level tag but NOT rule-level tag.
// Zone should be included because zone tag matches.
checkRateLimiting(t, cfg, reqsMatchGen, burst+1, 30, 503, time.Second*5, "tag_zone_mismatch")
// Middleware provides "tag_none" which matches neither rule nor zone tags.
// No zones should be included.
checkNoRateLimiting(t, cfg, reqsMatchGen, 30, "tag_none")
},
},
}
configLoader := config.NewLoader(config.NewViperAdapter())
for _, tt := range tests {
t.Run(tt.Name, func(t *testing.T) {
cfg := &Config{}
err := configLoader.LoadFromReader(bytes.NewReader([]byte(tt.CfgData)), config.DataTypeYAML, cfg)
require.NoError(t, err)
tt.Func(t, cfg)
})
}
}
type testCounters struct {
nextCalls atomic.Int32
rateLimitRejects atomic.Int32
rateLimitDryRunRejects atomic.Int32
rateLimitErrors atomic.Int32
inFlightLimitRejects atomic.Int32 //nolint
inFlightLimitDryRunRejects atomic.Int32 //nolint
inFlightLimitErrors atomic.Int32 //nolint
}
// nolint
func (c *testCounters) checkRateLimit(t *testing.T, wantRejects, wantDryRunRejects, wantErrors int) {
require.Equal(t, wantRejects, int(c.rateLimitRejects.Load()))
require.Equal(t, wantDryRunRejects, int(c.rateLimitDryRunRejects.Load()))
require.Equal(t, wantErrors, int(c.rateLimitErrors.Load()))
}
// nolint
func (c *testCounters) checkInFlightLimit(t *testing.T, wantRejects, wantDryRunRejects, wantErrors int) {
require.Equal(t, wantRejects, int(c.inFlightLimitRejects.Load()))
require.Equal(t, wantDryRunRejects, int(c.inFlightLimitDryRunRejects.Load()))
require.Equal(t, wantErrors, int(c.inFlightLimitErrors.Load()))
}
func makeHandlerWrappedIntoMiddleware(
cfg *Config, blockCh chan struct{}, tags []string, buildHandlerAtInit bool,
) (http.Handler, *testCounters, error) {
c := &testCounters{}
mw, err := MiddlewareWithOpts(cfg, testErrDomain, NewPrometheusMetrics(), MiddlewareOpts{
GetKeyIdentity: func(r *http.Request) (key string, bypass bool, err error) {
username, _, ok := r.BasicAuth()
if !ok {
return "", false, fmt.Errorf("no basic auth")
}
return username, false, nil
},
RateLimitOnReject: func(
rw http.ResponseWriter, r *http.Request, params middleware.RateLimitParams, next http.Handler, logger log.FieldLogger,
) {
c.rateLimitRejects.Inc()
middleware.DefaultRateLimitOnReject(rw, r, params, next, logger)
},
RateLimitOnRejectInDryRun: func(
rw http.ResponseWriter, r *http.Request, params middleware.RateLimitParams, next http.Handler, logger log.FieldLogger,
) {
c.rateLimitDryRunRejects.Inc()
middleware.DefaultRateLimitOnRejectInDryRun(rw, r, params, next, logger)
},
RateLimitOnError: func(
rw http.ResponseWriter, r *http.Request, params middleware.RateLimitParams, err error,
next http.Handler, logger log.FieldLogger,
) {
c.inFlightLimitErrors.Inc()
middleware.DefaultRateLimitOnError(rw, r, params, err, next, logger)
},
InFlightLimitOnReject: func(
rw http.ResponseWriter, r *http.Request, params middleware.InFlightLimitParams, next http.Handler, logger log.FieldLogger,
) {
c.inFlightLimitRejects.Inc()
middleware.DefaultInFlightLimitOnReject(rw, r, params, next, logger)
},
InFlightLimitOnRejectInDryRun: func(
rw http.ResponseWriter, r *http.Request, params middleware.InFlightLimitParams, next http.Handler, logger log.FieldLogger,
) {
c.inFlightLimitDryRunRejects.Inc()
middleware.DefaultInFlightLimitOnRejectInDryRun(rw, r, params, next, logger)
},
InFlightLimitOnError: func(
rw http.ResponseWriter, r *http.Request, params middleware.InFlightLimitParams, err error,
next http.Handler, logger log.FieldLogger,
) {
c.inFlightLimitErrors.Inc()
middleware.DefaultInFlightLimitOnError(rw, r, params, err, next, logger)
},
Tags: tags,
BuildHandlerAtInit: buildHandlerAtInit,
})
if err != nil {
return nil, nil, fmt.Errorf("create throttling middleware: %w", err)
}
return mw(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
c.nextCalls.Inc()
if blockCh != nil {
if err := waitSend(blockCh, time.Second*5); err != nil {
rw.WriteHeader(http.StatusInternalServerError)
return
}
}
rw.WriteHeader(http.StatusOK)
})), c, nil
}
// nolint
func checkRateLimiting(
t *testing.T,
cfg *Config,
reqsGen func() *http.Request,
wantNotThrottledReqsNum int,
totalReqsNum int,
wantRespCode int,
wantRetryAfter time.Duration,
tags ...string,
) {
if totalReqsNum < wantNotThrottledReqsNum {
panic("totalReqsNum should be > burst+1")
}
throttleHandler, counters, err := makeHandlerWrappedIntoMiddleware(cfg, nil, tags, false)
require.NoError(t, err)
// First N requests SHOULD NOT BE throttled.
for i := 0; i < wantNotThrottledReqsNum; i++ {
respRec := httptest.NewRecorder()
throttleHandler.ServeHTTP(respRec, reqsGen())
require.Equal(t, http.StatusOK, respRec.Code)
}
require.Equal(t, wantNotThrottledReqsNum, int(counters.nextCalls.Load()))
counters.checkRateLimit(t, 0, 0, 0)
// Next requests SHOULD BE throttled.
for i := wantNotThrottledReqsNum; i < totalReqsNum; i++ {
respRec := httptest.NewRecorder()
throttleHandler.ServeHTTP(respRec, reqsGen())
require.Equal(t, wantRespCode, respRec.Code)
retryAfterSecs, err := strconv.Atoi(respRec.Header().Get("Retry-After"))
require.NoError(t, err)
require.Equal(t, wantRetryAfter, time.Duration(retryAfterSecs)*time.Second)
}
require.Equal(t, wantNotThrottledReqsNum, int(counters.nextCalls.Load())) // Not changed.
counters.checkRateLimit(t, totalReqsNum-wantNotThrottledReqsNum, 0, 0)
}
func checkRateLimitingInDryRun(t *testing.T, cfg *Config, reqsGen func() *http.Request, burst, reqsNum int, tags ...string) {
if reqsNum <= burst+1 {
panic("reqsNum should be > burst+1")
}
checkNoRateLimitingOrDryRun(t, cfg, reqsGen, reqsNum, reqsNum-burst-1, tags...)
}
func checkNoRateLimiting(t *testing.T, cfg *Config, reqsGen func() *http.Request, reqsNum int, tags ...string) {
checkNoRateLimitingOrDryRun(t, cfg, reqsGen, reqsNum, 0, tags...)
}
func checkNoRateLimitingOrDryRun(
t *testing.T, cfg *Config, reqsGen func() *http.Request, reqsNum, wantDryRunRejects int, tags ...string,
) {
throttleHandler, counters, err := makeHandlerWrappedIntoMiddleware(cfg, nil, tags, false)
require.NoError(t, err)
for i := 0; i < reqsNum; i++ {
respRec := httptest.NewRecorder()
throttleHandler.ServeHTTP(respRec, reqsGen())
require.Equal(t, http.StatusOK, respRec.Code)
}
require.Equal(t, reqsNum, int(counters.nextCalls.Load()))
counters.checkRateLimit(t, 0, wantDryRunRejects, 0)
}
type checkInFlightLimitingParams struct {
reqsGen func() *http.Request
totalLimit int
reqsNum int
unblockDelay time.Duration
wantRespCode int
wantRetryAfter time.Duration
tags []string
buildHandlerAtInit bool
}
func checkInFlightLimiting(t *testing.T, cfg *Config, params checkInFlightLimitingParams) {
if params.reqsNum <= params.totalLimit {
panic("reqsNum should be > totalLimit")
}
blockCh := make(chan struct{})
throttleHandler, counters, err := makeHandlerWrappedIntoMiddleware(cfg, blockCh, params.tags, params.buildHandlerAtInit)
require.NoError(t, err)
var okCodes, throttledCodes, unexpectedCodes, wrongRetryAfterNums atomic.Int32
var wg sync.WaitGroup
for i := 0; i < params.reqsNum; i++ {
wg.Add(1)
go func() {
defer wg.Done()
respRec := httptest.NewRecorder()
throttleHandler.ServeHTTP(respRec, params.reqsGen())
switch respRec.Code {
case http.StatusOK:
okCodes.Inc()
case params.wantRespCode:
throttledCodes.Inc()
gotRetryAfterSecs, err := strconv.Atoi(respRec.Header().Get("Retry-After"))
if err != nil || time.Duration(gotRetryAfterSecs)*time.Second != params.wantRetryAfter {
wrongRetryAfterNums.Inc()
}
default:
unexpectedCodes.Inc()
}
}()
}
time.Sleep(params.unblockDelay)
for i := 0; i < params.totalLimit; i++ {
require.NoError(t, waitRecv(blockCh, time.Second*5))
}
wg.Wait()
require.Equal(t, params.totalLimit, int(counters.nextCalls.Load()))
require.Equal(t, params.totalLimit, int(okCodes.Load()))
require.Equal(t, params.reqsNum-params.totalLimit, int(throttledCodes.Load()))
require.Equal(t, 0, int(wrongRetryAfterNums.Load()))
require.Equal(t, 0, int(unexpectedCodes.Load()))
counters.checkInFlightLimit(t, params.reqsNum-params.totalLimit, 0, 0)
}
func checkInFlightLimitingInDryRun(
t *testing.T,
cfg *Config,
reqsGen func() *http.Request,
totalLimit int,
reqsNum int,
unblockDelay time.Duration,
tags ...string,
) {
if reqsNum <= totalLimit {
panic("reqsNum should be > totalLimit")
}
checkNoInFlightLimitingOrDryRun(t, cfg, reqsGen, reqsNum, unblockDelay, reqsNum-totalLimit, tags...)
}
//nolint:unparam
func checkNoInFlightLimiting(
t *testing.T, cfg *Config, reqsGen func() *http.Request, reqsNum int, unblockDelay time.Duration, tags ...string,
) {
checkNoInFlightLimitingOrDryRun(t, cfg, reqsGen, reqsNum, unblockDelay, 0, tags...)
}
func checkNoInFlightLimitingOrDryRun(
t *testing.T,
cfg *Config,
reqsGen func() *http.Request,
reqsNum int,
unblockDelay time.Duration,
wantDryRunRejects int,
tags ...string,
) {
blockCh := make(chan struct{})
throttleHandler, counters, err := makeHandlerWrappedIntoMiddleware(cfg, blockCh, tags, false)
require.NoError(t, err)
var okCodes, unexpectedCodes atomic.Int32
var wg sync.WaitGroup
for i := 0; i < reqsNum; i++ {
wg.Add(1)
go func() {
defer wg.Done()
respRec := httptest.NewRecorder()
throttleHandler.ServeHTTP(respRec, reqsGen())
switch respRec.Code {
case http.StatusOK:
okCodes.Inc()
default:
unexpectedCodes.Inc()
}
}()
}
time.Sleep(unblockDelay)
for i := 0; i < reqsNum; i++ {
require.NoError(t, waitRecv(blockCh, time.Second*5))
}
wg.Wait()
require.Equal(t, reqsNum, int(counters.nextCalls.Load()))
require.Equal(t, reqsNum, int(okCodes.Load()))
require.Equal(t, 0, int(unexpectedCodes.Load()))
counters.checkInFlightLimit(t, 0, wantDryRunRejects, 0)
}
func makeReqsGenerator(strReqs []string) func() *http.Request {
var i atomic.Int32
return func() *http.Request {
j := int(i.Inc()) - 1
reqParts := strings.SplitN(strReqs[j%len(strReqs)], " ", 2)
return httptest.NewRequest(reqParts[0], reqParts[1], http.NoBody)
}
}
func makeStrsGenerator(strs []string) func() string {
var i atomic.Int32
return func() string {
j := int(i.Inc()) - 1
res := strs[j%len(strs)]
return res
}
}
func makeFmtGenerator(format string) func() string {
var i atomic.Int32
return func() string {
j := int(i.Inc()) - 1
res := fmt.Sprintf(format, j)
return res
}
}
func waitRecv(ch chan struct{}, timeout time.Duration) error {
select {
case <-ch:
case <-time.After(timeout):
return fmt.Errorf("receive from channel: timeout %s exceeded", timeout)
}
return nil
}
func waitSend(ch chan struct{}, timeout time.Duration) error {
select {
case ch <- struct{}{}:
case <-time.After(timeout):
return fmt.Errorf("send to channel: timeout %s exceeded", timeout)
}
return nil
}