Skip to main content

MultiLimiter

Struct MultiLimiter 

Source
pub struct MultiLimiter { /* private fields */ }
Available on crate feature std only.
Expand description

A limiter with several named dimensions, each metered independently.

One outbound call often spends against more than one budget at once. An LLM request, for instance, counts as one request, some number of input tokens, and some number of output tokens — each with its own ceiling. A MultiLimiter holds one limiter per dimension and admits a call only when every dimension can afford its share, applying the per-dimension costs atomically (peek-all-then-commit, like Hybrid).

Costs are supplied per call as (dimension, cost) pairs. A dimension not named in a call is charged nothing; a name with no matching dimension is ignored.

Build one with MultiLimiter::builder.

§Examples

use std::time::Duration;
use throttle_net::{MultiLimiter, Throttle};

let minute = Duration::from_secs(60);
let limiter = MultiLimiter::builder()
    .dimension("requests", Throttle::per_duration(60, minute))
    .dimension("input_tokens", Throttle::per_duration(100_000, minute))
    .dimension("output_tokens", Throttle::per_duration(20_000, minute))
    .build();

// A call billed at 1 request, 1500 input tokens, 200 output tokens.
limiter
    .acquire_costs(&[
        ("requests", 1),
        ("input_tokens", 1500),
        ("output_tokens", 200),
    ])
    .await?;

Implementations§

Source§

impl MultiLimiter

Source

pub fn builder() -> MultiLimiterBuilder

Starts building a multi-dimensional limiter.

Source

pub fn peek_costs(&self, costs: &[(&str, u32)]) -> Decision

Reports whether the call’s per-dimension costs would all be granted now, without taking anything.

§Examples
use throttle_net::{MultiLimiter, Throttle};

let limiter = MultiLimiter::builder()
    .dimension("requests", Throttle::per_second(10))
    .dimension("tokens", Throttle::per_second(1000))
    .build();

assert!(limiter.peek_costs(&[("requests", 1), ("tokens", 500)]).is_acquired());
Source

pub fn try_acquire_costs(&self, costs: &[(&str, u32)]) -> bool

Attempts to charge the call’s per-dimension costs without waiting, returning whether every dimension granted.

All-or-nothing across dimensions.

§Examples
use throttle_net::{MultiLimiter, Throttle};

let limiter = MultiLimiter::builder()
    .dimension("requests", Throttle::per_second(2))
    .build();

assert!(limiter.try_acquire_costs(&[("requests", 2)]));
assert!(!limiter.try_acquire_costs(&[("requests", 1)]));
Source

pub fn available(&self, dimension: &str) -> Option<u32>

Returns the tokens available in dimension right now, or None if there is no such dimension.

Source§

impl MultiLimiter

Source

pub async fn acquire_costs( &self, costs: &[(&str, u32)], ) -> Result<(), ThrottleError>

Available on crate feature runtime only.

Charges the call’s per-dimension costs, waiting until every dimension can afford its share.

This is the headline multi-dimensional operation: it paces the caller until all budgets allow the call, then commits all of them together.

§Errors

Returns ThrottleError::CostExceedsCapacity when some dimension’s cost exceeds that dimension’s capacity, naming that dimension’s figures; such a call can never succeed.

§Examples
use throttle_net::{MultiLimiter, Throttle};

let limiter = MultiLimiter::builder()
    .dimension("requests", Throttle::per_second(100))
    .dimension("tokens", Throttle::per_second(100_000))
    .build();

limiter.acquire_costs(&[("requests", 1), ("tokens", 1500)]).await?;

Trait Implementations§

Source§

impl Clone for MultiLimiter

Source§

fn clone(&self) -> MultiLimiter

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

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<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> 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> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

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<E> WithErrorCode<E> for E

Source§

fn with_code(self, code: impl Into<String>) -> CodedError<E>

Attach an error code to an error
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