export { FlowControl }; export type { FlowControlEmit }; interface FlowControlEmit { byteWindowUpdate(bytes: number): void; msgWindowUpdate(count: number): void; bdpPing(): void; } /** * Per-channel flow control. Two orthogonal credit axes coordinated by a single * BDP probe per RTT, plus a self-utilisation gate that applies symmetrically * to growth (receiver-side) and to outbound pacing (sender-side). * * 1. **Byte credit** (`_peerWindow` / `WINDOW` frame) bounds in-flight memory. * 2. **Message-count credit** (`_peerMsgWindow` / `MSG_WINDOW` frame) bounds * in-flight per-RTT *message count* regardless of size. * 3. **Self-utilisation gate**: `_recordSelfTime(ms)` accumulates this channel's * sync work into a two-bucket rolling sum. Above `FC_SELF_UTIL_THRESHOLD`, * `onPingAck` refuses to grow either window and `decrement` yields a * macrotask before returning. */ declare class FlowControl { private readonly _emit; private _bdp; private _peerWindow; private _peerMsgWindow; private _consumedBytes; private _consumedMessages; /** Single deferred shared by all senders blocked on credit. */ private _creditReady; private _shutdown; private _prevBucket; private _curBucket; private _curBucketStart; private _openedAt; constructor(_emit: FlowControlEmit); get byteWindow(): number; get msgWindow(): number; /** Sender-side: decrement credit for one frame of `bytes`. Returns `void` when * both credit axes have headroom AND our loop utilisation is below the gate. * Otherwise a Promise that resolves on credit refresh (credit-gated) or one * macrotask later (util-gated — single yield, no re-check). */ decrement(bytes: number): void | Promise; onPeerByteWindow(bytes: number): void; onPeerMessageWindow(count: number): void; /** Receiver-side: account one received frame off the wire. Emits a * `BDP_PING` via the channel's emit callback iff the estimator opens a probe. */ onReceived(bytes: number): void; /** Receiver-side: account post-callback consumption of one frame. Emits * refresh `WINDOW` / `MSG_WINDOW` frames once a quarter of either window * has been consumed. */ onConsumed(bytes: number): void; /** Settle `BDP_PING_ACK`. Each axis grows iff its own sample saturated ≥ 2/3 * of its current window AND our own self-utilisation is below threshold. * On growth, the new window is emitted to the peer immediately. */ onPingAck(): void; /** Channel calls this after the sync portion of a send or a receive-dispatch * to report how much wall-clock that work consumed. */ _recordSelfTime(durationMs: number): void; /** Telefunc's share of wall-clock time on this channel over the last * `FC_SELF_TIME_WINDOW_MS`, in [0, 1]. While the channel is younger than the * window, denominates by lifetime instead. */ private _selfUtilisation; private _advanceBuckets; /** Transport reattach: reset sender-side credit on both axes to initial. */ reset(): void; /** Bump to the batch-POST initial window and advertise it. Grow-only / idempotent. */ useBatchTransportInitial(): void; shutdown(): void; private _tryWakeCreditWaiters; private _waitForCredit; }