#[non_exhaustive]pub enum RateLimiter {
InMemory(InMemoryRateLimiter),
}Expand description
Rate limiter that dispatches to either an in-memory or Redis backend.
Construct via RateLimiter::new (in-memory, default) or
RateLimiter::new_redis (distributed Redis, requires the
redis-rate-limiting Cargo feature).
Variants (Non-exhaustive)§
This enum is marked as non-exhaustive
InMemory(InMemoryRateLimiter)
Single-node token-bucket limiter backed by HashMap with RwLock.
Implementations§
Source§impl RateLimiter
impl RateLimiter
Sourcepub fn new(config: RateLimitConfig) -> Self
pub fn new(config: RateLimitConfig) -> Self
Create an in-memory rate limiter.
Sourcepub fn with_path_rules_from_security(
self,
sec: &RateLimitingSecurityConfig,
) -> Self
pub fn with_path_rules_from_security( self, sec: &RateLimitingSecurityConfig, ) -> Self
Attach per-path rules from [security.rate_limiting] auth endpoint fields.
Sourcepub const fn config(&self) -> &RateLimitConfig
pub const fn config(&self) -> &RateLimitConfig
Return the active rate limit configuration.
Sourcepub const fn path_rule_count(&self) -> usize
pub const fn path_rule_count(&self) -> usize
Number of per-path rate limit rules registered.
Sourcepub fn retry_after_for_path(&self, path: &str) -> u32
pub fn retry_after_for_path(&self, path: &str) -> u32
Seconds a client should wait before retrying after a per-path rate limit rejection.
Returns the window duration for the matching path rule (e.g. 60s for an auth/start rule with 5 req/60s), not the IP token-bucket interval.
Sourcepub async fn check_ip_limit(&self, ip: &str) -> CheckResult
pub async fn check_ip_limit(&self, ip: &str) -> CheckResult
Check whether a request from ip is within the global IP rate limit.
Sourcepub async fn check_user_limit(&self, user_id: &str) -> CheckResult
pub async fn check_user_limit(&self, user_id: &str) -> CheckResult
Check whether a request from user_id is within the per-user limit.
Sourcepub async fn check_path_limit(&self, path: &str, ip: &str) -> CheckResult
pub async fn check_path_limit(&self, path: &str, ip: &str) -> CheckResult
Check the per-path rate limit for a request from ip to path.
Returns an allowed CheckResult when no rule governs the path.
CheckResult::retry_after_secs reflects the actual per-path window, not
the global IP rate.
Sourcepub async fn cleanup(&self)
pub async fn cleanup(&self)
Evict stale in-memory buckets.
No-op for the Redis backend — Redis handles expiry via PEXPIRE.
Sourcepub fn retry_after_secs(&self) -> u32
pub fn retry_after_secs(&self) -> u32
Conservative static estimate of how long (in seconds) a client must wait
before the IP-level bucket refills one token: ceil(1 / rps_per_ip).
Used when no backend-computed retry_after_ms is available (e.g., the
in-memory backend before the precise value is plumbed end-to-end, or as
a fallback on Redis errors). Minimum 1 second.
Auto Trait Implementations§
impl Freeze for RateLimiter
impl !RefUnwindSafe for RateLimiter
impl Send for RateLimiter
impl Sync for RateLimiter
impl Unpin for RateLimiter
impl UnsafeUnpin for RateLimiter
impl !UnwindSafe for RateLimiter
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more