forked from acronis/go-appkit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.go
More file actions
475 lines (411 loc) · 16.9 KB
/
Copy pathmiddleware.go
File metadata and controls
475 lines (411 loc) · 16.9 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
/*
Copyright © 2024 Acronis International GmbH.
Released under MIT license.
*/
package throttle
import (
"fmt"
"net"
"net/http"
"strings"
"time"
"github.com/vasayxtx/go-glob"
"github.com/acronis/go-appkit/httpserver/middleware"
"github.com/acronis/go-appkit/internal/throttleconfig"
"github.com/acronis/go-appkit/log"
"github.com/acronis/go-appkit/restapi"
)
// RuleLogFieldName is a logged field that contains the name of the throttling rule.
const RuleLogFieldName = "throttle_rule"
// MiddlewareOpts represents an options for Middleware.
type MiddlewareOpts struct {
// GetKeyIdentity is a function that returns identity string representation.
// The returned string is used as a key for zone when key.type is "identity".
GetKeyIdentity func(r *http.Request) (key string, bypass bool, err error)
// RateLimitOnReject is a callback called for rejecting HTTP request when the rate limit is exceeded.
RateLimitOnReject middleware.RateLimitOnRejectFunc
// RateLimitOnRejectInDryRun is a callback called for rejecting HTTP request in the dry-run mode
// when the rate limit is exceeded.
RateLimitOnRejectInDryRun middleware.RateLimitOnRejectFunc
// RateLimitOnError is a callback called in case of any error that may occur during the rate limiting.
RateLimitOnError middleware.RateLimitOnErrorFunc
// InFlightLimitOnReject is a callback called for rejecting HTTP request when the in-flight limit is exceeded.
InFlightLimitOnReject middleware.InFlightLimitOnRejectFunc
// RateLimitOnRejectInDryRun is a callback called for rejecting HTTP request in the dry-run mode
// when the in-flight limit is exceeded.
InFlightLimitOnRejectInDryRun middleware.InFlightLimitOnRejectFunc
// RateLimitOnError is a callback called in case of any error that may occur during the in-flight limiting.
InFlightLimitOnError middleware.InFlightLimitOnErrorFunc
// Tags is a list of tags for filtering throttling rules from the config. If it's empty, all rules can be applied.
Tags []string
// BuildHandlerAtInit determines where the final handler will be constructed.
// If true, it will be done at the initialization step (i.e., in the constructor),
// false (default) - right in the ServeHTTP() method (gorilla/mux case).
BuildHandlerAtInit bool
}
// RateLimitOpts returns options for constructing rate limiting middleware.
func (opts MiddlewareOpts) RateLimitOpts() RateLimitMiddlewareOpts {
return RateLimitMiddlewareOpts{
GetKeyIdentity: opts.GetKeyIdentity,
RateLimitOnReject: opts.RateLimitOnReject,
RateLimitOnRejectInDryRun: opts.RateLimitOnRejectInDryRun,
RateLimitOnError: opts.RateLimitOnError,
}
}
// InFlightLimitOpts returns options for constructing in-flight limiting middleware.
func (opts MiddlewareOpts) InFlightLimitOpts() InFlightLimitMiddlewareOpts {
return InFlightLimitMiddlewareOpts{
GetKeyIdentity: opts.GetKeyIdentity,
InFlightLimitOnReject: opts.InFlightLimitOnReject,
InFlightLimitOnRejectInDryRun: opts.InFlightLimitOnRejectInDryRun,
InFlightLimitOnError: opts.InFlightLimitOnError,
}
}
// Middleware is a middleware that throttles incoming HTTP requests based on the passed configuration.
func Middleware(cfg *Config, errDomain string, mc MetricsCollector) (func(next http.Handler) http.Handler, error) {
return MiddlewareWithOpts(cfg, errDomain, mc, MiddlewareOpts{})
}
// MiddlewareWithOpts is a more configurable version of Middleware.
func MiddlewareWithOpts(
cfg *Config, errDomain string, mc MetricsCollector, opts MiddlewareOpts,
) (func(next http.Handler) http.Handler, error) {
if mc == nil {
mc = disabledMetrics{}
}
routes, err := makeRoutes(cfg, errDomain, mc, opts)
if err != nil {
return nil, err
}
if opts.BuildHandlerAtInit {
return func(next http.Handler) http.Handler {
for i := range routes {
route := &routes[i]
route.Handler = next
for j := len(route.Middlewares) - 1; j >= 0; j-- {
route.Handler = route.Middlewares[j](route.Handler)
}
}
return &handler{next: next, routesManager: restapi.NewRoutesManager(routes)}
}, nil
}
return func(next http.Handler) http.Handler {
return &handler{next: next, routesManager: restapi.NewRoutesManager(routes)}
}, nil
}
type handler struct {
next http.Handler
routesManager *restapi.RoutesManager
}
func (h *handler) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
matchedRoute, ok := h.routesManager.SearchMatchedRouteForRequest(r)
if !ok {
h.next.ServeHTTP(rw, r)
return
}
if matchedRoute.Handler != nil {
matchedRoute.Handler.ServeHTTP(rw, r)
return
}
// We build a final handler here and not in the constructor because it is how the gorilla/mux works:
// all middlewares apply only after the matched route is found
// (https://github.com/gorilla/mux/blob/d07530f46e1eec4e40346e24af34dcc6750ad39f/mux.go#L138-L146).
nextHandler := h.next
for i := len(matchedRoute.Middlewares) - 1; i >= 0; i-- {
nextHandler = matchedRoute.Middlewares[i](nextHandler)
}
nextHandler.ServeHTTP(rw, r)
}
//nolint:gocyclo,dupl // high cyclomatic complexity and some duplication is ok here for high functional cohesion and clarity
func makeRoutes(
cfg *Config, errDomain string, mc MetricsCollector, opts MiddlewareOpts,
) (routes []restapi.Route, err error) {
for _, rule := range cfg.Rules {
if len(rule.RateLimits) == 0 && len(rule.InFlightLimits) == 0 {
continue
}
ruleTagsMatch := throttleconfig.CheckStringSlicesIntersect(opts.Tags, rule.Tags)
var middlewares []func(http.Handler) http.Handler
// Build in-flight limiting middleware.
for i := 0; i < len(rule.InFlightLimits); i++ {
if len(opts.Tags) != 0 && !ruleTagsMatch &&
!throttleconfig.CheckStringSlicesIntersect(opts.Tags, rule.InFlightLimits[i].Tags) {
continue
}
zoneName := rule.InFlightLimits[i].Zone
cfgZone, ok := cfg.InFlightLimitZones[zoneName]
if !ok {
return nil, fmt.Errorf("in-flight zone %q is not defined", zoneName)
}
var inFlightLimitMw func(next http.Handler) http.Handler
inFlightLimitMw, err = InFlightLimitMiddlewareWithOpts(
&cfgZone, errDomain, rule.Name(), mc, opts.InFlightLimitOpts())
if err != nil {
return nil, fmt.Errorf("make in-flight limit middleware for zone %q: %w", zoneName, err)
}
middlewares = append(middlewares, inFlightLimitMw)
}
// Build rate limiting middleware.
for i := 0; i < len(rule.RateLimits); i++ {
if len(opts.Tags) != 0 && !ruleTagsMatch &&
!throttleconfig.CheckStringSlicesIntersect(opts.Tags, rule.RateLimits[i].Tags) {
continue
}
zoneName := rule.RateLimits[i].Zone
cfgZone, ok := cfg.RateLimitZones[zoneName]
if !ok {
return nil, fmt.Errorf("rate limit zone %q is not defined", zoneName)
}
var rateLimitMw func(next http.Handler) http.Handler
rateLimitMw, err = RateLimitMiddlewareWithOpts(
&cfgZone, errDomain, rule.Name(), mc, opts.RateLimitOpts())
if err != nil {
return nil, fmt.Errorf("make rate limit middleware for zone %q: %w", zoneName, err)
}
middlewares = append(middlewares, rateLimitMw)
}
// Skip the rule if no middlewares were added after filtering by tags
if len(middlewares) == 0 {
continue
}
for _, cfgRoute := range rule.Routes {
routes = append(routes, restapi.NewRoute(cfgRoute, nil, middlewares))
}
for _, exclCfgRoute := range rule.ExcludedRoutes {
routes = append(routes, restapi.NewExcludedRoute(exclCfgRoute))
}
}
return routes, nil
}
// RateLimitMiddlewareOpts represents an options for RateLimitMiddleware.
type RateLimitMiddlewareOpts struct {
// GetKeyIdentity is a function that returns identity string representation.
// The returned string is used as a key for zone when key.type is "identity".
GetKeyIdentity func(r *http.Request) (key string, bypass bool, err error)
// RateLimitOnReject is a callback that is called for rejecting HTTP request when the rate limit is exceeded.
RateLimitOnReject middleware.RateLimitOnRejectFunc
// RateLimitOnRejectInDryRun is a callback that is called for rejecting HTTP request in the dry-run mode
// when the rate limit is exceeded.
RateLimitOnRejectInDryRun middleware.RateLimitOnRejectFunc
// RateLimitOnError is a callback that is called in case of any error that may occur during the rate limiting.
RateLimitOnError middleware.RateLimitOnErrorFunc
}
// RateLimitMiddleware is a middleware that performs rate limiting based on the passed configuration.
func RateLimitMiddleware(
cfg *RateLimitZoneConfig, errDomain string, ruleName string, mc MetricsCollector,
) (func(next http.Handler) http.Handler, error) {
return RateLimitMiddlewareWithOpts(cfg, errDomain, ruleName, mc, RateLimitMiddlewareOpts{})
}
// RateLimitMiddlewareWithOpts is a more configurable version of RateLimitMiddleware.
func RateLimitMiddlewareWithOpts(
cfg *RateLimitZoneConfig, errDomain string, ruleName string, mc MetricsCollector, opts RateLimitMiddlewareOpts,
) (func(next http.Handler) http.Handler, error) {
var alg middleware.RateLimitAlg
switch cfg.Alg {
case "", RateLimitAlgLeakyBucket:
alg = middleware.RateLimitAlgLeakyBucket
case RateLimitAlgSlidingWindow:
alg = middleware.RateLimitAlgSlidingWindow
default:
return nil, fmt.Errorf("unknown rate limit alg %q", cfg.Alg)
}
if cfg.Key.Type == ZoneKeyTypeIdentity && opts.GetKeyIdentity == nil {
return nil, fmt.Errorf("GetKeyIdentity is required for identity key type")
}
getKey, err := makeGetKeyFunc(cfg.Key, opts.GetKeyIdentity, cfg.ExcludedKeys, cfg.IncludedKeys)
if err != nil {
return nil, err
}
var getRetryAfter func(r *http.Request, estimatedTime time.Duration) time.Duration
switch {
case cfg.ResponseRetryAfter.IsAuto:
getRetryAfter = middleware.GetRetryAfterEstimatedTime
case cfg.ResponseRetryAfter.Duration == 0:
getRetryAfter = nil
default:
getRetryAfter = func(_ *http.Request, _ time.Duration) time.Duration {
return cfg.ResponseRetryAfter.Duration
}
}
onReject := opts.RateLimitOnReject
if onReject == nil {
onReject = middleware.DefaultRateLimitOnReject
}
onRejectWithMetrics := func(
rw http.ResponseWriter, r *http.Request, params middleware.RateLimitParams, next http.Handler, logger log.FieldLogger,
) {
mc.IncRateLimitRejects(ruleName, false)
if logger != nil {
logger = logger.With(log.String(RuleLogFieldName, ruleName))
}
onReject(rw, r, params, next, logger)
}
onRejectInDryRun := opts.RateLimitOnRejectInDryRun
if onRejectInDryRun == nil {
onRejectInDryRun = middleware.DefaultRateLimitOnRejectInDryRun
}
onRejectInDryRunWithMetrics := func(
rw http.ResponseWriter, r *http.Request, params middleware.RateLimitParams, next http.Handler, logger log.FieldLogger,
) {
mc.IncRateLimitRejects(ruleName, true)
if logger != nil {
logger = logger.With(log.String(RuleLogFieldName, ruleName))
}
onRejectInDryRun(rw, r, params, next, logger)
}
rate := middleware.Rate{Count: cfg.RateLimit.Count, Duration: cfg.RateLimit.Duration}
return middleware.RateLimitWithOpts(rate, errDomain, middleware.RateLimitOpts{
Alg: alg,
MaxBurst: cfg.BurstLimit,
GetKey: getKey,
MaxKeys: cfg.MaxKeys,
BacklogLimit: cfg.BacklogLimit,
BacklogTimeout: time.Duration(cfg.BacklogTimeout),
ResponseStatusCode: cfg.getResponseStatusCode(),
GetRetryAfter: getRetryAfter,
DryRun: cfg.DryRun,
OnReject: onRejectWithMetrics,
OnRejectInDryRun: onRejectInDryRunWithMetrics,
OnError: opts.RateLimitOnError,
})
}
type InFlightLimitMiddlewareOpts struct {
// GetKeyIdentity is a function that returns identity string representation.
// The returned string is used as a key for zone when key.type is "identity".
GetKeyIdentity func(r *http.Request) (key string, bypass bool, err error)
// InFlightLimitOnReject is a callback that is called for rejecting HTTP request when the in-flight limit is exceeded.
InFlightLimitOnReject middleware.InFlightLimitOnRejectFunc
// RateLimitOnRejectInDryRun is a callback that is called for rejecting HTTP request in the dry-run mode
// when the in-flight limit is exceeded.
InFlightLimitOnRejectInDryRun middleware.InFlightLimitOnRejectFunc
// RateLimitOnError is a callback that is called in case of any error that may occur during the in-flight limiting.
InFlightLimitOnError middleware.InFlightLimitOnErrorFunc
}
// InFlightLimitMiddleware is a middleware that performs in-flight limiting based on the passed configuration.
func InFlightLimitMiddleware(
cfg *InFlightLimitZoneConfig, errDomain string, ruleName string, mc MetricsCollector,
) (func(next http.Handler) http.Handler, error) {
return InFlightLimitMiddlewareWithOpts(cfg, errDomain, ruleName, mc, InFlightLimitMiddlewareOpts{})
}
// InFlightLimitMiddlewareWithOpts is a more configurable version of InFlightLimitMiddleware.
func InFlightLimitMiddlewareWithOpts(
cfg *InFlightLimitZoneConfig, errDomain string, ruleName string, mc MetricsCollector, opts InFlightLimitMiddlewareOpts,
) (func(next http.Handler) http.Handler, error) {
if cfg.Key.Type == ZoneKeyTypeIdentity && opts.GetKeyIdentity == nil {
return nil, fmt.Errorf("GetKeyIdentity is required for identity key type")
}
getKey, err := makeGetKeyFunc(cfg.Key, opts.GetKeyIdentity, cfg.ExcludedKeys, cfg.IncludedKeys)
if err != nil {
return nil, err
}
var getRetryAfter func(r *http.Request) time.Duration
if cfg.ResponseRetryAfter != 0 {
getRetryAfter = func(_ *http.Request) time.Duration {
return time.Duration(cfg.ResponseRetryAfter)
}
}
onReject := opts.InFlightLimitOnReject
if onReject == nil {
onReject = middleware.DefaultInFlightLimitOnReject
}
onRejectWithMetrics := func(
rw http.ResponseWriter, r *http.Request, params middleware.InFlightLimitParams, next http.Handler, logger log.FieldLogger,
) {
mc.IncInFlightLimitRejects(ruleName, false, params.RequestBacklogged)
if logger != nil {
logger = logger.With(log.String(RuleLogFieldName, ruleName))
}
onReject(rw, r, params, next, logger)
}
onRejectInDryRun := opts.InFlightLimitOnRejectInDryRun
if onRejectInDryRun == nil {
onRejectInDryRun = middleware.DefaultInFlightLimitOnRejectInDryRun
}
onRejectInDryRunWithMetrics := func(
rw http.ResponseWriter, r *http.Request, params middleware.InFlightLimitParams, next http.Handler, logger log.FieldLogger,
) {
mc.IncInFlightLimitRejects(ruleName, true, params.RequestBacklogged)
if logger != nil {
logger = logger.With(log.String(RuleLogFieldName, ruleName))
}
onRejectInDryRun(rw, r, params, next, logger)
}
return middleware.InFlightLimitWithOpts(cfg.InFlightLimit, errDomain, middleware.InFlightLimitOpts{
GetKey: getKey,
MaxKeys: cfg.MaxKeys,
ResponseStatusCode: cfg.getResponseStatusCode(),
GetRetryAfter: getRetryAfter,
BacklogLimit: cfg.BacklogLimit,
BacklogTimeout: time.Duration(cfg.BacklogTimeout),
DryRun: cfg.DryRun,
OnReject: onRejectWithMetrics,
OnRejectInDryRun: onRejectInDryRunWithMetrics,
OnError: opts.InFlightLimitOnError,
})
}
//nolint:gocyclo // we would like to have high functional cohesion here.
func makeGetKeyFunc(
cfg ZoneKeyConfig,
getKeyIdentity func(r *http.Request) (string, bool, error),
excludedKeys []string,
includedKeys []string,
) (func(r *http.Request) (string, bool, error), error) {
makeByType := func() (func(r *http.Request) (string, bool, error), error) {
switch cfg.Type {
case ZoneKeyTypeIdentity:
return getKeyIdentity, nil
case ZoneKeyTypeHTTPHeader:
return func(r *http.Request) (string, bool, error) {
headerVal := strings.TrimSpace(r.Header.Get(cfg.HeaderName))
if cfg.NoBypassEmpty {
return headerVal, false, nil
}
return headerVal, headerVal == "", nil
}, nil
case ZoneKeyTypeRemoteAddr:
return func(r *http.Request) (string, bool, error) {
host, _, err := net.SplitHostPort(r.RemoteAddr)
return host, false, err
}, nil
case ZoneKeyTypeNoKey:
return nil, nil
}
return nil, fmt.Errorf("unknown key type %q", cfg.Type)
}
getKey, err := makeByType()
if err != nil || getKey == nil {
return nil, err
}
if len(excludedKeys) == 0 && len(includedKeys) == 0 {
return getKey, nil
}
if len(excludedKeys) != 0 && len(includedKeys) != 0 {
return nil, fmt.Errorf("excluded and included keys cannot be used together")
}
makeWithPredefinedKeys := func(keys []string, exclude bool) func(r *http.Request) (string, bool, error) {
compiledKeys := make([]func(s string) bool, 0, len(keys))
for _, key := range keys {
compiledKeys = append(compiledKeys, glob.Compile(key))
}
return func(r *http.Request) (string, bool, error) {
key, bypass, getKeyErr := getKey(r)
if getKeyErr != nil {
return key, bypass, getKeyErr
}
if bypass {
return key, bypass, nil
}
keyFound := false
for i := range compiledKeys {
if compiledKeys[i](key) {
keyFound = true
break
}
}
return key, keyFound == exclude, nil
}
}
if len(excludedKeys) != 0 {
return makeWithPredefinedKeys(excludedKeys, true), nil
}
return makeWithPredefinedKeys(includedKeys, false), nil
}