From 8675c1f7c3eedbb287c85de690341b7208ab5489 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ullrich=20Sch=C3=A4fer?= Date: Sun, 24 May 2026 12:37:09 +0200 Subject: [PATCH] fix(journal): reuse a dedicated pool for /api/health instead of per-call connect MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The previous handler opened a fresh postgres client (max: 1) on every call to /api/health and tore it down in the finally block. Under the prod monitoring cadence (probes every few seconds), that's a fresh TCP + TLS + auth handshake on every probe, plus connection-table churn on the Postgres side — fine for the trickle of curl-ish manual checks, slow-bleed under blackbox monitoring. Now we cache a module-level singleton postgres client dedicated to /api/health (max: 2, idle_timeout: 30) and reuse it across calls. Separate from the app's main DB pool (via @trails-cool/db's createDb) on purpose — so a starvation event on the main pool doesn't fail the liveness check and trigger a restart loop. Full repo: pnpm typecheck, pnpm lint, pnpm test all green. Co-Authored-By: Claude Opus 4.7 (1M context) --- apps/journal/server.ts | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/apps/journal/server.ts b/apps/journal/server.ts index 89dbf93..90c3c39 100644 --- a/apps/journal/server.ts +++ b/apps/journal/server.ts @@ -9,7 +9,7 @@ import { randomUUID } from "node:crypto"; import { httpRequestDuration, registry } from "./app/lib/metrics.server.ts"; import { createBoss, startWorker } from "@trails-cool/jobs"; import { getDatabaseUrl } from "@trails-cool/db"; -import postgres from "postgres"; +import postgres, { type Sql } from "postgres"; // Sentry DSN is read from env so self-hosted instances don't ship their // errors to the trails.cool flagship Sentry by default. The flagship @@ -76,17 +76,27 @@ async function handleMetrics(_req: IncomingMessage, res: ServerResponse): Promis const version = process.env.SENTRY_RELEASE ?? "dev"; +// Module-level singleton postgres client dedicated to /api/health. The +// previous handler opened a fresh client + connection on every call, +// which OOM'd the process under monitoring load (probes hit /api/health +// every few seconds). `max: 2` is plenty for liveness checks; the main +// app DB pool is separate (via @trails-cool/db's createDb). +let healthClient: Sql | null = null; +function getHealthClient(): Sql { + if (!healthClient) { + healthClient = postgres(getDatabaseUrl(), { max: 2, idle_timeout: 30 }); + } + return healthClient; +} + async function handleHealth(_req: IncomingMessage, res: ServerResponse): Promise { - const client = postgres(getDatabaseUrl(), { max: 1 }); try { - await client`SELECT 1`; + await getHealthClient()`SELECT 1`; res.writeHead(200, { "Content-Type": "application/json" }); res.end(JSON.stringify({ status: "ok", version, db: "connected" })); } catch { res.writeHead(503, { "Content-Type": "application/json" }); res.end(JSON.stringify({ status: "degraded", version, db: "unreachable" })); - } finally { - await client.end(); } }