pub struct Quota { /* private fields */ }Expand description
A rate-limiting quota.
Quotas are expressed in a positive number of “cells” (the maximum number of positive decisions / allowed items until the rate limiter needs to replenish) and the amount of time for the rate limiter to replenish a single cell.
Neither the number of cells nor the replenishment unit of time may be zero.
§Burst Sizes
There are multiple ways of expressing the same quota: a quota given as Quota::per_second(1)
allows, on average, the same number of cells through as a quota given as Quota::per_minute(60).
The quota of Quota::per_minute(60) has a burst size of 60 cells, meaning it is
possible to accommodate 60 cells in one go, after which the equivalent of a minute of inactivity
is required for the burst allowance to be fully restored.
Burst size gets really important when you construct a rate limiter that should allow multiple
elements through at one time (using RateLimiter.check_n
and its related functions): Only
at most as many cells can be let through in one call as are given as the burst size.
In other words, the burst size is the maximum number of cells that the rate limiter will ever allow through without replenishing them.
Implementations§
Source§impl Quota
Constructors for Quotas
impl Quota
Constructors for Quotas
Sourcepub const fn per_second(max_burst: NonZeroU32) -> Option<Self>
pub const fn per_second(max_burst: NonZeroU32) -> Option<Self>
Constructs a quota for a number of cells per second. The given number of cells is also assumed to be the maximum burst size.
Returns None if max_burst is so large that the replenish interval rounds to zero
nanoseconds (i.e. max_burst > 1_000_000_000).
Sourcepub const fn per_minute(max_burst: NonZeroU32) -> Self
pub const fn per_minute(max_burst: NonZeroU32) -> Self
Constructs a quota for a number of cells per 60-second period. The given number of cells is also assumed to be the maximum burst size.
Sourcepub const fn per_hour(max_burst: NonZeroU32) -> Self
pub const fn per_hour(max_burst: NonZeroU32) -> Self
Constructs a quota for a number of cells per 60-minute period. The given number of cells is also assumed to be the maximum burst size.
Sourcepub const fn with_period(replenish_1_per: Duration) -> Option<Self>
pub const fn with_period(replenish_1_per: Duration) -> Option<Self>
Constructs a quota that replenishes one cell in a given interval.
If the time interval is zero, returns None.
Sourcepub const fn allow_burst(self, max_burst: NonZeroU32) -> Self
pub const fn allow_burst(self, max_burst: NonZeroU32) -> Self
Adjusts the maximum burst size for a quota to construct a rate limiter with a capacity for at most the given number of cells.
Source§impl Quota
Retrieving information about a quota
impl Quota
Retrieving information about a quota
Sourcepub const fn replenish_interval(&self) -> Duration
pub const fn replenish_interval(&self) -> Duration
The time it takes for a rate limiter with an exhausted burst budget to replenish a single element.
Sourcepub const fn burst_size(&self) -> NonZeroU32
pub const fn burst_size(&self) -> NonZeroU32
The maximum number of cells that can be allowed in one burst.
Sourcepub const fn burst_size_replenished_in(&self) -> Duration
pub const fn burst_size_replenished_in(&self) -> Duration
The time it takes to replenish the entire maximum burst size.
Saturates at Duration::MAX if the full duration cannot be represented.
Trait Implementations§
impl Copy for Quota
impl Eq for Quota
impl StructuralPartialEq for Quota
Auto Trait Implementations§
impl Freeze for Quota
impl RefUnwindSafe for Quota
impl Send for Quota
impl Sync for Quota
impl Unpin for Quota
impl UnsafeUnpin for Quota
impl UnwindSafe for Quota
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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.