Module local_link

Source
Expand description

Bounded, independently instantiated supplied-time datagram router.

A two-peer router is a local pair; more peers use the same explicit routing. The caller supplies a nonzero, never-reused link_id namespace per router lifetime, and distinct peer IDs. Handles include that namespace and a private generation: reset/disposal rejects stale handles before touching queues. These identities are local routing guards, not wire authentication. Do not reuse a namespace after dropping a router while old handles might still exist.

send success means complete local admission, including packets selected for later configured loss. A full queue returns an error even for such a packet. Return send(...).is_ok() to protocol callbacks; intentional loss is observed only by receive, never by inventing ACKs. Default settings are a reliable ordered queue. Periodic impairment uses the one-based accepted packet ordinal across all routes: every Nth is lost and/or gets extra reorder delay. Refusals consume no ordinal. No RNG, thread, socket or implicit clock exists.

Time is monotonic, exact microsecond Duration, 0..=2^53 microseconds, shared across all operations/routes. Equal times are legal. Delay and extra reorder delay are independently <=60 seconds; delivery must fit the time domain. Delivery order is (due_at, admission ordinal) per destination, so equal times are exact FIFO even after removals. There is no head-of-line blocking across destinations. Call receive repeatedly with a host work budget; each call removes at most one due packet (including one loss record). Poll empty destinations too if they may hold losses; no background disposal occurs.

Limits are aggregate payload bytes and packets, not per-peer. Empty packets reject; maximum is the existing MAX_PACKET_BYTES (1200). At most 64 peers, 4096 packets and 4096*1200 logical bytes may be configured; zero queue limits are valid backpressure. Construction reserves storage for all configured packet slots (each admitted packet has an initialized 1200-byte array) and peers with fallible reservation. No send/receive/reset/dispose operation allocates. Physical allocation includes metadata/padding and unused packet-array tails; the byte limit counts only initialized payload prefixes in admitted packets. Queue selection/removal and endpoint checks take bounded linear work.

On an existing router, every returned error leaves time, queues, generations, ordinal and caller output unchanged. Success, including an empty receive, advances the time watermark. A short receive output rejects before dequeue; success copies only len bytes, preserving its tail. A loss record or empty receive writes nothing. Reset clears all traffic/ordinals and invalidates every live handle, while preserving limits, impairment and monotonic time. Endpoint reset clears both directions for that peer and invalidates its handles; other peers/ordinals remain intact. Dispose permanently removes that peer and its traffic. Drop disposes the whole router. Generation/ordinal exhaustion never wraps.

use ferravex_net::local_link::{LocalLink, Limits, Impairment};
use std::time::Duration;
let mut pair = LocalLink::new(42, &[10, 20], Limits::default(), Impairment::default())?;
let a = pair.endpoint(10)?;
let b = pair.endpoint(20)?;
pair.send(a, b, b"whole datagram", Duration::ZERO)?;
let mut output = [0xAA; 1200];
let delivery = pair.receive(b, Duration::ZERO, &mut output)?.unwrap();
assert!(!delivery.lost);
assert_eq!(&output[..delivery.len], b"whole datagram");
assert_eq!(output[delivery.len], 0xAA);
pair.reset(Duration::ZERO)?;
assert!(pair.send(a, b, b"stale", Duration::ZERO).is_err());

Structs§

Delivery
One removed due datagram. lost=true is a post-admission impairment record: output was untouched and must not be passed to a protocol driver.
Endpoint
Explicit peer identity within one live router generation. Use accessors for host address mapping; handles cannot be constructed with a guessed generation.
Impairment
Deterministic periodic impairment; defaults disable all impairment. Some(0) is invalid. Delay is applied to every packet; reorder_delay is additionally applied to every reorder_every-th admitted packet. Loss and reorder selections may overlap. Periods are global to this router, not flows.
Limits
Aggregate queue budgets. Defaults: 128 packets and 153600 payload bytes. Both limits apply independently, including to later loss records. Bytes need not equal packets * 1200; a smaller byte cap is useful for mixed packet sizes.
LocalLink
Bounded datagram storage; no global/shared transport state or implicit I/O. See the module recipe and FPS local_session example for driver composition.

Enums§

LinkError
Preflight failures; none mutate the router or caller output.