pub struct RateLimiter {
limit: u32,
window: Duration,
state: Mutex<State>,
}Expand description
Counts requests per key (typically a caller’s address, from
caller_key) within a fixed window, and says whether the next one is
still allowed.
Fields§
§limit: u32§window: Duration§state: Mutex<State>Implementations§
Source§impl RateLimiter
impl RateLimiter
Sourcepub fn new(limit: u32, window: Duration) -> Self
pub fn new(limit: u32, window: Duration) -> Self
A limiter allowing limit requests per window, per key.
Sourcepub fn check(&self, key: &str) -> bool
pub fn check(&self, key: &str) -> bool
Records one request for key and reports whether it is within the
limit. A key seen for the first time, or one whose window has
elapsed, starts a fresh window.
A key over MAX_KEY_LENGTH is counted against the one shared
OVER_LONG_KEY bucket rather than a bucket of its own, and buckets
whose windows have elapsed are dropped rather than kept — see both
items’ own docs for why neither changes what this answers.
Sourcepub fn tracked_keys(&self) -> usize
pub fn tracked_keys(&self) -> usize
How many keys this limiter is currently holding a bucket for.
The limiter’s retained footprint, for a test that needs to assert on
it and for anything that later wants to log it. A bucket whose window
has elapsed but which the sweep in check has not
reached yet still counts.
Trait Implementations§
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§
§impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
§impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
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
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§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