Skip to main content

Module rate_limit

Module rate_limit 

Source
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>(). X-Forwarded-For and X-Real-IP are read only when that peer is one of the TrustedProxies: any client can write those headers.

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 window
  • X-RateLimit-Remaining – requests left in the current window
  • X-RateLimit-Reset – epoch timestamp when the window resets

API keys with a rate_limit_override use that value instead of the server-wide default.

Structs§

InvalidTrustedProxy
A TrustedProxies entry that is neither an IP address nor a CIDR range.
RateLimitContext
Context needed by the rate limit middleware.
RateLimitState
Shared rate limiter state with fixed-window counters.
TrustedProxies
Reverse proxies whose X-Forwarded-For and X-Real-IP headers are believed.

Enums§

AccountLimit
Whether the limiter also counts requests against the account they target.
RateLimitKey
Rate limit key: identity-based when authenticated, IP-based otherwise.

Functions§

per_minute
Build a per-identity rate limiter allowing requests_per_minute requests per minute.
rate_limit
Axum middleware that enforces identity-aware rate limiting.