Skip to main content

Module stream

Module stream 

Source
Expand description

Backpressure-controlled streaming over REPE notifies.

When you need to push a large payload from the server to a peer over REPE (multi-GB capture files, log streams, query results that don’t fit in one message), the protocol gives you notify frames but no flow control. This module adds the missing pieces:

  • ACK-driven window credit. The producer waits before sending if the receiver is more than window_bytes behind on ACKs; when an ACK arrives the producer wakes and resumes.
  • Idle watchdog. A transfer that has gone silent for longer than idle_timeout is force-cancelled so a wedged peer can’t leak server resources.
  • Replay ring + reconnect. The producer keeps a byte-bounded ring of recently-emitted bodies; on a brief WS disconnect it parks until the peer reconnects and calls TransferControl::request_resume with a last_received_offset, then re-emits the ring tail through the new peer.

The protocol itself (the wire shape of transfer_begin / file_chunk / transfer_ack / etc.) is up to you: this module deals only in offsets, ACKs, and opaque body bytes. The concrete embedding – how each chunk reaches the wire – lives in your transport’s sink; a PeerHandle from the built-in WebSocketServer is one such sink.

§Lifecycle

  1. Build a TransferControl with a window size and replay ring capacity. Install the initial peer with set_peer.
  2. Register the control under your transfer-id type in a TransferRegistry so inbound ACK / cancel / resume handlers can find it.
  3. The producer thread, for each chunk:
  4. Inbound ACK handlers call record_ack; cancel calls cancel; resume calls request_resume.
  5. At each file (or whatever your chunked unit is) boundary, call advance_to_file to reset offsets and clear the ring.
  6. When the transfer ends, call TransferRegistry::unregister.

Structs§

PendingResume
Resume request published by the inbound resume handler and consumed by the producer thread on its next call to TransferControl::wait_for_reconnect.
RingChunk
One entry in the replay ring.
TransferControl
Per-transfer control: credit window, ACK accounting, replay ring, peer slot, cancel flag, idle timestamps.
TransferRegistry
Table of in-flight transfers indexed by an embedder-chosen key type K (typically a transfer-id newtype).

Enums§

CreditError
Why TransferControl::wait_for_credit returned without granting credit. The caller maps this to an error so the producer pipeline aborts cleanly.
ReconnectOutcome
Outcome of TransferControl::wait_for_reconnect. The producer uses this to decide whether to swap in a new peer + replay, give up because of cancel, or give up because the reconnect window expired.
ResumeRejection
Why TransferControl::request_resume rejected a resume. The embedder maps these to wire strings on its resume reply.

Constants§

DEFAULT_BACKPRESSURE_TIMEOUT
Default per-chunk credit timeout. If TransferControl::wait_for_credit hasn’t been released by an ACK in this long, the producer should abort the transfer.
DEFAULT_IDLE_TIMEOUT
Default idle-transfer threshold. If neither a chunk has been sent nor an ACK received in this long, the watchdog force-cancels the transfer.
DEFAULT_RECONNECT_TIMEOUT
Default reconnect window. After a disconnect the producer parks on TransferControl::wait_for_reconnect for at most this long before giving up.
DEFAULT_REPLAY_RING_BYTES
Default replay ring capacity (per active file). 64 MiB worth of recent bodies, FIFO-evicted; sized so a brief WS drop can replay the whole window.
DEFAULT_WINDOW_BYTES
Default in-flight wire bytes per transfer the producer allows before it must wait on receiver ACKs. 64 MiB is a reasonable starting point for LAN-class links; lower it on slow links.

Functions§

spawn_watchdog
Spawn a background thread that scans registry periodically and fires TransferControl::cancel(“transfer idle”) on any transfer whose last chunk and last ACK are both older than idle_timeout.