Documentation
¶
Overview ¶
Package middleware provides HTTP middleware for the limited rate limiter
Index ¶
Constants ¶
View Source
const (
// IdentifierKey is the context key for the rate limit identifier
IdentifierKey contextKey = "rate_limit_identifier"
)
Variables ¶
This section is empty.
Functions ¶
func Middleware ¶
func Middleware(opts Options) func(http.HandlerFunc) http.HandlerFunc
Middleware creates an HTTP middleware for rate limiting
Types ¶
type Options ¶
type Options struct {
// Limiter is the rate limiter to use
Limiter limited.RateLimiter
// Logger for debugging and error logging (optional)
Logger *zap.Logger
// ExtractIdentifier extracts the identifier from the request
// If not provided, defaults to using IP address
ExtractIdentifier func(r *http.Request) string
// ExtractResource extracts the resource from the request
// If not provided, defaults to using the URL path
ExtractResource func(r *http.Request) string
// ExtractOperation extracts the operation from the request
// If not provided, defaults to using the HTTP method
ExtractOperation func(r *http.Request) string
// ErrorHandler handles rate limit errors
// If not provided, defaults to returning 429 Too Many Requests
ErrorHandler func(w http.ResponseWriter, r *http.Request, decision *limited.LimitDecision)
// OnSuccess is called when a request is allowed
OnSuccess func(r *http.Request, decision *limited.LimitDecision)
// OnRateLimit is called when a request is rate limited
OnRateLimit func(r *http.Request, decision *limited.LimitDecision)
// SkipRequest determines if a request should skip rate limiting
SkipRequest func(r *http.Request) bool
}
Options configures the rate limiting middleware
Click to show internal directories.
Click to hide internal directories.