middleware

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jun 12, 2025 License: MIT Imports: 6 Imported by: 0

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

func WithIdentifier

func WithIdentifier(r *http.Request, identifier string) *http.Request

WithIdentifier adds an identifier to the request context

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

Jump to

Keyboard shortcuts

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