Skip to main content

Module mesh

Module mesh 

Source
Expand description

A full mesh of bounded channels between a fixed number of parties.

mesh builds, for every ordered pair (from, to) of parties, one mpsc::channel of the given capacity, and hands each party its ends: a Party keeps the sender to every party at the index of that party, and the receiver from every party at the index of that party. Parties are numbered 0..parties. Self-loops are included: party i can send to itself, so “send to the owner of key k” needs no special case.

The mesh only builds channels. Nothing here spawns anything or knows about workers: moving each Party into its own task or thread (a Party is Send when T is) is the caller’s, with any spawn.

Backpressure is per pair: a send to a party whose channel from this one is full waits for that party to take a value, and does not hold up sends to other parties or from other parties.

Party::recv receives from any party and is fair: it polls the receivers round robin, starting after the one that delivered last, so a party that floods cannot starve a quiet one. A party’s own sender to itself keeps its self-loop open, so recv ends only after Party::close_senders too, once every other party dropped its senders and everything sent was received.

use nuclei::sync::mesh::mesh;

nuclei::runtime::block_on(async {
    let mut parties = mesh::<u32>(2, 4);
    let mut b = parties.pop().unwrap();
    let a = parties.pop().unwrap();
    a.send_to(1, 7).await.unwrap();
    assert_eq!(b.recv().await, Some((0, 7)));
});

Structs§

Party
One party of a mesh: its senders to every party and its receivers from every party, both indexed by the peer.

Functions§

mesh
Builds a full mesh of parties parties, each pair joined by a bounded channel of capacity messages in each direction. Entry i of the answer is party i.