Skip to main content

Quota

Struct Quota 

Source
pub struct Quota { /* private fields */ }
Expand description

A rate limit: limit requests per period, per key, with a burst ceiling.

A quota describes the sustained rate a key is allowed and how much it may spend at once. Under the default token-bucket algorithm each key starts with a full allowance of burst, spends one unit per admitted request, and accrues limit units back over period — so a key may burst up to burst immediately and then sustain limit per period thereafter. burst defaults to limit (the classic “burst equals rate” bucket); raise it with with_burst to allow larger spikes, or lower it to shape traffic more tightly.

The convenience constructors per_second and per_minute are infallible (a limit of 0 yields a quota that admits nothing). The general rate constructor validates its inputs and returns a RateLimiterError for values that cannot describe a working limit.

The window algorithms (fixed and sliding window) admit at most limit per period and ignore burst; it applies to the token and leaky buckets.

§Examples

use rate_net::Quota;
use std::time::Duration;

let per_sec = Quota::per_second(100);
assert_eq!(per_sec.limit(), 100);
assert_eq!(per_sec.period(), Duration::from_secs(1));
assert_eq!(per_sec.burst(), 100); // defaults to the limit

// 1000 requests per minute, but bursts capped at 50.
let shaped = Quota::rate(1000, Duration::from_secs(60))?.with_burst(50);
assert_eq!(shaped.limit(), 1000);
assert_eq!(shaped.burst(), 50);

Implementations§

Source§

impl Quota

Source

pub const fn per_second(limit: u32) -> Self

A quota of limit requests per second, per key.

Infallible: a limit of 0 produces a quota that admits nothing, which is well-defined. Use rate when you want a zero limit rejected as an error.

§Examples
use rate_net::Quota;
use std::time::Duration;

let quota = Quota::per_second(50);
assert_eq!(quota.limit(), 50);
assert_eq!(quota.period(), Duration::from_secs(1));
Source

pub const fn per_minute(limit: u32) -> Self

A quota of limit requests per minute, per key.

Infallible, with the same zero-limit semantics as per_second.

§Examples
use rate_net::Quota;
use std::time::Duration;

let quota = Quota::per_minute(600);
assert_eq!(quota.period(), Duration::from_secs(60));
Source

pub const fn rate( limit: u32, period: Duration, ) -> Result<Self, RateLimiterError>

A quota of limit requests per arbitrary period, validated.

Use this when the natural window is neither a second nor a minute — for example 5 requests per 100 milliseconds, or 10 000 per hour.

§Errors
§Examples
use rate_net::{Quota, RateLimiterError};
use std::time::Duration;

let quota = Quota::rate(5, Duration::from_millis(100))?;
assert_eq!(quota.limit(), 5);

// A zero limit is rejected.
assert_eq!(
    Quota::rate(0, Duration::from_secs(1)),
    Err(RateLimiterError::ZeroQuota),
);
Source

pub const fn limit(&self) -> u32

The number of requests admitted per period.

§Examples
use rate_net::Quota;

assert_eq!(Quota::per_second(100).limit(), 100);
Source

pub const fn period(&self) -> Duration

The window over which limit requests accrue.

§Examples
use rate_net::Quota;
use std::time::Duration;

assert_eq!(Quota::per_minute(60).period(), Duration::from_secs(60));
Source

pub const fn burst(&self) -> u32

The burst ceiling: the most a key may spend at once before it must wait for the rate to refill. Defaults to limit.

Applies to the token and leaky buckets; the window algorithms admit at most limit per period regardless of burst.

§Examples
use rate_net::Quota;

assert_eq!(Quota::per_second(100).burst(), 100);
assert_eq!(Quota::per_second(100).with_burst(250).burst(), 250);
Source

pub const fn with_burst(self, burst: u32) -> Self

Returns a copy with the burst ceiling set to burst.

§Examples
use rate_net::Quota;

// Sustain 1000/min but never let a key spend more than 50 at once.
let quota = Quota::per_minute(1000).with_burst(50);
assert_eq!(quota.limit(), 1000);
assert_eq!(quota.burst(), 50);

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 Debug for Quota

Source§

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

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

impl PartialEq for Quota

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Copy for Quota

Source§

impl Eq for Quota

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
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
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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.

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 = Infallible

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.
Source§

impl<E> WithErrorCode<E> for E

Source§

fn with_code(self, code: impl Into<String>) -> CodedError<E>

Attach an error code to an error
Source§

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

Source§

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

Source§

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