Skip to main content

CapacityBucket

Struct CapacityBucket 

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

A token bucket for a venue that publishes a capacity and a refill rate, and charges each request a number of tokens.

It starts full, never holds more than capacity, and refills refill_per_sec tokens a second. acquire waits until a request’s tokens are there and takes them.

§Capacity is the published burst

This is the opposite of a window quota, and the two must not be made alike. A venue that publishes a bucket’s capacity and rate implements a bucket, so holding the published capacity is a faithful model of it. A window quota publishes a count per window with no capacity term, and a bucket holding that count admits it twice in one window; see WindowQuotaTable.

§A cost it can never hold

A request costing more than capacity can never be satisfied, however long it waits. An exact cost is refused at once with Refused, and nothing is sent. An inexact cost (a floor, such as Polymarket’s cancel-all) is charged at capacity and never refused: the server decides.

A bucket made provisional is sized from a guess, before the venue has said what the account’s capacity is. It never refuses: a cost above its capacity waits until confirm or resize, and is then charged or refused.

§The hold

acquire waits out the bucket’s Hold before charging, and again after any wait of its own, so a hold set while it waited for tokens is honoured. Every layer of one throttle shares one Hold, and resize keeps it.

A clone shares the tokens and the hold.

Implementations§

Source§

impl CapacityBucket

Source

pub fn new( layer: LayerId, capacity: u32, refill_per_sec: u32, hold: Hold, ) -> Self

A full bucket for layer holding at most capacity tokens and refilling refill_per_sec a second, waiting out hold.

§Panics

When refill_per_sec is zero, since an emptied bucket would never refill.

Source

pub fn provisional( layer: LayerId, capacity: u32, refill_per_sec: u32, hold: Hold, ) -> Self

As new, but sized provisionally: a cost above capacity waits for confirm or resize instead of being refused.

§Panics

When refill_per_sec is zero.

Source

pub fn layer(&self) -> LayerId

The layer this bucket charges.

Source

pub fn capacity(&self) -> u32

The most tokens the bucket holds.

Source

pub fn hold(&self) -> &Hold

The hold this bucket waits out.

Source

pub async fn acquire(&self, units: u32, exact: bool) -> Result<(), Refused>

Wait until units tokens are there, then take them.

Waits out the hold first, and again after its own wait. An inexact cost above capacity is charged at capacity.

§Errors

Refused at once, before any hold, when exact and units exceed the capacity of a bucket that is not provisional. Nothing is taken.

Source

pub fn resize(&self, capacity: u32, refill_per_sec: u32)

Resize the bucket in place. The tokens it holds are kept, down to the new capacity; the hold is kept; the sizing is confirmed.

§Panics

When refill_per_sec is zero.

Source

pub fn confirm(&self)

Confirm a provisional size: a cost above capacity is refused from now on, and those waiting are charged or refused.

Trait Implementations§

Source§

impl Clone for CapacityBucket

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for CapacityBucket

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: Sized + 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: Sized + 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 = !

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