Expand description
Identity-aware rate limiting middleware.
Requests are keyed by authenticated identity (API key ID or user ID) when available, falling back to the client IP address. Each key gets a fixed-window counter that resets every 60 seconds.
The client IP is the address of the TCP peer, so the server must be
served with into_make_service_with_connect_info::<SocketAddr>().
The configured forwarding header (TrustedProxies::header) is read
only when that peer is one of the TrustedProxies: any client can
write it. Without a peer address the IP limit is skipped.
With AccountLimit::ByEmail, a request whose JSON body names an
email is also counted against that account, so spreading attempts on
one account over many IP addresses does not help.
Responses carry standard rate-limit headers:
X-RateLimit-Limit– maximum requests allowed per windowX-RateLimit-Remaining– requests left in the current windowX-RateLimit-Reset– epoch timestamp when the window resets
A request is counted under an API key only once its secret is verified,
and the key’s rate_limit_override replaces the server-wide default only
when the key is owned by an admin. Secrets not yet verified are checked
at most KEY_CHECKS_PER_MINUTE times per client and key each minute.
Structs§
- Invalid
Client IpHeader - A
ClientIpHeadername that is neitherx-forwarded-fornorx-real-ip. - Invalid
Trusted Proxy - A
TrustedProxiesentry that is neither an IP address nor a CIDR range. - Rate
Limit Context - Context needed by the rate limit middleware.
- Rate
Limit State - Shared rate limiter state with fixed-window counters.
- Trusted
Proxies - Reverse proxies whose forwarding header is believed, and which header that is.
Enums§
- Account
Limit - Whether the limiter also counts requests against the account they target.
- Client
IpHeader - The forwarding header a trusted proxy reports the client IP in.
- Rate
Limit Key - Rate limit key: identity-based when authenticated, IP-based otherwise.
Functions§
- per_
minute - Build a per-identity rate limiter allowing
requests_per_minuterequests per minute. - rate_
limit - Axum middleware that enforces identity-aware rate limiting.