Skip to main content

Queue

Struct Queue 

Source
pub struct Queue<L, K = (), C = SystemClock>
where K: Eq + Hash + Clone + Send + Sync, C: Clock,
{ /* 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, ()>

Source

pub fn builder() -> QueueBuilder

Starts building a queue.

Source§

impl<L, K, C> Queue<L, K, C>
where L: Limiter, K: Eq + Hash + Clone + Send + Sync, C: Clock + Clone,

Source

pub fn with_clock<C2>(self, clock: C2) -> Queue<L, K, C2>
where C2: Clock + Clone,

Replaces the time source (the deadline clock), for deterministic tests. The queue is rebuilt empty around the new clock.

Source

pub fn len(&self) -> usize

The number of waiters currently enqueued (a momentary snapshot).

Source

pub fn is_empty(&self) -> bool

Returns true if no waiters are enqueued.

Source

pub fn capacity(&self) -> usize

The configured waiter capacity.

Source

pub fn inner(&self) -> &L

A shared reference to the wrapped limiter.

Source

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

Auto Trait Implementations§

§

impl<L, K = (), C = SystemClock> !Freeze for Queue<L, K, C>

§

impl<L, K, C> RefUnwindSafe for Queue<L, K, C>

§

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>
where L: Unpin, C: Unpin, K: Unpin,

§

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