Skip to main content

DegradedLink

Struct DegradedLink 

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

A [Transport] decorator that simulates an ongoing degraded link.

Where Faulty in pamoja-loopback fails a fixed number of upcoming sends, a DegradedLink models a link that stays bad, so offline-first behavior can be proven against a realistic pattern rather than a one-shot outage. It can drop a configurable fraction of sends (a lossy radio) and cycle between reachable and unreachable windows (a link that comes and goes). Both are deterministic - driven by a send counter, not a clock or randomness - so a store-and-forward drain over the link behaves the same way every run.

Connect and subscribe pass straight through; only send is degraded, since that is the path store-and-forward depends on. A degraded send returns [Error::Transport], which drain_to leaves buffered, in order, to retry later.

§Examples

use pamoja_core::Transport;
use pamoja_loopback::{LoopbackBroker, LoopbackTransport};
use pamoja_sim::DegradedLink;

let broker = LoopbackBroker::new();
// A link that drops every second packet.
let mut link = DegradedLink::new(LoopbackTransport::new(broker)).drop_every(2);
link.connect().await?;

link.send("t", b"1").await?; // first send: delivered
assert!(link.send("t", b"2").await.is_err()); // second send: dropped
link.send("t", b"3").await?; // third send: delivered

Implementations§

Source§

impl<T> DegradedLink<T>

Source

pub fn new(inner: T) -> Self

Wraps inner as a perfect link, until loss or intermittency is added.

§Arguments
  • inner - the transport to decorate.
§Returns

A decorator that passes every send through until configured otherwise.

Source

pub fn drop_every(self, n: u32) -> Self

Drops one in every n sends, simulating a lossy link.

§Arguments
  • n - drop every nth send; 0 disables loss.
§Returns

The updated link, for chaining.

Source

pub fn intermittent(self, up: u32, down: u32) -> Self

Cycles between up reachable sends and down unreachable sends.

Sends rejected during a down window return [Error::Transport], the same as a real link that is temporarily out of range.

§Arguments
  • up - the number of sends that succeed at the start of each cycle.
  • down - the number of sends that fail before the cycle repeats.
§Returns

The updated link, for chaining.

Source

pub fn into_inner(self) -> T

Unwraps the decorator, returning the inner transport.

§Returns

The wrapped transport.

Trait Implementations§

Source§

impl<T: Clone> Clone for DegradedLink<T>

Source§

fn clone(&self) -> DegradedLink<T>

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: Debug> Debug for DegradedLink<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T: Transport + Send> Transport for DegradedLink<T>

Source§

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

Establishes the connection to the broker, peer, or bus. Read more
Source§

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

Publishes a payload to a topic. Read more
Source§

async fn subscribe(&mut self, topic: &str) -> Result<()>

Subscribes to a topic so that matching payloads are routed to this transport. Read more

Auto Trait Implementations§

§

impl<T> Freeze for DegradedLink<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for DegradedLink<T>
where T: RefUnwindSafe,

§

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

§

impl<T> Sync for DegradedLink<T>
where T: Sync,

§

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

§

impl<T> UnsafeUnpin for DegradedLink<T>
where T: UnsafeUnpin,

§

impl<T> UnwindSafe for DegradedLink<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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. 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.