Skip to main content

RateBudget

Struct RateBudget 

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

A cloneable, thread-safe token bucket. Cheap to clone (bumps an Arc); the inner state is shared, which is the whole point.

Implementations§

Source§

impl RateBudget

Source

pub fn default_system() -> Self

Build a bucket with the default capacity + refill. Clock is SystemClock (the only right answer in production).

Source

pub fn with_clock( capacity: u32, refill_per_second: f64, clock: Arc<dyn Clock>, ) -> Self

Build a bucket with explicit capacity, refill rate, and clock. Panics if capacity == 0 (a zero-capacity bucket is never usable and always exhausts; callers intending “infinitely permissive” should not wire the bucket in at all) or refill_per_second < 0.0 (a negative refill is incoherent). A refill of 0.0 is permitted — useful in tests that want to prove the exhaustion path without accrual confounding the observation.

§Panics

If capacity == 0 or refill_per_second.is_sign_negative() or refill_per_second.is_nan(). All three are programmer errors caught at construction rather than surfaced as silent “never allows anything” behavior downstream.

Source

pub fn try_consume(&self, cost: u32) -> Result<(), Exhausted>

Attempt to consume cost tokens. Returns Ok(()) on success (the bucket has been debited), or Err(Exhausted) with a floor-rounded retry_after when the bucket cannot satisfy the cost.

A cost that exceeds capacity can never be satisfied — the caller gets an Exhausted { retry_after } shaped as “forever-ish” (Duration::MAX). This is a misconfiguration signal, not a transient error; the caller should surface it loudly. In practice the cost table’s highest value (3) is well below the default capacity (60), so this path is a developer-error canary, not a production concern.

Source

pub fn refund(&self, cost: u32)

Refund cost tokens — used when an outer rate limiter (the engine’s own 429) fires after we already debited our local bucket. Without this, every 429 would double-charge the operator: once against our bucket, once against the engine’s. Capped at capacity so a runaway refund bug cannot inflate the bucket beyond its design size.

Source

pub fn snapshot(&self) -> BudgetSnapshot

Snapshot for display. Runs the refill pass so the returned token count reflects elapsed time, not the last try_consume ago — a status bar that reads rate:40/60 when the real answer is rate:55/60 paints a fake scarcity.

Source

pub fn reset_to_full(&self)

Force the bucket full. Only wired through zero doctor --fix (M2_PLAN §1’s clear-counter action) — operator confirmation is required before this runs, because bypassing the local bucket has no production use case. The name is loud on purpose.

Trait Implementations§

Source§

impl Clone for RateBudget

Source§

fn clone(&self) -> RateBudget

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for RateBudget

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> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
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 = 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