Lifecycle & health
Graceful shutdown drains in-flight requests and sends Connection: close. Health and readiness checks keep new traffic away from an instance that is starting or stopping.
Daloy ships the lifecycle & health slice of the secure-by-default initiative: connection-draining shutdown, crash-on-unhandled-rejection, and first-class healthcheck() / readinesscheck() primitives. All three default-on the safe behaviour and let you opt out per-feature or globally with app({ secureDefaults: false }).
1. Connection-draining shutdown
- 01signalapp.close(timeoutMs)drain flag flips synchronously
- 02new trafficNew requests rejected503 + retry-after: 5 + connection: close
- 03in-flightExisting responses finishgain connection: close header
- 04drainedonClose hooks runidle keep-alive sockets killed
app.shutdown(timeoutMs, reason?) (alias app.close()) flips a drain flag synchronously. Every subsequent request returns a structured 503 application/problem+json with retry-after: 5 and connection: close. Any in-flight response that finishes during the drain window also gains connection: close so HTTP/1.1 load balancers stop reusing the keep-alive socket.
On the Node adapter, serve(app) registers an idle-connection close hook that calls server.closeIdleConnections() the moment draining begins, keep-alive sockets without an in-flight request are killed immediately, without affecting sockets that are still serving a request. Custom adapters can register the same hook via app._registerIdleConnectionCloseHook(hook).
2. Crash on unhandled rejection in production
The new app({ crashOnUnhandledRejection }) option installs process-wide listeners for unhandledRejection and uncaughtException that log fatal through the pluggable logger and call process.exit(1). The framework deliberately avoids the "swallow and keep running" anti-pattern, a crashed process is easier to reason about than a zombie one. Defaults:
- Omitted: install in production (
env: "production"orNODE_ENV === "production"), skip elsewhere. true: install even in development (useful for staging / CI).false: never install, even in production.
A process-wide latch ensures the listeners are installed exactly once even when multiple App instances boot in the same process. No-op on runtimes without process.on (Cloudflare Workers, Vercel, Fastly Compute).
3. app.healthcheck() and app.readinesscheck()
Opt-in route registration with sensible defaults: paths /healthz and /readyz, per-IP fixed-window rate limit (60 requests / 60 s, in-memory), optional bearer-token auth compared via timingSafeEqual. Readiness returns 503 with retry-after: 5 while draining or while any plugin is still pending in register().
Pass rateLimit: false to disable the per-IP cap entirely (sidecar-only probes that arrive directly from the orchestrator). The client IP comes from resolveClientIp() under your app's behindProxy posture: the TCP peer when behindProxy is unset or "none", and a forwarded address only from a verified proxy peer (hops, cidrs, or "loopback"). Raw X-Real-IP, Fly-Client-IP and X-Forwarded-For headers are never read directly, so rotating them does not bypass the cap. IPv6 clients are grouped per /64, as in rateLimit(). Only when the runtime exposes no peer at all (some edge platforms) do probes share one "global" bucket. The CSP report receiver uses the same identity, and its onReport sink receives that resolved address (or null), never a header value.
Dependency checks (database, cache, queue)
By itself, /readyz only knows whether the app is draining and whether plugins finished booting. It does not know that your database is down. Since 1.13.0, pass named checks and the probe also answers 503 when one of them fails:
- Every check runs on every probe, concurrently, after the draining and plugin checks pass. A check fails when it returns
false, throws, rejects, or runs pastcheckTimeoutMs. - Each check gets an
AbortSignalthat aborts (with aTimeoutError) at the limit. Forward it to the driver so a hung connection is released. A check that ignores the signal and rejects later is still handled: it never becomes an unhandled rejection. - The
503body is always{ "status": "not-ready" }withretry-after: 5, whichever check failed. The check name and the error are logged server-side asreadiness.check_failed(withreason:timeout,returned_false, orthrew), so an unauthenticated probe never reveals which dependency is down or what its connection error says. - Keep checks cheap (
select 1,PING). The probe's rate limit and token still apply before any check runs. - Liveness (
/healthz) has no checks on purpose: a database outage should take the pod out of rotation, not restart it. - Check names must match
[A-Za-z0-9_.-]{1,64}. MalformedchecksorcheckTimeoutMsthrow at registration.
- checks
- Readonly<Record<string, ReadinessCheck>>
- Default:-
- Named dependency checks, run concurrently on every probe after the draining and plugin checks pass. Any failing check makes the probe answer
503. Names must match[A-Za-z0-9_.-]{1,64}. Which check failed, and why, is logged server-side (readiness.check_failed) and never included in the response. - checkTimeoutMs
- number
- Default:2000
- Per-check time limit in milliseconds, between 1 and 60000. A check still running at the limit is aborted and counts as failed.
src/app.ts.Opt-out
Disable the whole slice with new App({ secureDefaults: false }), or only the crash handlers with new App({ crashOnUnhandledRejection: false }). Health and readiness routes are opt-in, no auto-registration happens, the framework only flips behaviour when you call app.healthcheck() / app.readinesscheck() explicitly.