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
@@ -20,6 +20,7 @@ import type { BodyFailure, BodyReader } from './body';
|
||||
import type { RouteDef, WsRouteDef, MeshControl, HttpOptions } from './types';
|
||||
import type { Logger } from '../logger';
|
||||
import { nodeRequire } from '../node-require';
|
||||
import { answerProbe } from './health';
|
||||
|
||||
interface HandlerConfig {
|
||||
routes: RouteDef[];
|
||||
@@ -116,6 +117,8 @@ function createRequestHandler(cfg: HandlerConfig) {
|
||||
const injected = computeInjected(opts, { secure, headers: req.headers });
|
||||
patchResponseHeaders(res, injected);
|
||||
|
||||
if (writeProbe(req, res, opts)) return;
|
||||
|
||||
let result: HandleResult | Preflight;
|
||||
let acquired = false;
|
||||
const watching = watchingRequests(bus);
|
||||
@@ -203,6 +206,20 @@ function createRequestHandler(cfg: HandlerConfig) {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Answers a health probe and returns true, or returns false for any other request. Runs before the
|
||||
* gate, so shedding never refuses a probe, and before `handle()`, so no step, plugin or lifecycle
|
||||
* event sees one. Written through the patched writeHead, so the security headers still land.
|
||||
*/
|
||||
function writeProbe(req: http.IncomingMessage, res: http.ServerResponse, opts: HttpOptions | undefined): boolean {
|
||||
const probe = opts?.health && answerProbe(req.method ?? 'GET', req.url ?? '/', opts.health);
|
||||
if (!probe) return false;
|
||||
|
||||
res.writeHead(probe.status, probe.headers);
|
||||
res.end(probe.body);
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* The last resort for a request whose handling rejected. Deliberately does not call `onError`: this
|
||||
* runs because something already failed past the point where errors convert to a response, and user
|
||||
|
||||
Reference in new issue
Block a user