Expand description
Bounded, separate sent and installed snapshot histories.
An ACK authorizes only the exact bytes still retained for that tick. This proves local sent-byte retention, not remote application acceptance: callers must authenticate the packet/session and define an ACK to mean the receiver validated, accepted and installed those bytes. A transport ACK is insufficient. Receiver application is separate from install, so unacceptable reconstructed snapshots never become baselines automatically.
Both stores reserve metadata and a byte arena at construction. Thereafter retain, ACK, select, apply and reset allocate nothing. Limits bound snapshot count and the sum of retained byte lengths; empty snapshots count too. Allocator overhead is excluded. Eviction removes an oldest insertion prefix, including an acknowledged baseline when necessary; no pinned copy bypasses the byte limit. Compaction costs O(retained bytes + snapshot count). Rejected operations preserve the store and output.
Ticks advance by crate::sequence_newer, including u32::MAX -> 0.
Identical retained repeats are idempotent without refreshing eviction order;
conflicting repeats, evicted repeats, backward/half-range ticks and a retained
window spanning half the tick space are rejected. Internal nonwrapping u64
insertion generations order ACKs even across tick wrap. Generation exhaustion
requires reset. No local generation is transmitted or authenticates packets.
Call reset on both stores on reconnect/session change and validate the
external session epoch before every call, including ACKs after reset.
Reset cannot distinguish a delayed old-session ACK with a reused tick.
Tick-only ACKs also cannot identify a prior revolution in the same session:
callers must expire packets within half the tick space, or rotate the session
before tick reuse. Local generations only order known retained insertions.
This runnable first-party recipe uses real snapshot/ACK/delta codecs. The
journey400_229 integration test also covers lost/evicted baselines, full
recovery and membership length changes. Outer packet encoding still allocates.
use ferravex_net::{
baselines::{AckOutcome, BaselineLimits, Selection, SenderBaselines, ReceiverBaselines},
delta::Delta, ClientExtension, ClientPacket, ServerExtension, ServerPacket,
Snapshot, MAX_PACKET_BYTES, decode_client, decode_server, encode_client, encode_server,
};
fn main() -> Result<(), Box<dyn std::error::Error>> {
let limits = BaselineLimits { max_snapshots: 32, max_bytes: 32 * MAX_PACKET_BYTES };
let mut sent = SenderBaselines::new(limits)?;
let mut received = ReceiverBaselines::new(limits)?;
let first = Snapshot { session: 7, tick: 40, ..Snapshot::default() };
let full = encode_server(&ServerPacket::Snapshot(first.clone()))?;
// Deliver successfully, then record the exact full representation sent.
sent.record_sent(first.tick, &full)?;
assert_eq!(decode_server(&full)?, ServerPacket::Snapshot(first.clone()));
received.install(first.tick, &full)?; // Consumer accepts this snapshot.
let ack = encode_client(&ClientPacket::Extension(ClientExtension::SnapshotAck {
session: 7, tick: Some(40),
}))?;
let ClientPacket::Extension(ClientExtension::SnapshotAck { session: 7, tick: Some(tick) })
= decode_client(&ack)? else { panic!("wrong session/ACK") };
assert_eq!(sent.acknowledge(tick), AckOutcome::Accepted);
let next = Snapshot { tick: 41, ..first };
let full = encode_server(&ServerPacket::Snapshot(next.clone()))?;
let mut delta = Delta { base_tick: 0, tick: 0, bytes: Vec::with_capacity(MAX_PACKET_BYTES) };
assert!(matches!(sent.select_into(41, &full, &mut delta, 24, MAX_PACKET_BYTES),
Selection::Delta { base_tick: 40, .. }));
let wire = encode_server(&ServerPacket::Extension(ServerExtension::SnapshotDelta {
session: 7, delta,
}))?;
sent.record_sent(41, &full)?; // Retain reconstructed bytes, not delta bytes.
let ServerPacket::Extension(ServerExtension::SnapshotDelta { session: 7, delta })
= decode_server(&wire)? else { panic!("wrong session/delta") };
let mut output = [0; MAX_PACKET_BYTES];
let n = received.apply_into(&delta, &mut output)?;
let ServerPacket::Snapshot(snapshot) = decode_server(&output[..n])?
else { panic!("not a snapshot") };
assert_eq!((snapshot.session, snapshot.tick), (7, delta.tick));
assert_eq!(snapshot, next); // Validate/accept before installation or ACK.
received.install(snapshot.tick, &output[..n])?;
let ack = encode_client(&ClientPacket::Extension(ClientExtension::SnapshotAck {
session: snapshot.session, tick: Some(snapshot.tick),
}))?;
let ClientPacket::Extension(ClientExtension::SnapshotAck { session: 7, tick: Some(tick) })
= decode_client(&ack)? else { panic!("wrong session/ACK") };
assert_eq!(sent.acknowledge(tick), AckOutcome::Accepted);
assert_eq!(sent.baseline(), Some((41, &output[..n])));
sent.reset(); received.reset();
assert_eq!(sent.acknowledge(41), AckOutcome::Unknown);
Ok(())
}Structs§
- Baseline
Limits - Explicit per-peer storage budget. Zero bytes permits only empty snapshots; zero snapshots is invalid. See the module recipe for construction and use.
- Receiver
Baselines - Receiver-only installed bytes. Applying into scratch never installs or ACKs the result; the caller must decode, validate and accept it first.
- Sender
Baselines - Exact sent bytes with ACK authorization, separate from installed state. See the module recipe for the required send/install/ACK sequence.
Enums§
- AckOutcome
- ACK decisions require exact retained membership before checking generation. Unknown (including evicted) and stale ACKs never change authorization.
- Apply
Error - Application failure preserves the entire caller output and installed store.
- Baseline
Error - Retention/construction failure. Every error leaves an existing store intact.
- Full
Reason - Why selection recommends the caller’s full snapshot. This does not validate that the full packet fits its protocol, transport, or retention budget.
- Retain
Outcome - Successful retention. A duplicate neither evicts nor advances generation.
- Selection
- Only
Deltameans the supplied reusable delta was replaced. Full fallback leaves it unchanged; branch on this result before encoding that delta.