pub struct RateLimits { /* private fields */ }
Expand description
A collection of scoped rate limits.
RateLimits
manages a set of active rate limits that can be checked against
incoming data.
The collection can be empty, indicated by is_ok
, meaning no rate limits
are currently active.
Implementations§
Source§impl RateLimits
impl RateLimits
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates an empty RateLimits
instance.
Sourcepub fn add(&mut self, limit: RateLimit)
pub fn add(&mut self, limit: RateLimit)
Adds a limit to this collection.
If a rate limit with an overlapping scope already exists, the retry_after
count is merged
with the existing limit. Otherwise, the new rate limit is added.
Sourcepub fn merge(&mut self, limits: Self)
pub fn merge(&mut self, limits: Self)
Merges all limits from another RateLimits
instance into this one.
This keeps all existing rate limits, adds new ones, and updates any existing ones with a later expiration time. The resulting collection contains the maximum constraints from both instances.
Sourcepub fn is_ok(&self) -> bool
pub fn is_ok(&self) -> bool
Returns true
if this instance contains no active limits.
This is the opposite of is_limited
.
Sourcepub fn is_limited(&self) -> bool
pub fn is_limited(&self) -> bool
Returns true
if this instance contains any active rate limits.
A rate limit is considered active if it has not yet expired.
Sourcepub fn clean_expired(&mut self, now: Instant)
pub fn clean_expired(&mut self, now: Instant)
Removes expired rate limits from this instance.
This is useful for cleaning up rate limits that are no longer relevant, reducing memory usage and improving performance of subsequent operations.
Sourcepub fn check(&self, scoping: ItemScoping) -> Self
pub fn check(&self, scoping: ItemScoping) -> Self
Checks whether any rate limits apply to the given scoping.
Returns a new RateLimits
instance containing only the rate limits that match
the provided ItemScoping
. If no limits match, the returned instance will be empty
and is_ok
will return true
.
Sourcepub fn check_with_quotas<'a>(
&self,
quotas: impl IntoIterator<Item = &'a Quota>,
scoping: ItemScoping,
) -> Self
pub fn check_with_quotas<'a>( &self, quotas: impl IntoIterator<Item = &'a Quota>, scoping: ItemScoping, ) -> Self
Checks whether any rate limits or static quotas apply to the given scoping.
This is similar to check
, but additionally checks for quotas with a static
limit of 0
, which reject items even if there is no active rate limit in this instance.
Returns a new RateLimits
instance containing the rate limits that match the provided
ItemScoping
. If no limits or quotas match, the returned instance will be empty.
Sourcepub fn iter(&self) -> RateLimitsIter<'_> ⓘ
pub fn iter(&self) -> RateLimitsIter<'_> ⓘ
Returns an iterator over all rate limits in this collection.
Trait Implementations§
Source§impl Clone for RateLimits
impl Clone for RateLimits
Source§fn clone(&self) -> RateLimits
fn clone(&self) -> RateLimits
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moreSource§impl Debug for RateLimits
impl Debug for RateLimits
Source§impl Default for RateLimits
impl Default for RateLimits
Source§fn default() -> RateLimits
fn default() -> RateLimits
Source§impl<'a> IntoIterator for &'a RateLimits
impl<'a> IntoIterator for &'a RateLimits
Auto Trait Implementations§
impl Freeze for RateLimits
impl RefUnwindSafe for RateLimits
impl Send for RateLimits
impl Sync for RateLimits
impl Unpin for RateLimits
impl UnwindSafe for RateLimits
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> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§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