feat(http): liveness and readiness probes with a drain window (CARD-17, #115)
createApp({ health: true }) serves GET/HEAD /__live__ and /__ready__.
Ready reports booting until boot() resolves, ok or degraded (from
app.degraded()) after that, and draining from the moment close() starts.
Probes are answered by the adapters, ahead of the request gate and of
handle(): Node in serve() (src/http/server.ts), the Fetch path in
app.fetch before it awaits the boot. Otherwise ready could never say
booting. Being ahead of the gate means maxConcurrentRequests never sheds
a probe (D14). Being ahead of handle() means no step, provider, plugin or
onError runs, and no lifecycle event is emitted, so logRequests and
metrics never see a probe. Security headers still apply, plus
cache-control: no-store. Other methods get 405 with Allow: GET, HEAD.
A probe through app.fetch before any boot starts the boot without
awaiting it. On the edge, or under a bare Deno.serve(app.fetch), nothing
else may boot the app, and an orchestrator that waits for ready before
sending traffic would wait forever.
health: { drainDelayMs } (D14) marks the app draining and keeps the
listener open for that long before connections drain. Without it, Node
closes idle keep-alive sockets at once, and a probe sees a reset rather
than the 503. The window is spent out of the close budget. serveDeno and
serveBun start the same window, through a symbol-keyed hook on the app,
before server.shutdown() / server.stop(false). workerd has no close(),
so it never reports draining.
While health is on, /__live__ and /__ready__ join the reserved-path
boot check that CARD-21 introduced, for local and mesh-spliced routes.
Invalid health values throw at createApp.
Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
15 files changed
+789
-10
No files matched your search
@@ -121,3 +121,29 @@ Deno.test('serveDeno rejects when a provider fails, instead of binding a port th
|
||||
throw new Error(`expected serveDeno to reject with the provider's error, got: ${message || '<resolved>'}`);
|
||||
}
|
||||
});
|
||||
|
||||
// CARD-17 T5 (D14): close() marks the app draining before the server shuts down, and the drain window
|
||||
// keeps the listener open long enough for a probe to read the 503.
|
||||
Deno.test('serveDeno close() with health.drainDelayMs: /__ready__ 503 draining, in-flight request 200', async () => {
|
||||
const app = createApp({ modules: [M], health: { drainDelayMs: 300 } });
|
||||
const server = await serveDeno(app, { port: 0, onListen: () => {} });
|
||||
const base = `http://127.0.0.1:${portOf(server)}`;
|
||||
|
||||
const ready = await fetch(`${base}/__ready__`);
|
||||
if (ready.status !== 200) throw new Error(`ready before close: ${ready.status}`);
|
||||
await ready.body?.cancel();
|
||||
|
||||
const inFlight = fetch(`${base}/slow`).then((r) => r.json());
|
||||
await new Promise((r) => setTimeout(r, 50));
|
||||
|
||||
const closing = server.close({ timeoutMs: 5000 });
|
||||
const probe = await fetch(`${base}/__ready__`);
|
||||
const body = await probe.json();
|
||||
if (probe.status !== 503 || body.status !== 'draining') {
|
||||
throw new Error(`probe during close: ${probe.status} ${JSON.stringify(body)}`);
|
||||
}
|
||||
|
||||
const done = await inFlight;
|
||||
if (done.ok !== true) throw new Error(`in-flight request was cut: ${JSON.stringify(done)}`);
|
||||
await closing;
|
||||
});
|
||||
Reference in new issue
Block a user