pub struct RateLimiter<K, C>where
C: Clock,{ /* private fields */ }
Expand description
A rate limiter that enforces different quotas per key using the GCRA algorithm.
This implementation allows setting different rate limits for different keys, with an optional default quota for keys that don’t have specific quotas.
Implementations§
Source§impl<K> RateLimiter<K, MonotonicClock>
impl<K> RateLimiter<K, MonotonicClock>
Source§impl<K> RateLimiter<K, FakeRelativeClock>
impl<K> RateLimiter<K, FakeRelativeClock>
Sourcepub fn advance_clock(&self, by: Duration)
pub fn advance_clock(&self, by: Duration)
Advances the fake clock by the specified duration.
This is only available for testing with FakeRelativeClock
.
Source§impl<K, C> RateLimiter<K, C>
impl<K, C> RateLimiter<K, C>
Sourcepub fn add_quota_for_key(&self, key: K, value: Quota)
pub fn add_quota_for_key(&self, key: K, value: Quota)
Adds or updates a quota for a specific key.
Sourcepub fn check_key(&self, key: &K) -> Result<(), NotUntil<C::Instant>>
pub fn check_key(&self, key: &K) -> Result<(), NotUntil<C::Instant>>
Checks if the given key is allowed under the rate limit.
§Errors
Returns Err(NotUntil)
if the key is rate-limited, indicating when it will be allowed.
Sourcepub async fn until_key_ready(&self, key: &K)
pub async fn until_key_ready(&self, key: &K)
Waits until the specified key is ready (not rate-limited).
Sourcepub async fn await_keys_ready(&self, keys: Option<Vec<K>>)
pub async fn await_keys_ready(&self, keys: Option<Vec<K>>)
Waits until all specified keys are ready (not rate-limited).
If no keys are provided, this function returns immediately.
Trait Implementations§
Auto Trait Implementations§
impl<K, C> Freeze for RateLimiter<K, C>
impl<K, C> !RefUnwindSafe for RateLimiter<K, C>
impl<K, C> Send for RateLimiter<K, C>
impl<K, C> Sync for RateLimiter<K, C>
impl<K, C> Unpin for RateLimiter<K, C>
impl<K, C> UnwindSafe for RateLimiter<K, C>
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
§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>
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