pub struct RateLimitLayer<B: RateLimitBackend> { /* private fields */ }tower only.Expand description
Tower layer that applies rate limiting to inner services.
By default the key is the client’s socket address; forwarded
headers are only believed for peers listed in
ClientIpConfig::trusted_proxies (see
RateLimitLayer::with_client_ip). The default is secure by
default: an unconfigured layer ignores X-Forwarded-For entirely.
For axum, serve the router with
.into_make_service_with_connect_info::<SocketAddr>() so the peer
address is available; without it the
MissingClientPolicy applies (fail-closed 503 by default).
Implementations§
Source§impl<B: RateLimitBackend> RateLimitLayer<B>
impl<B: RateLimitBackend> RateLimitLayer<B>
Sourcepub fn new(quota: Quota, backend: B) -> Self
pub fn new(quota: Quota, backend: B) -> Self
Create a layer that applies quota through the given backend.
Identity defaults to the client’s socket address with an empty
trusted-proxy list (forwarded headers ignored) and
MissingClientPolicy::Reject when the peer is unknown.
Sourcepub fn with_client_ip(self, config: ClientIpConfig) -> Self
pub fn with_client_ip(self, config: ClientIpConfig) -> Self
Trust X-Forwarded-For (or a ClientIpConfig::trusted_header
override) only for the given proxy networks, using the
right-to-left hop walk described there. REQ-THROTTLE-100/101.
Sourcepub fn with_missing_client_policy(self, policy: MissingClientPolicy) -> Self
pub fn with_missing_client_policy(self, policy: MissingClientPolicy) -> Self
Set the policy for requests whose client identity cannot be
resolved (no ConnectInfo extension). REQ-THROTTLE-103.
Only meaningful for client-IP identity.
Sourcepub fn with_key_extractor(self, extractor: KeyExtractor) -> Self
pub fn with_key_extractor(self, extractor: KeyExtractor) -> Self
Key requests by a custom extractor instead of client IP (API key, tenant id, …).
Trait Implementations§
Source§impl<B: Clone + RateLimitBackend> Clone for RateLimitLayer<B>
impl<B: Clone + RateLimitBackend> Clone for RateLimitLayer<B>
Source§fn clone(&self) -> RateLimitLayer<B>
fn clone(&self) -> RateLimitLayer<B>
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more