Skip to main content

AdaptiveLimiter

Struct AdaptiveLimiter 

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

Shared concurrency gate with additive-increase, multiplicative-decrease backoff.

TypeSafe’s rate limit is a sustained token budget with no quota headers, and several processes may share one key. So on a throttled reply every thread pauses briefly and the allowed concurrency halves; each success grows it back by roughly one slot per round.

A Client owns one and wraps every attempt in acquire and release. The type is public so that the gate can be inspected, and so that other transports can share the policy.

Implementations§

Source§

impl AdaptiveLimiter

Source

pub fn new(max_concurrency: usize, pause: Duration) -> Self

max_concurrency is the ceiling the limit grows back to (at least one). pause is how long every thread waits after a throttled reply, scaled by a random factor in [1, 2).

Source

pub fn limit(&self) -> f64

The current concurrency limit. Starts at the ceiling, halves on throttling, grows back on success; never below one. Fractional, because it grows by a fraction of a slot per reply; the whole part is the number of attempts allowed at once.

Source

pub fn in_flight(&self) -> usize

Attempts between acquire and release right now.

Source

pub fn acquire(&self)

Blocks until there is a free slot under the current limit and no pause is in effect. Every acquire must be matched by one release.

Source

pub fn release(&self, throttled: bool)

Frees the slot. throttled reports a rate-limited reply (HTTP 429 or 529): the limit halves and every thread pauses, once per throttling episode rather than once per reply. Any other outcome grows the limit by about one slot per round of requests. A release without an acquire frees nothing.

Trait Implementations§

Source§

impl Debug for AdaptiveLimiter

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