pub struct Queue<L, K = (), C = SystemClock>{ /* private fields */ }Available on crate feature
runtime only.Expand description
A bounded, deadline-aware, priority queue fronting a limiter L, keyed by K
for fairness and timed by clock C.
Build one with Queue::builder. Use K = () for a plain priority queue
with no cross-key fairness.
§Examples
use std::time::Duration;
use throttle_net::{Overflow, Queue, Throttle};
// 50 req/s, with room for 100 waiters; reject when full.
let queue: Queue<Throttle, &str> = Queue::builder()
.capacity(100)
.overflow(Overflow::DropOldest)
.build(Throttle::per_second(50));
// Wait for a slot, but give up after 2 seconds.
queue
.acquire("tenant:1", 0, Some(Duration::from_secs(2)))
.await?;Implementations§
Source§impl Queue<Infallible, ()>
impl Queue<Infallible, ()>
Sourcepub fn builder() -> QueueBuilder
pub fn builder() -> QueueBuilder
Starts building a queue.
Source§impl<L, K, C> Queue<L, K, C>
impl<L, K, C> Queue<L, K, C>
Sourcepub fn with_clock<C2>(self, clock: C2) -> Queue<L, K, C2>
pub fn with_clock<C2>(self, clock: C2) -> Queue<L, K, C2>
Replaces the time source (the deadline clock), for deterministic tests. The queue is rebuilt empty around the new clock.
Sourcepub async fn acquire(
&self,
key: K,
priority: u32,
deadline: Option<Duration>,
) -> Result<(), ThrottleError>
pub async fn acquire( &self, key: K, priority: u32, deadline: Option<Duration>, ) -> Result<(), ThrottleError>
Acquires one token, waiting in the queue until served, the deadline passes, or the overflow policy turns the request away.
priority orders waiters (higher first). key is the fairness key —
equal-priority waiters are served round-robin across keys. deadline is a
wait budget; None waits indefinitely.
§Errors
ThrottleError::QueueFullwhen the queue is full and the policy rejects (or evicts) this request.ThrottleError::DeadlineExceededwhen the deadline passes first.ThrottleError::CostExceedsCapacitywhen the wrapped limiter can never grant a single unit.
Auto Trait Implementations§
impl<L, K = (), C = SystemClock> !Freeze for Queue<L, K, C>
impl<L, K, C> RefUnwindSafe for Queue<L, K, C>where
L: RefUnwindSafe,
C: RefUnwindSafe,
impl<L, K, C> Send for Queue<L, K, C>where
L: Send,
impl<L, K, C> Sync for Queue<L, K, C>where
L: Sync,
impl<L, K, C> Unpin for Queue<L, K, C>
impl<L, K, C> UnsafeUnpin for Queue<L, K, C>where
L: UnsafeUnpin,
C: UnsafeUnpin,
impl<L, K, C> UnwindSafe for Queue<L, K, C>where
L: UnwindSafe,
C: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more