Skip to main content

RateLimiter

Struct RateLimiter 

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

An async-ready rate limiter.

Call acquire().await before each operation. If the system is ahead of the target rate, it returns immediately. If behind, it awaits until a permit is available. The blocked time is the wait_time that surfaces coordinated omission.

Implementations§

Source§

impl RateLimiter

Source

pub fn start(spec: RateSpec) -> Self

Create and start a rate limiter from a spec.

Source

pub async fn acquire(&self) -> i64

Acquire one operation permit. Blocks (async) if rate-limited.

Returns the current backlog in ticks (waiting pool value).

Reads ticks_per_op atomically, so a concurrent Self::reconfigure takes effect on the next call.

Source

pub fn wait_time_nanos(&self) -> u64

Current wait time in nanoseconds (backlog converted to nanos).

Source

pub fn total_blocks(&self) -> u64

Total number of acquire calls.

Source

pub fn rate(&self) -> f64

Current ops/sec target.

Source

pub fn spec(&self) -> RateSpec

Current full spec snapshot.

Source

pub fn reconfigure(&self, spec: RateSpec) -> Result<(), String>

Live reconfigure. Atomically swaps the target rate / burst ratio / unit without stopping the refill task. The next Self::acquire call reads the new ticks_per_op; the next refill cycle reads the new burst config.

In-flight backlog (waiting_pool) and already-issued active-pool permits are preserved. If the new rate is much higher than the old one the active pool will fill up over the next few refill ticks; if much lower, already-granted permits still drain before pressure builds.

Validation of the new spec is the caller’s responsibility — a negative or zero ops_per_sec will panic in ticks_per_op. Callers that wire this up through a Control<RateSpec> should install a validator on the control to reject bad values before this method is called.

Source

pub async fn stop(self)

Stop the rate limiter and its refill task.

Trait Implementations§

Source§

impl Drop for RateLimiter

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. 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<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> 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> 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, 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.