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
@@ -0,0 +1,139 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { createApp, Step, Route, Get, Module, needs } from '../../src/index';
|
||||
|
||||
@Step({ provides: 'auth', needs: [], export: true })
|
||||
class Auth {
|
||||
run() {
|
||||
return { auth: { ok: true } };
|
||||
}
|
||||
}
|
||||
@Module({ mountpoint: '/api', steps: [Auth] })
|
||||
class TeapotModule {}
|
||||
|
||||
@Route('/local')
|
||||
class LocalCtl {
|
||||
@Get('/who')
|
||||
who(@needs('auth') auth: any) {
|
||||
return { auth };
|
||||
}
|
||||
}
|
||||
@Module({ mountpoint: '/api', controllers: [LocalCtl] })
|
||||
class TeacupModule {}
|
||||
|
||||
const collect = () => {
|
||||
const lines: string[] = [];
|
||||
return { lines, logger: { debug() {}, info() {}, warn: (m: string) => lines.push(m), error: (m: string) => lines.push(m) } };
|
||||
};
|
||||
|
||||
describe('mesh boot retry', () => {
|
||||
it('waits for a teapot that starts late, rather than failing the deploy', async () => {
|
||||
const port = await new Promise<number>((resolve) => {
|
||||
const probe = require('node:net').createServer();
|
||||
probe.listen(0, () => {
|
||||
const { port: p } = probe.address();
|
||||
probe.close(() => resolve(p));
|
||||
});
|
||||
});
|
||||
const { lines, logger } = collect();
|
||||
|
||||
const teacup = createApp({
|
||||
modules: [TeacupModule],
|
||||
experimental: true,
|
||||
logger,
|
||||
mesh: {
|
||||
teapots: [{ url: `ws://127.0.0.1:${port}/__mesh__/control`, secret: 'good' }],
|
||||
timeoutMs: 500,
|
||||
bootTimeoutMs: 4000,
|
||||
},
|
||||
});
|
||||
|
||||
// the teapot arrives *after* the teacup has already started trying
|
||||
const pending = teacup.fetch(new Request('http://x/api/local/who'));
|
||||
const teapot = createApp({ modules: [TeapotModule], experimental: true, mesh: { secret: 'good' } });
|
||||
await new Promise((r) => setTimeout(r, 300));
|
||||
const server = await teapot.listen(port);
|
||||
|
||||
try {
|
||||
const res = await pending;
|
||||
expect(res.status).toBe(200);
|
||||
expect(lines.some((l) => l.includes('of boot budget left'))).toBe(true);
|
||||
} finally {
|
||||
await teacup.close();
|
||||
await teapot.close();
|
||||
server.close();
|
||||
}
|
||||
}, 15_000);
|
||||
|
||||
it('still fails the boot once the budget is spent — a needed provider is not optional', async () => {
|
||||
const { lines, logger } = collect();
|
||||
const teacup = createApp({
|
||||
modules: [TeacupModule],
|
||||
experimental: true,
|
||||
logger,
|
||||
mesh: {
|
||||
teapots: [{ url: 'ws://127.0.0.1:1/__mesh__/control', secret: 'good' }],
|
||||
timeoutMs: 200,
|
||||
bootTimeoutMs: 600,
|
||||
},
|
||||
});
|
||||
|
||||
try {
|
||||
await expect(teacup.fetch(new Request('http://x/api/local/who'))).rejects.toThrow(/mesh/);
|
||||
expect(lines.some((l) => l.includes('giving up after'))).toBe(true);
|
||||
} finally {
|
||||
await teacup.close();
|
||||
}
|
||||
}, 15_000);
|
||||
|
||||
it('does not retry a wrong secret — the teapot refused, and it will refuse again', async () => {
|
||||
const teapot = createApp({ modules: [TeapotModule], experimental: true, mesh: { secret: 'good' } });
|
||||
const server = await teapot.listen(0);
|
||||
const port = (server.address() as { port: number }).port;
|
||||
const { lines, logger } = collect();
|
||||
|
||||
const teacup = createApp({
|
||||
modules: [TeacupModule],
|
||||
experimental: true,
|
||||
logger,
|
||||
mesh: {
|
||||
teapots: [{ url: `ws://127.0.0.1:${port}/__mesh__/control`, secret: 'WRONG' }],
|
||||
timeoutMs: 500,
|
||||
bootTimeoutMs: 30_000,
|
||||
},
|
||||
});
|
||||
|
||||
try {
|
||||
const started = Date.now();
|
||||
await expect(teacup.fetch(new Request('http://x/api/local/who'))).rejects.toThrow(/mesh/);
|
||||
// with a 30s budget, retrying would have taken 30s to reach the same error
|
||||
expect(Date.now() - started).toBeLessThan(3000);
|
||||
expect(lines.some((l) => l.includes('refused this peer'))).toBe(true);
|
||||
expect(lines.filter((l) => l.includes('of boot budget left'))).toEqual([]);
|
||||
} finally {
|
||||
await teacup.close();
|
||||
await teapot.close();
|
||||
server.close();
|
||||
}
|
||||
}, 40_000);
|
||||
|
||||
it('bootTimeoutMs: 0 makes one attempt, as before', async () => {
|
||||
const { lines, logger } = collect();
|
||||
const teacup = createApp({
|
||||
modules: [TeacupModule],
|
||||
experimental: true,
|
||||
logger,
|
||||
mesh: {
|
||||
teapots: [{ url: 'ws://127.0.0.1:1/__mesh__/control', secret: 'good' }],
|
||||
timeoutMs: 200,
|
||||
bootTimeoutMs: 0,
|
||||
},
|
||||
});
|
||||
|
||||
try {
|
||||
await expect(teacup.fetch(new Request('http://x/api/local/who'))).rejects.toThrow(/mesh/);
|
||||
expect(lines.filter((l) => l.includes('of boot budget left'))).toEqual([]);
|
||||
} finally {
|
||||
await teacup.close();
|
||||
}
|
||||
}, 15_000);
|
||||
});
|
||||
Reference in new issue
Block a user