pub struct BoundedQueue<T> {
pub total_enqueued: AtomicU64,
pub total_dequeued: AtomicU64,
pub total_dropped: AtomicU64,
/* private fields */
}Expand description
Thread-safe bounded FIFO queue with condvar-based blocking and backpressure.
The internal state is stored behind a Mutex<VecDeque<(T, Instant)>>.
Two Condvars (not_empty and not_full) allow producers and consumers
to park efficiently instead of busy-waiting.
Fields§
§total_enqueued: AtomicU64Cumulative items enqueued without being dropped.
total_dequeued: AtomicU64Cumulative items dequeued.
total_dropped: AtomicU64Cumulative items rejected due to a full queue.
Implementations§
Source§impl<T: Send> BoundedQueue<T>
impl<T: Send> BoundedQueue<T>
Sourcepub fn new(capacity: usize) -> Self
pub fn new(capacity: usize) -> Self
Create a new bounded queue with the given maximum capacity.
Sourcepub fn try_push(&self, item: T) -> bool
pub fn try_push(&self, item: T) -> bool
Attempt to push item without blocking.
Returns true on success, false if the queue is already at capacity
(backpressure: the caller should propagate a 503 to the client).
Sourcepub fn push_timeout(&self, item: T, timeout: Duration) -> bool
pub fn push_timeout(&self, item: T, timeout: Duration) -> bool
Push item, blocking up to timeout for a free slot.
Returns true if the item was enqueued before the timeout elapsed,
false otherwise.
Sourcepub fn pop(&self) -> Option<T>
pub fn pop(&self) -> Option<T>
Remove and return the oldest item without blocking.
Returns None if the queue is empty.
Sourcepub fn pop_timeout(&self, timeout: Duration) -> Option<T>
pub fn pop_timeout(&self, timeout: Duration) -> Option<T>
Remove and return the oldest item, blocking up to timeout for one to
become available.
Returns None on timeout.
Sourcepub fn utilization(&self) -> f32
pub fn utilization(&self) -> f32
Current fill level as a fraction in [0.0, 1.0].
Sourcepub fn stats(&self) -> QueueStats
pub fn stats(&self) -> QueueStats
Take a snapshot of queue statistics.
Auto Trait Implementations§
impl<T> !Freeze for BoundedQueue<T>
impl<T> RefUnwindSafe for BoundedQueue<T>
impl<T> Send for BoundedQueue<T>where
T: Send,
impl<T> Sync for BoundedQueue<T>where
T: Send,
impl<T> Unpin for BoundedQueue<T>where
T: Unpin,
impl<T> UnsafeUnpin for BoundedQueue<T>
impl<T> UnwindSafe for BoundedQueue<T>
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
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more