The teacup built its route table once at boot. A teapot that started
late was never served, and one that was gone for good stayed advertised.
Implement D15 (accepted 2026-10-18):
- A teapot that missed bootTimeoutMs is retried in the background with
the reconnect backoff (not with reconnect: false). On connect its
routes join the remote table. Its steps do not, because the graph
was validated at boot without them.
- While a link is reconnecting, its routes stay and answer 503, now
with retry-after (seconds until the next attempt).
- A link that gives up for good (dropped with reconnect: false, or a
permanent refusal while reconnecting) has its routes removed: 404,
and Allow drops its methods. A refused reconnect no longer retries
forever.
- A returning manifest replaces the teapot's routes. missingFromManifest
now checks steps only, so a dropped route is removed instead of
refusing the reconnect.
- A late route that conflicts with another teapot's is refused and
logged. The table keeps serving. At boot the same conflict still
fails boot.
- Each change emits mesh:routes with { url, added, removed }.
- The reserved-path check from CARD-21/CARD-17 (#141, #145) runs in
the table for every remote route: at boot a route on /__mesh__/control
(or /__live__ and /__ready__ with health on) fails boot as before. A
late or returning one is refused and logged, and the teapot's other
routes are served.
Mechanics: the remote table publishes a new array per change
(copy-on-write). app.fetch and listen() share one table through a
getter, and buildFetch/createHttpServer read it once per request, so a
request sees the old table or the new one, never a mix. The router
reuses local trees when the local entries are identical, so a
membership change rebuilds only the remote trees. Dev routes now sit
before remote ones in the table to keep the local positions stable.
Tests cover T1-T8 on Node (test/mesh/membership.test.ts), plus the
local-tree build counter and late reserved-path routes, and T1/T2 on
Deno and Bun.
The frozen owner-mesh-lifetime.json capture changes in the next commit.
Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
171 lines
6.7 KiB
TypeScript
171 lines
6.7 KiB
TypeScript
import { describe, it, expect } from 'vitest';
|
|
import { WebSocketServer } from 'ws';
|
|
import { connectLink, missingFromManifest } from '../../src/mesh/link';
|
|
import { encode, decode, MESH_PROTOCOL_VERSION, type Manifest } from '../../src/mesh/protocol';
|
|
|
|
const V = MESH_PROTOCOL_VERSION;
|
|
const env = { method: 'GET', params: {}, query: {}, body: undefined, headers: {} };
|
|
|
|
/**
|
|
* A teapot bound to a fixed port so it can be killed and restarted at the same URL — which is the
|
|
* whole point here: reconnection means returning to the *same* teapot, not finding another one.
|
|
* Its manifest is swappable so a restart can come back exporting something different.
|
|
*/
|
|
function teapotOn(port: number, manifest: Manifest, answer: unknown = { ok: true }) {
|
|
const wss = new WebSocketServer({ port });
|
|
wss.on('connection', (ws) => {
|
|
let authed = false;
|
|
ws.on('message', (d) => {
|
|
const frame = decode(d.toString());
|
|
if (!authed) {
|
|
if (frame.type === 'hello' && frame.secret === 'good') {
|
|
authed = true;
|
|
ws.send(encode({ type: 'manifest', v: V, steps: manifest.steps, routes: manifest.routes }));
|
|
} else ws.close(1008);
|
|
return;
|
|
}
|
|
if (frame.type === 'ping') ws.send(encode({ type: 'pong' }));
|
|
if (frame.type === 'rpc-req') ws.send(encode({ type: 'rpc-res', id: frame.id, ok: true, result: answer }));
|
|
});
|
|
});
|
|
|
|
return new Promise<{ close: () => Promise<void> }>((resolve) => {
|
|
wss.on('listening', () =>
|
|
resolve({
|
|
close: () =>
|
|
new Promise<void>((done) => {
|
|
for (const client of wss.clients) client.terminate();
|
|
wss.close(() => done());
|
|
}),
|
|
}),
|
|
);
|
|
});
|
|
}
|
|
|
|
/** A free port, taken by opening and immediately closing a server on port 0. */
|
|
async function freePort(): Promise<number> {
|
|
const probe = new WebSocketServer({ port: 0 });
|
|
await new Promise((r) => probe.on('listening', r));
|
|
const port = (probe.address() as { port: number }).port;
|
|
await new Promise((r) => probe.close(r));
|
|
|
|
return port;
|
|
}
|
|
|
|
const settle = (ms: number) => new Promise((r) => setTimeout(r, ms));
|
|
const url = (port: number) => `ws://127.0.0.1:${port}/__mesh__/control`;
|
|
const STEPS: Manifest = { steps: ['auth'], routes: [] };
|
|
const fast = { initialDelayMs: 20, maxDelayMs: 60 };
|
|
|
|
describe('mesh reconnection', () => {
|
|
it('reconnects to the same teapot and serves again', async () => {
|
|
const port = await freePort();
|
|
let teapot = await teapotOn(port, STEPS);
|
|
const link = await connectLink({ url: url(port), secret: 'good', reconnect: fast, heartbeatMs: 50 });
|
|
|
|
expect(await link.rpc('scope', 'auth', env)).toEqual({ ok: true });
|
|
|
|
await teapot.close();
|
|
await settle(50);
|
|
// the window where the link is down answers immediately rather than after timeoutMs
|
|
await expect(link.rpc('scope', 'auth', env)).rejects.toMatchObject({ status: 503 });
|
|
|
|
teapot = await teapotOn(port, STEPS, { ok: 'again' });
|
|
await settle(300);
|
|
|
|
expect(await link.rpc('scope', 'auth', env)).toEqual({ ok: 'again' });
|
|
link.close();
|
|
await teapot.close();
|
|
});
|
|
|
|
it('refuses a returning teapot whose manifest lost a token the graph needs', async () => {
|
|
const port = await freePort();
|
|
let teapot = await teapotOn(port, STEPS);
|
|
const warnings: string[] = [];
|
|
const logger = { debug() {}, info() {}, warn: (m: string) => warnings.push(m), error() {} };
|
|
const link = await connectLink({ url: url(port), secret: 'good', reconnect: fast, heartbeatMs: 50, logger });
|
|
|
|
await teapot.close();
|
|
await settle(50);
|
|
|
|
// comes back exporting nothing — the graph was validated against 'auth' at boot
|
|
teapot = await teapotOn(port, { steps: [], routes: [] });
|
|
await settle(300);
|
|
|
|
await expect(link.rpc('scope', 'auth', env)).rejects.toMatchObject({ status: 503 });
|
|
expect(warnings.some((w) => w.includes("step 'auth'") && w.includes('refusing to reconnect'))).toBe(true);
|
|
// logged once per distinct manifest, not once per attempt
|
|
expect(warnings.filter((w) => w.includes('refusing to reconnect')).length).toBe(1);
|
|
|
|
link.close();
|
|
await teapot.close();
|
|
});
|
|
|
|
it('stops reconnecting once the application closes the link', async () => {
|
|
const port = await freePort();
|
|
const teapot = await teapotOn(port, STEPS);
|
|
const link = await connectLink({ url: url(port), secret: 'good', reconnect: fast, heartbeatMs: 50 });
|
|
|
|
await teapot.close();
|
|
link.close();
|
|
|
|
// if close() were not terminal, the link would reconnect to the teapot that comes back here —
|
|
// which is what would leave app.close() with a process that cannot exit
|
|
const revived = await teapotOn(port, STEPS, { ok: 'revived' });
|
|
await settle(300);
|
|
|
|
await expect(link.rpc('scope', 'auth', env)).rejects.toMatchObject({ status: 503 });
|
|
await revived.close();
|
|
});
|
|
|
|
it('reconnect: false keeps the old fail-once behaviour', async () => {
|
|
const port = await freePort();
|
|
let teapot = await teapotOn(port, STEPS);
|
|
const link = await connectLink({ url: url(port), secret: 'good', reconnect: false, heartbeatMs: 50 });
|
|
|
|
await teapot.close();
|
|
teapot = await teapotOn(port, STEPS);
|
|
await settle(300);
|
|
|
|
await expect(link.rpc('scope', 'auth', env)).rejects.toMatchObject({ status: 503 });
|
|
link.close();
|
|
await teapot.close();
|
|
});
|
|
});
|
|
|
|
describe('missingFromManifest', () => {
|
|
const booted: Manifest = {
|
|
steps: ['auth', 'config'],
|
|
routes: [{ method: 'GET', pattern: '/svc/:id' }],
|
|
};
|
|
|
|
it('accepts an identical manifest', () => {
|
|
expect(missingFromManifest(booted, booted)).toEqual([]);
|
|
});
|
|
|
|
it('accepts extra exports — extra steps are ignored, since the graph is fixed at boot', () => {
|
|
const wider: Manifest = {
|
|
steps: [...booted.steps, 'billing'],
|
|
routes: [...booted.routes, { method: 'POST', pattern: '/svc/new' }],
|
|
};
|
|
expect(missingFromManifest(booted, wider)).toEqual([]);
|
|
});
|
|
|
|
it('accepts a route parameter that was only renamed', () => {
|
|
const renamed: Manifest = { steps: booted.steps, routes: [{ method: 'GET', pattern: '/svc/:name' }] };
|
|
expect(missingFromManifest(booted, renamed)).toEqual([]);
|
|
});
|
|
|
|
it('reports a step token that vanished, since the graph resolved it under the old manifest', () => {
|
|
const shrunk: Manifest = { steps: ['config'], routes: booted.routes };
|
|
expect(missingFromManifest(booted, shrunk)).toEqual(["step 'auth'"]);
|
|
});
|
|
|
|
// D15 (CARD-19): routes are not part of the graph, so a returning teapot's routes replace the old
|
|
// ones (a dropped route is removed from the remote table) instead of refusing the reconnect.
|
|
it('does not report a missing route — routes follow the returning manifest', () => {
|
|
const routeless: Manifest = { steps: booted.steps, routes: [] };
|
|
expect(missingFromManifest(booted, routeless)).toEqual([]);
|
|
});
|
|
});
|