pub struct Hybrid { /* private fields */ }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
impl Hybrid
Sourcepub fn builder() -> HybridBuilder
pub fn builder() -> HybridBuilder
Starts building a hybrid limiter.
Sourcepub fn try_acquire(&self) -> bool
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());Sourcepub fn try_acquire_with_cost(&self, cost: u32) -> bool
pub fn try_acquire_with_cost(&self, cost: u32) -> bool
Attempts to take cost tokens from every constituent without waiting.
Source§impl Hybrid
impl Hybrid
Sourcepub async fn acquire(&self) -> Result<(), ThrottleError>
Available on crate feature runtime only.
pub async fn acquire(&self) -> Result<(), ThrottleError>
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?;Sourcepub async fn acquire_with_cost(&self, cost: u32) -> Result<(), ThrottleError>
Available on crate feature runtime only.
pub async fn acquire_with_cost(&self, cost: u32) -> Result<(), ThrottleError>
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 Limiter for Hybrid
impl Limiter for Hybrid
Source§fn available(&self) -> u32
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
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).