Skip to main content

Router

Struct Router 

Source
pub struct Router { /* private fields */ }
Expand description

Router for mesh message routing. Peer selection uses UCB1 (multi-armed bandit) once sufficient samples exist; falls back to a heuristic score during cold-start. UCB1 state is persisted via an optional RouterStore so learned topology survives restarts.

Implementations§

Source§

impl Router

Source

pub fn new(our_node_id: String) -> Self

Create a new router (no persistence).

Source

pub fn with_store(our_node_id: String, store: Arc<dyn RouterStore>) -> Self

Create a router backed by store for UCB1 state persistence. Previously learned peer quality is loaded immediately.

Source

pub async fn should_process(&self, message: &MeshMessage) -> bool

Check if message should be processed (deduplication and TTL check)

Source

pub async fn mark_seen(&self, message_id: &str)

Mark message as seen

Source

pub fn is_for_us(&self, message: &MeshMessage) -> bool

Check if message is for us

Source

pub fn prepare_for_forwarding(&self, message: &MeshMessage) -> MeshMessage

Prepare message for forwarding (decrement TTL, add to path)

Source

pub fn calculate_peer_score( peer_metrics: &PeerMetrics, route_stats: Option<&RouteStats>, ) -> f64

Calculate routing score for a peer based on metrics (higher is better) Uses adaptive learning: score = αold_score + βnew_score

Source

pub fn get_forward_peers( &self, message: &MeshMessage, all_peers: &[String], ) -> Vec<String>

Get list of peers to forward to (flooding: all except sender)

Source

pub async fn get_best_forward_peers( &self, message: &MeshMessage, peer_infos: &[PeerInfo], max_peers: usize, ) -> Vec<String>

Get best peers to forward to using UCB1 adaptive routing.

Peer selection strategy:

  • Warm-up (selections < UCB1_MIN_SAMPLES): heuristic score + exploration bonus. Unvisited peers receive the highest bonus, ensuring all peers are tried first.
  • Exploitation (selections >= UCB1_MIN_SAMPLES): pure UCB1 score.

Returns peers sorted by score (best first), limited to top max_peers.

Source

pub async fn get_best_forward_peers_toward( &self, message: &MeshMessage, peer_infos: &[PeerInfo], max_peers: usize, dest: &str, ) -> Vec<String>

Destination-conditioned peer selection.

Identical to Self::get_best_forward_peers except that the bandit state consulted is the one scoped to dest. A neighbour that is an excellent step toward one destination is often a poor step toward another, so scoring peers with a single destination-agnostic estimate discards the signal that actually determines routing quality.

Warm-up behaviour is unchanged: until a peer has UCB1_MIN_SAMPLES observations for this destination, the heuristic score plus an exploration bonus is used, so unvisited peers are still tried first.

Source

pub async fn record_route_outcome_toward( &self, dest: &str, peer_id: &str, success: Option<Duration>, )

Record a delivery outcome against the bandit scoped to dest.

success carries the observed hop latency; None records a failure. Reward matches the destination-agnostic path: clamp(1 - 2*latency, 0.5, 1.0) on success, 0.0 on failure.

Source

pub async fn q_select_toward( &self, message: &MeshMessage, peer_infos: &[PeerInfo], max_peers: usize, dest: &str, ) -> Vec<String>

Choose next hops for dest by Q-value, best first, limited to max_peers.

Optimistic initialisation (Q_INIT = 1.0) means any neighbour never yet tried for dest outscores explored ones, so every neighbour is attempted at least once before the estimates take over — the same warm-up the benchmark uses. Connected peers only; sender and nodes already on the path are excluded for loop-freedom.

Source

pub async fn q_advertised_value(&self, dest: &str, neighbours: &[String]) -> f64

The value this node advertises to an upstream neighbour for dest: max over its own neighbours’ Q-estimates. This is the quantity a downstream node bootstraps from. neighbours is the caller’s current connected-peer id list.

Source

pub async fn q_record( &self, dest: &str, peer: &str, delivered: bool, downstream_value: f64, )

Update the Q-estimate for hop peer toward dest after an outcome.

downstream_value is the estimate the neighbour advertised (its q_advertised_value for dest). On success the bootstrap target is that value; on failure it is 0. This is the delivery-probability form of the Boyan–Littman update.

Source

pub async fn record_route_success(&self, peer_id: &str, latency: Duration)

Record successful route (for adaptive learning). Updates both the heuristic history and the UCB1 bandit state. Reward is latency-weighted: r = clamp(1 - 2*latency_secs, 0.5, 1.0).

Source

pub async fn record_route_failure(&self, peer_id: &str)

Record failed route (for adaptive learning). Updates both the heuristic history and the UCB1 bandit state (reward = 0).

Source

pub async fn cleanup_cache(&self)

Cleanup old cache entries

Trait Implementations§

Source§

impl Clone for Router

Source§

fn clone(&self) -> Self

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

Auto Trait Implementations§

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

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more