feat(mesh): give boot a grace period for a slow teapot, and fail for an absent one
A teapot that is thirty seconds behind and a teapot that does not exist look identical for the first thirty seconds, and only one of them should stop a deploy. bootTimeoutMs is that grace: retries with backoff until it passes. It still fails after that, deliberately. A provider the graph depends on is not optional, and booting without it would only move the failure to the first request, where it is a caller's 503 rather than the deploy's error. The full battery caught what the first cut got wrong: a bad secret was being retried. That is not a transient failure — it is the teapot's decision, and it will be the same decision in thirty seconds, so retrying only spends the deploy's patience to arrive at the identical error. Refusals now fail at once. Telling them apart without threading close codes through the neutral socket contract of four runtimes: a peer that accepted the connection and then hung up before answering rejected us on purpose; one that never accepted it may simply not be listening yet. That is exactly the distinction needed here. Retries are logged and emitted as mesh:boot:retry, kept separate from mesh:disconnect — that one is a link that was up and went away, this one never came up, and a deploy watching for trouble wants to tell those apart. 437 vitest / 12 Deno / 7 Bun / edge / JSR dry run. Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
6 files changed
+277
-15
No files matched your search
+35
-3
@@ -30,6 +30,26 @@ export interface ReconnectOptions {
|
||||
*/
|
||||
export type ManifestPolicy = 'refuse';
|
||||
|
||||
/**
|
||||
* A connection failure the peer decided, rather than one the network caused.
|
||||
*
|
||||
* The distinction is what boot retry needs: a refused secret or a version mismatch will fail
|
||||
* identically forever, so retrying one spends a deploy's patience to reach the same error. It is
|
||||
* derived from whether the socket ever *opened* — a peer that accepted the connection and then hung
|
||||
* up before the manifest rejected us on purpose; one that never accepted it may simply not be up.
|
||||
*/
|
||||
export function isPermanentRefusal(error: unknown): boolean {
|
||||
return (error as { meshRefused?: boolean } | undefined)?.meshRefused === true;
|
||||
}
|
||||
|
||||
/** Tag an error as a decision by the peer, so the caller does not retry it. */
|
||||
function refused(message: string): Error {
|
||||
const error = new Error(message) as Error & { meshRefused: boolean };
|
||||
error.meshRefused = true;
|
||||
|
||||
return error;
|
||||
}
|
||||
|
||||
/** An in-flight RPC awaiting its `rpc-res`, keyed by id in the pending map. */
|
||||
type PendingEntry = {
|
||||
/** The token or route asked for. Kept so an error can be reported by name — the frame only carries the id. */
|
||||
@@ -185,7 +205,7 @@ function settleManifest(frame: Extract<Frame, { type: 'manifest' }>, hs: Handsha
|
||||
if (frame.v !== MESH_PROTOCOL_VERSION) {
|
||||
hs.socket.close();
|
||||
hs.reject(
|
||||
new Error(
|
||||
refused(
|
||||
`mesh protocol version mismatch at ${hs.url}: teapot speaks v${frame.v}, this teacup speaks v${MESH_PROTOCOL_VERSION}`,
|
||||
),
|
||||
);
|
||||
@@ -242,6 +262,7 @@ function openSession(args: SessionArgs): Promise<Session> {
|
||||
const { socket, opened } = connectSocket(args.url, args.Ctor ?? undefined);
|
||||
let heartbeat: Session['heartbeat'] | undefined;
|
||||
let handshook = false;
|
||||
let accepted = false;
|
||||
|
||||
return new Promise<Session>((resolve, reject) => {
|
||||
const connectTimer = setTimeout(() => {
|
||||
@@ -268,7 +289,10 @@ function openSession(args: SessionArgs): Promise<Session> {
|
||||
};
|
||||
|
||||
opened.then(
|
||||
() => socket.send(encode({ type: 'hello', v: MESH_PROTOCOL_VERSION, secret: args.secret })),
|
||||
() => {
|
||||
accepted = true;
|
||||
socket.send(encode({ type: 'hello', v: MESH_PROTOCOL_VERSION, secret: args.secret }));
|
||||
},
|
||||
(error) => reject(error),
|
||||
);
|
||||
|
||||
@@ -279,7 +303,15 @@ function openSession(args: SessionArgs): Promise<Session> {
|
||||
() => {
|
||||
clearTimeout(connectTimer);
|
||||
heartbeat?.stop();
|
||||
if (!handshook) reject(new Error(`mesh handshake failed: ${args.url}`));
|
||||
|
||||
if (!handshook) {
|
||||
// Opened and then hung up before answering: the teapot refused this peer — a bad secret,
|
||||
// or a version it would not speak. Never opened: it may just not be listening yet.
|
||||
const message = `mesh handshake failed: ${args.url}`;
|
||||
|
||||
reject(accepted ? refused(message) : new Error(message));
|
||||
}
|
||||
|
||||
args.onEnd();
|
||||
},
|
||||
{ once: true },
|
||||
|
||||
Reference in new issue
Block a user