feat(http): liveness and readiness probes with a drain window (CARD-17, #115) #145

Merged
chrnx merged 1 commits from feature/fprd-001c-health into develop 2026-10-23 05:58:59 +00:00
Owner

Depends on #141 (CARD-21). Review and merge after it. This branch stacks on feature/fprd-001c-mesh-reserved-path, so until #141 merges the diff also shows its two commits. The CARD-17 change is the single commit feat(http): liveness and readiness probes with a drain window (CARD-17, #115).

What and why

CARD-17 (core #115), decision D14 (health: true | { drainDelayMs?: number }; probes bypass shedding; the edge never reports draining).

  • createApp({ health }), off by default. GET/HEAD /__live__ returns 200 {"status":"ok"}. /__ready__ returns:
    • 503 booting until boot() resolves,
    • then 200 ok or 200 degraded with degraded: [...] from app.degraded(),
    • then 503 draining from the first line of close().
  • Where probes are answered (src/http/health.ts, new): ahead of the request gate and handle().
    • Node: in serve() (src/http/server.ts, the writeProbe helper).
    • Fetch: in app.fetch before it awaits the boot (answerFetchProbe in src/http/web.ts). That is the only place /__ready__ can still say booting.
    • Because probes never reach the gate or the pipeline, they are never shed, run no step, provider, plugin or onError, and emit no lifecycle event. Security headers are still merged in, plus cache-control: no-store. Other methods get 405 with Allow: GET, HEAD.
  • State: trackHealth wraps the memoised boot, so a resolved boot moves the app to ready. drain() sets draining and holds for drainDelayMs, once. The phase only moves forward.
  • Drain:
    • Node app.close() drains first. The window is spent out of the close budget, so close() still returns within timeoutMs.
    • serveDeno/serveBun close() call the same drain through a symbol-keyed hook on the app (HEALTH_DRAIN, not public API) before server.shutdown() / server.stop(false).
    • workerd has no close(), so it never reports draining.
  • Reserved paths: while health is on, /__live__ and /__ready__ join the boot check CARD-21 introduced (src/http/reserved-paths.ts), for local and mesh-spliced routes. With health off they are ordinary paths.
  • Validation: createApp throws on any health other than true, false or { drainDelayMs?: number }. That includes unknown keys such as checks, a negative or non-finite delay, and drainDelayMs + teardownTimeoutMs > shutdownTimeoutMs.
  • JSDoc on the option and on App.close(). README bullet. CHANGELOG under ### Added.

Choices the card left open: please confirm

  1. A probe through app.fetch before any boot starts the boot without waiting for it. Without this, on the edge or under a bare Deno.serve(app.fetch), an orchestrator that waits for ready before sending traffic would see booting forever, because only a real request would boot the app. The rejection is swallowed only on the probe path; the boot memo keeps it, so real requests still fail with it.
  2. A failed boot leaves /__ready__ at 503 booting. The card has no failed state, and I didn't invent one.
  3. The drain window comes out of the shutdown budget, matching how teardownTimeoutMs is carved out. A drainDelayMs that can't fit throws at createApp, the way teardownTimeoutMs > shutdownTimeoutMs already does. The card only says "invalid health throws", so this bound is my addition.
  4. Only the exact paths are probes. /__ready__/ (trailing slash) goes to the route table as usual. A query string is ignored.
  5. On the Node listener and serveDeno/serveBun the app boots before it binds, so booting is only observable through app.fetch (T1). Both runtimes run T5 and see ok and draining. T2 and T3 run through shared code on Node.
  6. Bun's stop(false) and Deno's shutdown() behaviour after the window is unchanged; D14's runtime limits apply.

Test evidence (local, on develop with #134 and #135)

test/health.test.ts:

  • T1 (slow provider: booting then ok, and a probe starting the boot), T2, T3
  • T4 on Node: a kept-alive probe connection gets 503 draining during close() while /slow completes 200. With drainDelayMs: 0 the same test gets ECONNRESET, which is D14's rationale, checked by hand.
  • T6 (maxConcurrentRequests: 1 saturated: probes 200, others 503), T7 (POST 405 with Allow; HEAD with no body)
  • T8 (@Get('/__live__') boot error, exact message), /__ready__ HTTP and WS with health on and off, teapot route at /__ready__
  • T9 (10 probes with logRequests: no log line, bus.emit never called, a plugin step never runs, with a control request)
  • invalid values, and the budget check

T5 runs in test/deno/shutdown.test.ts and test/bun/shutdown.test.ts. T10 runs in test/edge/run.ts: live 200, ready booting then ok with only probes reaching the isolate.

npm run lint            0 errors (52 warnings, unchanged: no-explicit-any)
npm run format:check    All matched files use Prettier code style!
npm run typecheck       clean
npm test                Test Files 94 passed (94) · Tests 946 passed (946)
npm run build           CJS/ESM/DTS build success
npm run complexity:check  every function <= 25
npm run test:deno       38 passed | 0 failed
npm run test:bun        28 pass · 0 fail
npm run test:edge       ... + reserved path + health — PASS; router parity 1158 + 55 — PASS
npm audit --audit-level=moderate   found 0 vulnerabilities

Docs follow-up (CARD-12)

  • reference/createapp.md: the health option: shape, states and bodies, 405, cache-control, drainDelayMs and its budget rule, the probe-starts-boot behaviour. Add /__live__ and /__ready__ to the reserved-paths list started for CARD-21.
  • Deployment and shutdown guide: Kubernetes and load-balancer readiness with drainDelayMs versus a platform preStop sleep (PRD open question), why Node needs the window (idle keep-alive sockets close at once), and Deno/Bun close() behaviour.
  • Runtimes page: the edge never reports draining, and booting is per isolate.
**Depends on #141 (CARD-21). Review and merge after it.** This branch stacks on `feature/fprd-001c-mesh-reserved-path`, so until #141 merges the diff also shows its two commits. The CARD-17 change is the single commit `feat(http): liveness and readiness probes with a drain window (CARD-17, #115)`. ## What and why CARD-17 (core #115), decision **D14** (`health: true | { drainDelayMs?: number }`; probes bypass shedding; the edge never reports draining). - `createApp({ health })`, off by default. `GET`/`HEAD /__live__` returns `200 {"status":"ok"}`. `/__ready__` returns: - `503 booting` until `boot()` resolves, - then `200 ok` or `200 degraded` with `degraded: [...]` from `app.degraded()`, - then `503 draining` from the first line of `close()`. - **Where probes are answered** (`src/http/health.ts`, new): ahead of the request gate and `handle()`. - Node: in `serve()` (`src/http/server.ts`, the `writeProbe` helper). - Fetch: in `app.fetch` **before it awaits the boot** (`answerFetchProbe` in `src/http/web.ts`). That is the only place `/__ready__` can still say `booting`. - Because probes never reach the gate or the pipeline, they are never shed, run no step, provider, plugin or `onError`, and emit no lifecycle event. Security headers are still merged in, plus `cache-control: no-store`. Other methods get `405` with `Allow: GET, HEAD`. - **State**: `trackHealth` wraps the memoised boot, so a resolved boot moves the app to ready. `drain()` sets draining and holds for `drainDelayMs`, once. The phase only moves forward. - **Drain**: - Node `app.close()` drains first. The window is spent out of the close budget, so `close()` still returns within `timeoutMs`. - `serveDeno`/`serveBun` `close()` call the same drain through a symbol-keyed hook on the app (`HEALTH_DRAIN`, not public API) before `server.shutdown()` / `server.stop(false)`. - workerd has no `close()`, so it never reports draining. - **Reserved paths**: while `health` is on, `/__live__` and `/__ready__` join the boot check CARD-21 introduced (`src/http/reserved-paths.ts`), for local and mesh-spliced routes. With health off they are ordinary paths. - **Validation**: `createApp` throws on any `health` other than `true`, `false` or `{ drainDelayMs?: number }`. That includes unknown keys such as `checks`, a negative or non-finite delay, and `drainDelayMs + teardownTimeoutMs > shutdownTimeoutMs`. - JSDoc on the option and on `App.close()`. README bullet. `CHANGELOG` under `### Added`. ## Choices the card left open: please confirm 1. **A probe through `app.fetch` before any boot starts the boot without waiting for it.** Without this, on the edge or under a bare `Deno.serve(app.fetch)`, an orchestrator that waits for ready before sending traffic would see `booting` forever, because only a real request would boot the app. The rejection is swallowed only on the probe path; the boot memo keeps it, so real requests still fail with it. 2. **A failed boot leaves `/__ready__` at `503 booting`.** The card has no failed state, and I didn't invent one. 3. **The drain window comes out of the shutdown budget**, matching how `teardownTimeoutMs` is carved out. A `drainDelayMs` that can't fit throws at `createApp`, the way `teardownTimeoutMs > shutdownTimeoutMs` already does. The card only says "invalid health throws", so this bound is my addition. 4. **Only the exact paths are probes.** `/__ready__/` (trailing slash) goes to the route table as usual. A query string is ignored. 5. On the Node listener and `serveDeno`/`serveBun` the app boots before it binds, so `booting` is only observable through `app.fetch` (T1). Both runtimes run T5 and see ok and draining. T2 and T3 run through shared code on Node. 6. Bun's `stop(false)` and Deno's `shutdown()` behaviour after the window is unchanged; D14's runtime limits apply. ## Test evidence (local, on develop with #134 and #135) `test/health.test.ts`: - T1 (slow provider: booting then ok, and a probe starting the boot), T2, T3 - T4 on Node: a kept-alive probe connection gets `503 draining` during `close()` while `/slow` completes `200`. With `drainDelayMs: 0` the same test gets `ECONNRESET`, which is D14's rationale, checked by hand. - T6 (`maxConcurrentRequests: 1` saturated: probes `200`, others `503`), T7 (POST `405` with `Allow`; HEAD with no body) - T8 (`@Get('/__live__')` boot error, exact message), `/__ready__` HTTP and WS with health on and off, teapot route at `/__ready__` - T9 (10 probes with `logRequests`: no log line, `bus.emit` never called, a plugin step never runs, with a control request) - invalid values, and the budget check T5 runs in `test/deno/shutdown.test.ts` and `test/bun/shutdown.test.ts`. T10 runs in `test/edge/run.ts`: live 200, ready booting then ok with only probes reaching the isolate. ``` npm run lint 0 errors (52 warnings, unchanged: no-explicit-any) npm run format:check All matched files use Prettier code style! npm run typecheck clean npm test Test Files 94 passed (94) · Tests 946 passed (946) npm run build CJS/ESM/DTS build success npm run complexity:check every function <= 25 npm run test:deno 38 passed | 0 failed npm run test:bun 28 pass · 0 fail npm run test:edge ... + reserved path + health — PASS; router parity 1158 + 55 — PASS npm audit --audit-level=moderate found 0 vulnerabilities ``` ## Docs follow-up (CARD-12) - `reference/createapp.md`: the `health` option: shape, states and bodies, 405, `cache-control`, `drainDelayMs` and its budget rule, the probe-starts-boot behaviour. Add `/__live__` and `/__ready__` to the reserved-paths list started for CARD-21. - Deployment and shutdown guide: Kubernetes and load-balancer readiness with `drainDelayMs` versus a platform `preStop` sleep (PRD open question), why Node needs the window (idle keep-alive sockets close at once), and Deno/Bun `close()` behaviour. - Runtimes page: the edge never reports `draining`, and booting is per isolate.
Author
Owner

Owner decisions (2026-10-23): probe-triggered boot, 503 booting on failed boot, and the drain-vs-shutdown-timeout check are accepted. Not merging yet: implementation review first.

Owner decisions (2026-10-23): probe-triggered boot, `503 booting` on failed boot, and the drain-vs-shutdown-timeout check are accepted. Not merging yet: implementation review first.
chrnx force-pushed feature/fprd-001c-health from f7e26e4dbb to ba57568a84 2026-10-23 05:54:39 +00:00 Compare
Author
Owner

Implementation review done on 2026-10-23: a Codex review of all 12 sprint PRs, with each finding verified. The P1s in #142 and the P3 in #149 are fixed. The owner (Chrnx) authorized merging in order, each one rebased on develop with CI green. This supersedes the earlier "not merging yet" note.

Implementation review done on 2026-10-23: a Codex review of all 12 sprint PRs, with each finding verified. The P1s in #142 and the P3 in #149 are fixed. The owner (Chrnx) authorized merging in order, each one rebased on develop with CI green. This supersedes the earlier "not merging yet" note.
chrnx added 1 commit 2026-10-23 05:57:15 +00:00
feat(http): liveness and readiness probes with a drain window (CARD-17, #115)
CI / dco (pull_request) Successful in 4s
CI / runtimes (bun) (pull_request) Successful in 16s
CI / build (pull_request) Successful in 27s
CI / runtimes (deno) (pull_request) Successful in 21s
CI / runtimes (edge) (pull_request) Successful in 14s
f0530b15bb
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>
chrnx force-pushed feature/fprd-001c-health from ba57568a84 to f0530b15bb 2026-10-23 05:57:15 +00:00 Compare
chrnx merged commit 1da6bf2f99 into develop 2026-10-23 05:58:59 +00:00
chrnx deleted branch feature/fprd-001c-health 2026-10-23 05:59:00 +00:00
Sign in to join this conversation.