fix(mesh): give the control channel its own inbound queue bound (#148)

The control channel also inherited limits.maxWsQueue (16 undelivered
frames, close 1013) from #107. On Node, ws emits every frame of one
socket read synchronously, so a teacup with more than 16 RPCs in flight
sent a burst the teapot's queue refused: the link dropped and every
pending request failed 503. Deno and Bun deliver one frame per task and
did not trip.

controlLimits() now also raises maxWsQueue for the control channel, to
an internal 4,096 frames or maxWsQueue if larger, alongside the size
bound from #120. The teapot's frame loop never awaits between frames, so
the queue only holds one delivery burst, and one read (at most 64 KiB)
holds fewer than 4,096 of the smallest control frame. The bound stays
finite, so a peer flooding a channel nobody is reading is still cut off.
No new option. User @Ws routes keep closing 1013 at maxWsQueue.

Tests: 64 RPCs in one tick through app.fetch on one link all answer 200
(red on Node before), and a user route on the same teapot still closes
1013, on Node, Deno and Bun.

Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
chrnx committed 2026-10-23 01:13:36 -05:00
1 parent 734a26edb4
commit 1d31a3c3dc
5 files changed
+165 -8

No files matched your search

+19 -5
View File
@@ -42,15 +42,29 @@ export function wsMessageBytes(limits: Pick<RequestLimits, 'maxWsMessageBytes'>
}
/**
* The limits the mesh control channel's socket is bounded by (#120): `limits` with
* `maxWsMessageBytes` raised to the channel's own frame bound when that is larger. A user route
* never goes through this, so it keeps `maxWsMessageBytes` as configured.
* Undelivered inbound frames the mesh control channel may hold (#148), in place of
* `limits.maxWsQueue` when larger. Its consumer, the teapot's frame loop, never awaits between
* frames, so its queue only ever holds one delivery burst: `ws` emits every frame of one socket
* read synchronously, and a read is at most 64 KiB, under 4,096 of the smallest control frame
* (`{"type":"ping"}`). A teacup with many RPCs in flight sends exactly such bursts, which the
* 16-frame user default closed with 1013. Still finite, so a peer flooding the channel while
* nothing reads it — before the boot finishes, say — is still cut off.
*/
const CONTROL_QUEUE = 4096;
/**
* The limits the mesh control channel's socket is bounded by: `limits` with `maxWsMessageBytes`
* raised to the channel's own frame bound (#120) and `maxWsQueue` to {@link CONTROL_QUEUE} (#148),
* each only when larger. A user route never goes through this, so it keeps both as configured.
*/
export function controlLimits(
limits: Pick<RequestLimits, 'maxWsMessageBytes' | 'maxWsQueue'> | undefined,
maxFrameBytes: number | undefined,
): Pick<RequestLimits, 'maxWsQueue'> & { maxWsMessageBytes: number } {
return { ...limits, maxWsMessageBytes: Math.max(wsMessageBytes(limits), maxFrameBytes ?? 0) };
): { maxWsMessageBytes: number; maxWsQueue: number } {
return {
maxWsMessageBytes: Math.max(wsMessageBytes(limits), maxFrameBytes ?? 0),
maxWsQueue: Math.max(limits?.maxWsQueue ?? DEFAULT_MAX_WS_QUEUE, CONTROL_QUEUE),
};
}
/** Throws when a WebSocket limit is present but not a positive safe integer (#107). */