pub struct Queue<E: Context, V: CodecShared> { /* private fields */ }Expand description
A durable, at-least-once delivery queue with per-item acknowledgment.
Items are durably stored in a journal and survive crashes. Queue::init returns the queue, which writes items, and its Reader, which delivers them. The reader must acknowledge each item individually after processing. Items can be acknowledged out of order, enabling parallel processing.
§Operations
- append / commit: Write items to the journal, then persist them and publish them to the reader. Appended items are lost on restart if not committed.
- enqueue: Append + commit in one step. The item is durable before return.
- sync: Commit, then prune completed sections below the reader’s ack floor.
- Reader::recv / Reader::try_recv: Return the next unacked committed item in FIFO order.
- Reader::ack / Reader::ack_up_to: Mark items as processed (in-memory only).
§Acknowledgment
Acks are tracked in-memory with an ack_floor (all positions below are acked)
plus an RMap of acked positions above the floor. When items are acked
contiguously from the floor, the floor advances automatically.
Acks are not persisted. The durable equivalent is the journal’s pruning boundary, advanced by sync. On restart, all non-pruned items are re-delivered regardless of prior ack state.
§Crash Recovery
On restart, ack_floor is set to the journal’s pruning boundary.
Items that were pruned are gone. Everything else is re-delivered.
Applications must handle duplicates (idempotent processing).
Storage-mutating functions consume the queue and return it only on success: an error (or a
dropped future) destroys the handle. The reader then delivers every published item and returns
None.
Implementations§
Source§impl<E: Context, V: CodecShared> Queue<E, V>
impl<E: Context, V: CodecShared> Queue<E, V>
Sourcepub async fn init(
context: E,
cfg: Config<V::Cfg>,
) -> Result<(Self, Reader<E, V>), Error>
pub async fn init( context: E, cfg: Config<V::Cfg>, ) -> Result<(Self, Reader<E, V>), Error>
Initialize a queue from storage, returning it with its Reader.
On first initialization, creates an empty queue. On restart, the reader begins from the journal’s pruning boundary (providing at-least-once delivery for all non-pruned items). Both handles must be dropped before the queue is reopened.
§Errors
Returns an error if the underlying journal cannot be initialized.
Sourcepub async fn enqueue(self, item: &V) -> Result<(Self, u64), Error>
pub async fn enqueue(self, item: &V) -> Result<(Self, u64), Error>
Append and commit an item, returning its position. The reader can receive the item once this returns.
§Errors
Returns an error if the underlying storage operation fails.
Sourcepub async fn enqueue_bulk(
self,
items: impl IntoIterator<Item = impl Borrow<V>>,
) -> Result<(Self, Range<u64>), Error>
pub async fn enqueue_bulk( self, items: impl IntoIterator<Item = impl Borrow<V>>, ) -> Result<(Self, Range<u64>), Error>
Append a batch of items with a single commit, returning positions [start, end). The
reader can receive the items once this returns.
§Errors
Returns an error if any append or the final commit fails.
Sourcepub async fn append(self, item: &V) -> Result<(Self, u64), Error>
pub async fn append(self, item: &V) -> Result<(Self, u64), Error>
Append an item without committing, returning its position. The reader can receive the item after the next Self::commit or Self::sync.
§Errors
Returns an error if the underlying storage operation fails.
Sourcepub async fn commit(self) -> Result<Self, Error>
pub async fn commit(self) -> Result<Self, Error>
Durably persist appended items and publish them to the reader.
This does not persist acknowledgements. For a stronger guarantee that eliminates potential recovery and prunes acknowledged items, use Self::sync instead.
Trait Implementations§
Auto Trait Implementations§
impl<E, V> !RefUnwindSafe for Queue<E, V>
impl<E, V> !UnwindSafe for Queue<E, V>
impl<E, V> Freeze for Queue<E, V>
impl<E, V> Send for Queue<E, V>
impl<E, V> Sync for Queue<E, V>
impl<E, V> Unpin for Queue<E, V>
impl<E, V> UnsafeUnpin for Queue<E, V>
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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> FutureExt for T
impl<T> FutureExt for T
Source§fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ
fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ
Source§fn with_current_context(self) -> WithContext<Self> ⓘ
fn with_current_context(self) -> WithContext<Self> ⓘ
impl<A, B, T> HttpServerConnExec<A, B> for Twhere
B: Body,
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