Skip to main content

Throttle

Struct Throttle 

Source
pub struct Throttle<C: Clock = SystemClock> { /* 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. Tokens accrue at refill_per_sec per second up to capacity, computed lazily against the Clock on each Throttle::try_acquire call.

Throttle is generic over the clock so loom and unit tests can inject a ManualClock. Production code uses the SystemClock default.

Implementations§

Source§

impl Throttle<SystemClock>

Source

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

Builds a throttle backed by the SystemClock.

The bucket starts full; the first acquire of up to capacity tokens succeeds without waiting.

Source§

impl<C: Clock> Throttle<C>

Source

pub fn with_clock(capacity: u64, refill_per_sec: u64, clock: C) -> Self

Builds a throttle that consults clock for refill timing.

The bucket starts full and the last-refill timestamp is captured from clock at construction time. Subsequent clock.now() values must be monotonic relative to that initial reading.

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

Tries to take n tokens.

Returns true on success and false if the bucket does not currently hold n tokens. The bucket is refilled from the clock-elapsed interval before the check.

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

Source

pub fn acquire_blocking(&self, n: u64) -> Result<(), ThrottleError>

Acquires n tokens, sleeping the calling thread if the bucket is empty.

This is the synchronous counterpart of dynomite’s async Throttle::acquire. The wait loop sleeps for the time required to refill the missing tokens at refill_per_sec, clamped to the range 1 ms .. 1 s so a fractional refill never spins tightly and a misconfigured throttle still polls regularly.

§Errors

Trait Implementations§

Source§

impl<C: Clock> Debug for Throttle<C>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<C = SystemClock> !Freeze for Throttle<C>

§

impl<C> RefUnwindSafe for Throttle<C>
where C: RefUnwindSafe,

§

impl<C> Send for Throttle<C>

§

impl<C> Sync for Throttle<C>

§

impl<C> Unpin for Throttle<C>
where C: Unpin,

§

impl<C> UnsafeUnpin for Throttle<C>
where C: UnsafeUnpin,

§

impl<C> UnwindSafe for Throttle<C>
where C: UnwindSafe,

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