Skip to main content

OrderedQueue

Struct OrderedQueue 

Source
pub struct OrderedQueue<T> { /* private fields */ }
Expand description

A reorder buffer that outputs items in serial order.

Uses smart backpressure to prevent deadlock:

  • When waiting for next_seq: MUST accept items (refusing would deadlock)
  • When we have next_seq: CAN refuse items (consumer can drain)

§Deadlock Prevention

The key insight is that if we’re waiting for serial N, we must accept serials N+1, N+2, etc. because serial N might be produced by another thread that’s blocked trying to push to this queue. Only when we have serial N available can we safely apply backpressure, because the consumer can make progress by draining serial N.

§Example

let queue = OrderedQueue::new(1_000_000); // 1MB limit
queue.insert(2, item2, 1000)?; // Accepted (waiting for 0)
queue.insert(0, item0, 1000)?; // Accepted (now has 0)
let (item, size) = queue.try_pop_next().unwrap(); // Returns item0
queue.insert(1, item1, 1000)?; // Accepted (now has 1)

Implementations§

Source§

impl<T> OrderedQueue<T>

Source

pub fn new(limit_bytes: u64) -> Self

Create a new ordered queue with the given memory limit.

Source

pub fn can_accept(&self, heap_size: usize) -> bool

Check if we can accept an item (lock-free fast path).

Returns true if:

  • We don’t have next_seq (must accept to make progress), OR
  • We’re under the memory limit
Source

pub fn insert( &self, serial: u64, item: T, heap_size: usize, ) -> Result<(), (T, usize)>

Insert an item into the reorder buffer.

Acceptance rule:

  • If we do NOT have next_seq: ACCEPT (must accumulate for progress)
  • If we DO have next_seq: only accept if under memory limit

Returns Err((item, heap_size)) if rejected due to backpressure.

§Errors

Returns the item and heap size if rejected due to memory backpressure.

Source

pub fn try_pop_next(&self) -> Option<(T, usize)>

Try to pop the next item in serial order.

Returns Some((item, heap_size)) if next_seq is available.

Source

pub fn next_seq(&self) -> u64

Get the next expected serial number.

Source

pub fn can_pop(&self) -> bool

Check if we have the next expected serial (can make progress).

Source

pub fn current_bytes(&self) -> u64

Current memory usage in bytes.

Source

pub fn set_limit(&self, new_limit: u64)

Update the memory limit (for dynamic rebalancing).

Source

pub fn limit_bytes(&self) -> u64

Get current limit.

Source

pub fn len(&self) -> usize

Number of items in the buffer.

Source

pub fn is_empty(&self) -> bool

Check if buffer is empty.

Source

pub fn record_sample(&self)

Record a sample for stats.

Source

pub fn record_blocked(&self, ns: u64)

Record blocked time in nanoseconds.

Source

pub fn collect_stats(&self) -> QueueStats

Collect and reset stats.

Auto Trait Implementations§

§

impl<T> !Freeze for OrderedQueue<T>

§

impl<T> !RefUnwindSafe for OrderedQueue<T>

§

impl<T> Send for OrderedQueue<T>
where T: Send,

§

impl<T> Sync for OrderedQueue<T>
where T: Send,

§

impl<T> Unpin for OrderedQueue<T>
where T: Unpin,

§

impl<T> UnsafeUnpin for OrderedQueue<T>

§

impl<T> UnwindSafe for OrderedQueue<T>
where T: 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<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. 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.