fix(mesh): RPC frames wait for the send buffer too (CARD-26, #41)
CARD-26 made the WebSocket route pump await `drained()` before each pull, but mesh RPC traffic sends outside that pump, so it never waited: - teapot: `handleRpc` runs concurrently per rpc-req, so a teacup that keeps sending requests and stops reading grows the teapot's buffer by one response each. - link: `sendRpc` sent each rpc-req at once, whatever the buffer held. Both now wait for `drained()` before sending. The teapot drops a response whose socket closed while waiting. The link starts its timeout before waiting, so a teapot that stops reading still answers 504, and a call that settles while waiting (timeout or close) is never sent late. Found in the pre-freeze integrated review. Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
4 files changed
+88
-4
No files matched your search
+11
-2
@@ -170,6 +170,15 @@ function resolveRpc(
|
||||
return deps.resolveRoute(name, ctx);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends one frame once the socket has room (#41): RPCs are served concurrently, so without the wait
|
||||
* a peer that stops reading grows the send buffer by one response per request it keeps sending.
|
||||
*/
|
||||
async function reply(socket: WsSocket, frame: Frame): Promise<void> {
|
||||
if (socket.drained) await socket.drained();
|
||||
if (!socket.abort.aborted) socket.send(encode(frame));
|
||||
}
|
||||
|
||||
/** Serve one authenticated rpc-req: resolve it and frame the ok/error response back to the peer. */
|
||||
async function handleRpc(
|
||||
socket: WsSocket,
|
||||
@@ -196,12 +205,12 @@ async function handleRpc(
|
||||
throw error;
|
||||
}
|
||||
|
||||
socket.send(encode({ type: 'rpc-res', id, ok: true, result }));
|
||||
await reply(socket, { type: 'rpc-res', id, ok: true, result });
|
||||
} catch (err) {
|
||||
const status = isHttpError(err) ? err.status : ((err as any)?.status ?? 500);
|
||||
|
||||
deps.bus?.emit('mesh:rpc:error', { name, error: err });
|
||||
socket.send(encode({ type: 'rpc-res', id, ok: false, error: { message: (err as Error).message, status } }));
|
||||
await reply(socket, { type: 'rpc-res', id, ok: false, error: { message: (err as Error).message, status } });
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user