ratelimit

package
v1.30.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 29, 2020 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewInFlightRequestLimitMiddleware

func NewInFlightRequestLimitMiddleware(limit refreshable.Int, matches MatchFunc, healthcheck reporter.HealthComponent) wrouter.RouteHandlerMiddleware

NewInFlightRequestLimitMiddleware returns a middleware which counts and limits the number of inflight requests that match the provided MatchFunc filter. If MatchFunc is nil, it will match all requests. When the number of active matched requests exceeds the limit, the middleware returns StatusTooManyRequests (429).

If healthcheck is non-nil, it will be set to REPAIRING when the middleware is throttling and HEALTHY when the current counter falls below the limit. It is initialized to HEALTHY.

If limit is ever negative it will be treated as a 0, i.e. all requests will be throttled.

TODO: We should set the Retry-After header based on how many requests we're rejecting.

Maybe enqueue requests in a channel for a few seconds in case other requests return quickly?

Types

type MatchFunc

type MatchFunc func(req *http.Request, vals wrouter.RequestVals) bool
var MatchMutating MatchFunc = func(req *http.Request, vals wrouter.RequestVals) bool {
	return req.Method == http.MethodPost || req.Method == http.MethodPut || req.Method == http.MethodDelete || req.Method == http.MethodPatch
}
var MatchReadOnly MatchFunc = func(req *http.Request, vals wrouter.RequestVals) bool {
	return req.Method == http.MethodGet || req.Method == http.MethodHead || req.Method == http.MethodOptions
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL