Skip to main content

Throttle

Struct Throttle 

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

Token-bucket admission control gate.

The bucket is initialised full, so the first burst of up to capacity tokens is granted immediately.

Implementations§

Source§

impl Throttle

Source

pub fn new(capacity: u64, refill_per_sec: u64) -> Self

Build a new throttle with the given burst capacity and sustained refill rate. The bucket starts full.

The queue metric label defaults to "default". Use Throttle::with_name to pick a meaningful label.

Source

pub fn with_name(name: &'static str, capacity: u64, refill_per_sec: u64) -> Self

Build a new throttle with an explicit metric label.

Source

pub fn capacity(&self) -> u64

Burst capacity (the maximum number of tokens that may be acquired in one go).

Source

pub fn refill_per_sec(&self) -> u64

Sustained refill rate in tokens per second.

Source

pub fn available(&self) -> u64

Best-effort snapshot of the currently available tokens. Useful for tests and diagnostics; do not branch on this in admission code (use Throttle::try_acquire instead).

Source

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

Try to take n tokens. Returns true on success and false if the bucket does not currently hold n tokens. Refills the bucket from the elapsed wall-clock interval before checking.

Requesting n > capacity always returns false: the bucket can never hold that many tokens, so blocking is pointless.

Source

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

Acquire n tokens, waiting if necessary for the bucket to refill. The time spent waiting is recorded on the throttle_wait_seconds histogram.

§Panics

Panics if n > capacity. The bucket can never hold that many tokens, so an unconditional wait would be a deadlock. Panics if refill_per_sec is zero and the initial bucket cannot satisfy the request: the throttle has no way to ever recover, so a deadlock is the alternative.

Trait Implementations§

Source§

impl Debug for Throttle

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

Source§

fn with_context(self, otel_cx: Context) -> WithContext<Self>

Attaches the provided Context to this type, returning a WithContext wrapper. Read more
Source§

fn with_current_context(self) -> WithContext<Self>

Attaches the current Context to this type, returning a WithContext wrapper. Read more
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> 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 = 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<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

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