A streaming-proxy front otherwise opens one backend connection per
request -- fd churn and a connect(2) round trip on the hot path at high
concurrency, plus an ad-hoc length prefix re-invented at each call site.
This module replaces that with a persistent multiplexed transport: many
concurrent logical streams ride one UnixStream, demuxed by a 64-bit
request_id, framed once here.
Wire frame (all integers big-endian)::
| u32 payload_len | u64 request_id | u8 kind | payload[payload_len] |
kind is one of FrameKind.{OPEN, CHUNK, DONE, ERROR, CANCEL}. The
13-byte fixed header means a reader knows the full frame size after the
first 13 bytes, so partial reads reassemble deterministically.
Three layers, smallest first (each is independently testable):
encode_frame / decode_frame -- the pure codec over
ByteWriter / ByteReader. No I/O, no allocation beyond the
payload copy. This is the fuzz/ASan core.
FrameDemux -- a sans-I/O reassembly buffer: feed(bytes) appends
raw bytes (possibly splitting or coalescing frames arbitrarily) and
routes every complete frame into a per-request_id FIFO inbox.
poll(id) / take_all(id) drain a stream's inbox. One stream's
malformed payload never reaches another stream's inbox (isolation);
frames within a stream keep arrival order (ordering).
FrameMux -- ties a FrameDemux and an outbound ByteWriter to
one owned UnixStream: open / send_chunk / done /
cancel queue outbound frames, flush writes them, pump reads
inbound bytes and demuxes. No call ever reconnects -- the connection is
opened once and owned for the mux's lifetime.
FrameDemux accumulates into a single List[UInt8] and
compacts the consumed prefix after each drain (so a slow consumer cannot
make the buffer grow without bound across the consumed bytes). The
limit is one in-flight frame's payload held contiguously; a frame
larger than MAX_FRAME_PAYLOAD is rejected as a protocol error rather
than allocated. open returns nothing here; the single-stream
UpstreamChunkSource (flare.http.async_body) is the reactor-
integrated wrapper over one logical stream.