Repository navigation
Description
The two serve framers — ChunkFramer (crates/node/src/handlers/client/delivery.rs) and CoherentFrameProducer (crates/node/src/handlers/client/serve_encoder.rs) — hold identical queue state and maintain its invariants by hand, with a free function (wire::drain_frame) holding the seam open via two &mut out-params that must move in lockstep.
Surfaced by the type-design review of #1828. Deliberately deferred from that PR's follow-up because it subsumes and contradicts the hardening that landed there: FrameQueue would delete the very checked_sub and queue.is_empty() guards the follow-up added, so doing both at once would have been incoherent. Harden first, refactor second — this is second.
The invariants, all currently prose
queued == queue.iter().map(Bytes::len).sum()— held by hand at four mutation sites across two files.- No empty
Bytesever enters the queue — enforced by anif len > 0at each of two independent push sites, and depended on by a third party (drain_frame'sfront_len == 0branch). - A non-empty queue with
target >= 1always yields at least one chunk — stated in a doc comment and re-checked as a near-identicalanyhow::bail!in two places.
The consequence of a breach is not a panic: it is Ok(None), which serve_leg reads as done_delivering and turns into StreamEnd over a truncated blob — with the client paying the closing voucher. These are money-carrying invariants enforced by runtime checks at the right doors rather than by a type.
Proposed shape
pub(super) struct FrameQueue { queue: VecDeque<Bytes>, queued: usize }
impl FrameQueue {
fn push(&mut self, b: Bytes); // drops empties, keeps `queued` in step
fn len(&self) -> usize;
fn is_empty(&self) -> bool;
fn clear(&mut self); // the fault path
fn cut(&mut self, target: usize) -> Option<FrameChunks>; // None iff is_empty()
}
pub(super) struct FrameChunks { chunks: Vec<Bytes>, total: usize }FrameChunks falls out of cut with a private constructor, which makes the (Vec<Bytes>, usize) tuple's sum invariant unrepresentable-if-violated. drain_frame is the only producer of that tuple in the tree today, so this is a two-field struct in the module that already owns the sole constructor — not a refactor.
What it deletes
- Both copies of the "cut no chunks from N queued bytes" desync bail, and their two divergent error strings.
- Both push-site
if len > 0filters. drain_frame'sfront_len == 0branch, which today would silently produce a short-but-correctly-labelled frame.- Both guards in
chunk_frame_bufs— including the sum check, which is already unreachable by construction sincedrain_framecomputestotalfrom the same items it pushes. - The
checked_subandqueue.is_empty()guards added in the perf(node,protocol): zero-copy ChunkData framing via vectored QUIC writes (#1791) #1828 follow-up.
Net: fewer lines than it adds.
Explicitly out of scope
- Do not fold
faultedintoFrameQueue. The two faults are semantically different (a store export fault vs. an encode fault), carry different messages, and the terminal-poison rule is a property of the producer, not of a byte queue. Duplicatingfaulted: boolin each producer is the correct amount of duplication. - Do not unify the two producers behind a trait — they differ in fault and drain semantics.
FrameQueueextracts the shared part and that is where it should stop.
Adjacent, optional
frame_target could return NonZeroUsize, which would delete both if target == 0 guards and make the two zero-target regression tests uncompilable — the right outcome, but it touches ~6 call sites and deserves its own commit.
Metadata
Metadata
Assignees
Labels
Type
Fields
Priority
Projects
- StatusShow more project fieldsDone
Activity
refactor(node): extract FrameQueue/FrameChunks so the serve framers s…