guard

package
v1.4.0 Latest Latest
Warning

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

Go to latest
Published: Feb 5, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MaxBody

func MaxBody(maxBytes int64) func(http.Handler) http.Handler

MaxBody returns middleware that rejects requests with a body exceeding maxBytes with 413 Payload Too Large.

func RateLimit

func RateLimit(cfg RateLimitConfig) func(http.Handler) http.Handler

RateLimit returns middleware enforcing per-key rate limiting with token bucket.

func Timeout

func Timeout(d time.Duration) func(http.Handler) http.Handler

Timeout returns middleware that sets a context deadline on the request and actively returns 504 Gateway Timeout if the handler does not complete before the deadline fires. If the caller already set a tighter deadline, the tighter deadline wins and no new deadline is applied.

Types

type KeyFunc

type KeyFunc func(r *http.Request) string

KeyFunc extracts a rate limit key from an HTTP request.

func HeaderKey

func HeaderKey(header string) KeyFunc

HeaderKey returns a KeyFunc using the value of a request header as the key. Falls back to RemoteAddr if the header is absent.

func RemoteAddr

func RemoteAddr() KeyFunc

RemoteAddr returns a KeyFunc that uses the request's RemoteAddr (without port).

func XForwardedFor

func XForwardedFor(trustedCIDRs ...string) KeyFunc

XForwardedFor returns a KeyFunc that reads the client IP from X-Forwarded-For, but only if RemoteAddr is within a trusted CIDR range. Falls back to RemoteAddr if untrusted or if the X-Forwarded-For value is not a valid IP address.

type RateLimitConfig

type RateLimitConfig struct {
	Rate    int
	Window  time.Duration
	KeyFunc KeyFunc // REQUIRED
}

RateLimitConfig configures the rate limiter.

Jump to

Keyboard shortcuts

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