Skip to main content

Quota

Struct Quota 

Source
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

Source

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).

Source

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.

Source

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.

Source

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.

Source

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

Source

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.

Source

pub const fn burst_size(&self) -> NonZeroU32

The maximum number of cells that can be allowed in one burst.

Source

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§

Source§

impl Clone for Quota

Source§

fn clone(&self) -> Quota

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for Quota

Source§

impl Debug for Quota

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Eq for Quota

Source§

impl PartialEq for Quota

Source§

fn eq(&self, other: &Quota) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

§

impl<T> PolicyExt for T
where T: ?Sized,

§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns [Action::Follow] only if self and other return Action::Follow. Read more
§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns [Action::Follow] if either self or other returns Action::Follow. Read more
§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<T> Ungil for T
where T: Send,

§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more