fix(mesh): queue RPC sends per socket so one drain releases one frame (#41)
#152 made each mesh RPC send wait for `drained()`, but each one waited on its own. A drain on a backed-up socket releases every waiter at once (`nodeSocket` keeps them in one list), so they all sent and the buffer overshot the 1 MiB mark by one frame per concurrent RPC: bounded, but not what #152 claims. Found in the second pre-freeze review. `sendWhenDrained` (ws-core, internal) chains sends per socket, and each one waits for `drained()` when its turn comes. The buffer then peaks at the mark plus one frame, however many RPCs wait, and frames go out in call order. The teapot's `reply` and the link's `sendRpc` both use it. The frame is built at send time, so a call that timed out while queued sends nothing, and nothing is sent after the close. Tests: a burst of five 100-byte frames behind a backed-up socket sends one frame per drain, in order, with the buffer at most one frame. It fails when each sender waits on its own. Also: a withdrawn frame is skipped, and a frame queued at close is dropped. Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
4 files changed
+100
-9
No files matched your search
+4
-4
@@ -2,7 +2,7 @@ import type { Bus } from '../bus';
|
||||
import type { Logger } from '../logger';
|
||||
import { HttpError } from '../signals';
|
||||
import { connectSocket, type SocketCtor } from '../http/ws-adapter';
|
||||
import type { WsSocket } from '../http/ws-core';
|
||||
import { sendWhenDrained, type WsSocket } from '../http/ws-core';
|
||||
import { encode, decode, MESH_PROTOCOL_VERSION, type Manifest, type RequestEnvelope, type Frame } from './protocol';
|
||||
|
||||
/** Client handle to a remote mesh server: its manifest, an RPC caller, and a close(). */
|
||||
@@ -121,9 +121,9 @@ function sendRpc(
|
||||
pending.set(id, { name, resolve: resolveCall, reject: rejectCall, timer });
|
||||
// Not before the socket has room (#41); the timeout already runs, so a teapot that stops
|
||||
// reading still answers 504. Settled while waiting (timeout, close): nothing left to send.
|
||||
void (socket.drained?.() ?? Promise.resolve()).then(() => {
|
||||
if (pending.has(id) && !socket.abort.aborted) socket.send(encode({ type: 'rpc-req', id, kind, name, ctx }));
|
||||
});
|
||||
void sendWhenDrained(socket, () =>
|
||||
pending.has(id) ? encode({ type: 'rpc-req', id, kind, name, ctx }) : undefined,
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user