Skip to main content

Hybrid

Struct Hybrid 

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

Several limiters combined so a request must satisfy all of them.

The classic use is layering windows on one resource — say “at most 10 per second and at most 100 per minute” — where either ceiling can bind. A hybrid is itself a Limiter, so hybrids nest and slot anywhere a single limiter does.

Acquisition is two-phase to stay correct: every constituent is first peeked, and tokens are only taken once all of them would grant. Without that, an early constituent could spend a token for a request a later one refuses. See Limiter for the full rationale.

Build one with Hybrid::builder.

§Examples

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

// 10 per second, and no more than 100 per minute.
let hybrid = Hybrid::builder()
    .limiter(Throttle::per_second(10))
    .limiter(Throttle::per_duration(100, Duration::from_secs(60)))
    .build();

assert!(hybrid.try_acquire());

Implementations§

Source§

impl Hybrid

Source

pub fn builder() -> HybridBuilder

Starts building a hybrid limiter.

Source

pub fn try_acquire(&self) -> bool

Attempts to take one token from every constituent without waiting, returning whether all granted.

All-or-nothing across constituents: either every one is debited or, on a refusal, the call reports failure.

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

let hybrid = Hybrid::builder().limiter(Throttle::per_second(1)).build();
assert!(hybrid.try_acquire());
assert!(!hybrid.try_acquire());
Source

pub fn try_acquire_with_cost(&self, cost: u32) -> bool

Attempts to take cost tokens from every constituent without waiting.

Source§

impl Hybrid

Source

pub async fn acquire(&self) -> Result<(), ThrottleError>

Available on crate feature runtime only.

Takes one token from every constituent, waiting until all are free.

§Errors

Returns ThrottleError::CostExceedsCapacity if some constituent’s capacity is too small to ever grant the request.

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

let hybrid = Hybrid::builder().limiter(Throttle::per_second(100)).build();
hybrid.acquire().await?;
Source

pub async fn acquire_with_cost(&self, cost: u32) -> Result<(), ThrottleError>

Available on crate feature runtime only.

Takes cost tokens from every constituent, waiting until all are free.

§Errors

Returns ThrottleError::CostExceedsCapacity if some constituent can never grant cost.

Trait Implementations§

Source§

impl Clone for Hybrid

Source§

fn clone(&self) -> Hybrid

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 Limiter for Hybrid

Source§

fn available(&self) -> u32

The headroom of the binding constituent: the fewest tokens any one of them has available. An empty hybrid is unbounded (u32::MAX).

Source§

fn capacity(&self) -> u32

The capacity of the binding constituent: the smallest capacity among them, since that is the first ceiling a request hits. An empty hybrid is unbounded (u32::MAX).

Source§

fn peek(&self, cost: u32) -> Decision

Reports whether cost tokens would be granted now, without taking them. Read more
Source§

fn acquire_cost(&self, cost: u32) -> Decision

Attempts to take cost tokens now, deducting them on success. 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