feat(mesh): heartbeat the link, and tell the truth when a teapot is gone
Three bugs, found by writing the tests for the status codes. A teapot with a live control channel could never shut down. Control connections were never registered with the stream registry, so server.close() sat waiting on a connected teacup that had no reason to hang up, and app.close() never resolved. Every other long-lived socket was already tracked; the control channel was the one that skipped it. A downed teapot answered 500 — after 30 seconds. sendRpc queued the frame on a closed socket and waited out the full timeout for a verdict already known. It now rejects immediately with 503: a dead upstream is not "this service broke", and the status is what tells a caller it may retry and an operator where to look. An RPC that times out on a *live* link is 504 — a slow teapot and a gone one are different stories. Heartbeat: each teacup pings its teapots (`mesh.heartbeatMs`, default 15s) and closes a link after two unanswered rounds. This is what catches a half-open connection, where the socket looks open with nobody home — otherwise a teacup only learns on the next request. Ping/pong are mesh frames rather than WebSocket protocol pings because Deno's and Bun's platform WebSocket does not expose ws.ping(), so a protocol-level heartbeat could not work everywhere mesh runs. Added to v1 while the wire still has no deployed peers; later it would be a version bump that refuses every old peer. The heartbeat's first close code was 1011, which silently did nothing: a *client* may only send 1000 or 3000-4999, so it threw `invalid code` — and our neutral close swallows throws, so the link stayed open while looking closed. 4011 mirrors 1011's meaning inside the application range. Reconnect (and reconciling app-scope values, which survive their teapot from cache) stays out — it is a post-beta spec of its own. Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
11 files changed
+378
-13
No files matched your search
+16
-2
@@ -31,13 +31,21 @@ export interface Manifest {
|
||||
routes: RouteEntry[];
|
||||
}
|
||||
|
||||
/** Discriminated union of every message on the mesh wire. The handshake frames carry `v`. */
|
||||
/**
|
||||
* Discriminated union of every message on the mesh wire. The handshake frames carry `v`.
|
||||
*
|
||||
* `ping`/`pong` are application frames rather than WebSocket protocol pings on purpose: the `ws`
|
||||
* package exposes `ws.ping()`, but the platform `WebSocket` that Deno and Bun provide does not,
|
||||
* so a protocol-level heartbeat could not work on every runtime mesh claims to support.
|
||||
*/
|
||||
export type Frame =
|
||||
| { type: 'hello'; v: number; secret: string }
|
||||
| { type: 'manifest'; v: number; scopes: ScopeEntry[]; routes: RouteEntry[] }
|
||||
| { type: 'rpc-req'; id: string; kind: 'scope' | 'route'; name: string; ctx: RequestEnvelope }
|
||||
| { type: 'rpc-res'; id: string; ok: true; result: unknown }
|
||||
| { type: 'rpc-res'; id: string; ok: false; error: { message: string; status?: number } };
|
||||
| { type: 'rpc-res'; id: string; ok: false; error: { message: string; status?: number } }
|
||||
| { type: 'ping' }
|
||||
| { type: 'pong' };
|
||||
|
||||
const isString = (value: unknown): value is string => typeof value === 'string';
|
||||
const isNumber = (value: unknown): boolean => typeof value === 'number';
|
||||
@@ -71,6 +79,12 @@ const SHAPE: Record<Frame['type'], ShapeCheck> = {
|
||||
if (typeof frame.ok !== 'boolean') bad('ok must be a boolean');
|
||||
if (frame.ok === false && !isObject(frame.error)) bad('error must be an object when ok is false');
|
||||
},
|
||||
ping: () => {
|
||||
/* the tag is the whole frame */
|
||||
},
|
||||
pong: () => {
|
||||
/* the tag is the whole frame */
|
||||
},
|
||||
};
|
||||
|
||||
/** Serialize a frame to a JSON wire string. */
|
||||
|
||||
Reference in new issue
Block a user