pub struct CacheRateLimitLayer { /* private fields */ }Expand description
Fixed-window counter rate limiter backed by a Cache.
Cheap to clone (everything is Arc-wrapped or Copy).
Implementations§
Source§impl CacheRateLimitLayer
impl CacheRateLimitLayer
Sourcepub fn new(cache: BoxedCache, capacity: u32, window: Duration) -> Self
pub fn new(cache: BoxedCache, capacity: u32, window: Duration) -> Self
New limiter: capacity requests per window, keyed by IP.
pub fn key_by(self, key_by: KeyBy) -> Self
pub fn key_prefix(self, prefix: impl Into<String>) -> Self
Sourcepub async fn take(&self, key: &str) -> Result<(u32, u64), u64>
pub async fn take(&self, key: &str) -> Result<(u32, u64), u64>
Take one slot from the bucket for key. Returns
Ok((current_count, reset_at_unix_secs)) on success,
Err(retry_after_secs) when over capacity.
§Errors
Returns Err(retry_after_secs) when the limit has been hit. Any
underlying cache error is treated as “fail open” — the request is
allowed and Ok((0, 0)) is returned. This avoids hard outages
when Redis is briefly unreachable; flip to fail-closed by reading
cache.incr(...) directly if your threat model requires it.
Trait Implementations§
Source§impl Clone for CacheRateLimitLayer
impl Clone for CacheRateLimitLayer
Source§fn clone(&self) -> CacheRateLimitLayer
fn clone(&self) -> CacheRateLimitLayer
Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreAuto Trait Implementations§
impl Freeze for CacheRateLimitLayer
impl !RefUnwindSafe for CacheRateLimitLayer
impl Send for CacheRateLimitLayer
impl Sync for CacheRateLimitLayer
impl Unpin for CacheRateLimitLayer
impl UnsafeUnpin for CacheRateLimitLayer
impl !UnwindSafe for CacheRateLimitLayer
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
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
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>
Converts
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>
Converts
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