Skip to main content

TransportLadder

Struct TransportLadder 

Source
pub struct TransportLadder<S> { /* private fields */ }
Expand description

An ordered set of transports backed by an offline buffer.

Rungs are tried in the order they are added, so the cheapest, most-preferred link is added first. A send that no rung accepts is buffered in the [Store] and replayed by flush.

Implementations§

Source§

impl<S: Store> TransportLadder<S>

Source

pub fn new(buffer: S) -> Self

Creates an empty ladder that buffers into buffer.

§Arguments
  • buffer - the durable queue that holds messages while no rung is reachable.
§Returns

A ladder with no rungs; add them with rung.

Source

pub fn rung(self, transport: impl Transport + Send + 'static) -> Self

Adds a rung, lowest-cost first.

§Arguments
  • transport - a transport to try. Rungs added earlier are preferred, so add the cheapest link first and the costliest fallback last.
§Returns

The ladder, for chaining.

Source

pub async fn connect(&mut self) -> Result<()>

Connects every rung, best-effort.

A rung that fails to connect is left unreachable rather than failing the whole ladder; sends simply fall through to the next rung or the buffer.

§Returns

Ok(()) once every rung has been given the chance to connect.

§Errors

This call is best-effort and currently always returns Ok(()).

Source

pub async fn send(&mut self, topic: &str, payload: &[u8]) -> Result<Delivery>

Sends a payload, falling back down the rungs and then to the buffer.

If the buffer is empty, each rung is tried in order and the first to accept the message delivers it. If every rung fails, or the buffer already holds a backlog, the message is buffered to preserve order.

§Arguments
  • topic - the destination topic.
  • payload - the bytes to send.
§Returns

Delivery::Sent if a rung delivered the message, or Delivery::Buffered if it was queued for a later flush.

§Errors

Returns [Error::Io] if the message must be buffered but the store cannot be written.

Source

pub async fn flush(&mut self) -> Result<usize>

Drains the buffer across the rungs, oldest record first.

Each record is sent before it is removed, so the first record no rung can deliver halts the drain and leaves it, and everything after it, buffered in order for a later retry.

§Returns

The number of records forwarded before the buffer emptied or a rung refused one.

§Errors

Returns [Error::Io] if the store cannot be read or written, or [Error::Codec] if a buffered record cannot be decoded.

Source

pub async fn buffered(&mut self) -> Result<usize>

Returns how many messages are currently buffered.

Takes the ladder mutably, like the rest of its surface. Reading through a shared borrow would hold one across the await, which would in turn oblige every rung to be Sync rather than only Send, and that is a heavier requirement than a transport should have to meet.

§Returns

The number of records waiting for a flush.

§Errors

Returns [Error::Io] if the store length cannot be read.

Auto Trait Implementations§

§

impl<S> !RefUnwindSafe for TransportLadder<S>

§

impl<S> !Sync for TransportLadder<S>

§

impl<S> !UnwindSafe for TransportLadder<S>

§

impl<S> Freeze for TransportLadder<S>
where S: Freeze,

§

impl<S> Send for TransportLadder<S>
where S: Send,

§

impl<S> Unpin for TransportLadder<S>
where S: Unpin,

§

impl<S> UnsafeUnpin for TransportLadder<S>
where S: UnsafeUnpin,

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