Skip to main content

RateLimiter

Struct RateLimiter 

Source
pub struct RateLimiter { /* private fields */ }
Available on crate feature std only.
Expand description

A cheap-to-clone async handle around a shared TokenBucket: clones share the same budget.

Two ways to consume it:

Time is tracked against a per-limiter epoch taken at construction, using tokio’s clock (so paused-time tests work as expected).

There is no FIFO fairness guarantee between concurrent waiters. Under a sustained stream of smaller acquisitions, a larger acquisition can be repeatedly beaten to newly refilled tokens and starve; callers that need strict waiter fairness must serialize acquisitions above this primitive.

Implementations§

Source§

impl RateLimiter

Source

pub fn new(rate: Rate, burst: u64) -> Self

Create a new RateLimiter with the given Rate and burst capacity, starting full.

§Panics

Panics if burst is zero.

Source

pub fn from_rate(rate: Rate) -> Self

Create a new RateLimiter with a burst capacity of one period worth of units (rate.units()): a plain smooth rate.

Source

pub fn rate(&self) -> Rate

The configured Rate.

Source

pub fn burst(&self) -> u64

The configured burst capacity.

Source

pub fn try_acquire(&self, n: u64) -> Acquire

Try to spend n units without waiting.

See TokenBucket::try_acquire; the Acquire::RetryAt instant is in this limiter’s clock and can be turned into a deadline with RateLimiter::deadline.

Source

pub async fn acquire(&self, n: u64)

Spend n units, waiting until the budget allows it.

Requests larger than the burst capacity are acquired in burst-sized chunks, so this never fails: it paces. Concurrent acquisitions are not queued fairly; see the type docs.

Dropping the future while it waits for a chunk spends nothing for that chunk. For a request larger than the burst, chunks already acquired before cancellation stay spent. Refunding them could mint capacity when concurrent users spent refill produced while those chunks were held.

Source

pub fn refund(&self, n: u64)

Give back up to n previously spent units, saturating at the burst capacity.

Source

pub fn notified_on_refund(&self) -> RefundWait ⓘ

Wait until this limiter is next refunded.

Poll-based users should create this listener before calling try_acquire, so a concurrent refund cannot land between observing a deficit and registering for notification.

Source

pub fn deadline(&self, retry_at_nanos: u64) -> Instant

Turn an Acquire::RetryAt instant into a timer deadline, for poll-based callers.

Trait Implementations§

Source§

impl Clone for RateLimiter

Source§

fn clone(&self) -> Self

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 RateLimiter

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

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> Instrument for T

Source§

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

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

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.

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, !>

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<T> WithSubscriber for T

Source§

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

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

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