@adaptic/backend-legacy 0.0.1042 → 0.0.1044
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/esm/auth/service-token.d.ts +8 -0
- package/esm/auth/service-token.d.ts.map +1 -1
- package/esm/auth/service-token.js.map +1 -1
- package/esm/auth/service-token.mjs +103 -40
- package/esm/health.d.ts +65 -2
- package/esm/health.d.ts.map +1 -1
- package/esm/health.js.map +1 -1
- package/esm/health.mjs +141 -4
- package/esm/prismaClient.d.ts +38 -0
- package/esm/prismaClient.d.ts.map +1 -1
- package/esm/prismaClient.js.map +1 -1
- package/esm/prismaClient.mjs +59 -6
- package/health.cjs +178 -7
- package/health.d.ts +65 -2
- package/package.json +1 -1
- package/prismaClient.cjs +67 -11
- package/prismaClient.d.ts +38 -0
|
@@ -67,6 +67,14 @@ export declare const MAX_SERVICE_TOKEN_LIFETIME_SEC = 3600;
|
|
|
67
67
|
* a minter and this host and nothing more.
|
|
68
68
|
*/
|
|
69
69
|
export declare const MAX_SERVICE_TOKEN_CLOCK_SKEW_SEC = 60;
|
|
70
|
+
/**
|
|
71
|
+
* Which configured secret proved a credential's signature.
|
|
72
|
+
*
|
|
73
|
+
* `primary` is `BACKEND_SERVICE_JWT_SECRET`; `previous` is the optional
|
|
74
|
+
* `BACKEND_SERVICE_JWT_SECRET_PREVIOUS` that exists only for the length of a
|
|
75
|
+
* rotation.
|
|
76
|
+
*/
|
|
77
|
+
export type ServiceKeySlot = 'primary' | 'previous';
|
|
70
78
|
/**
|
|
71
79
|
* Attempt to establish a service principal from a bearer token.
|
|
72
80
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"service-token.d.ts","sourceRoot":"","sources":["../../../src/auth/service-token.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AAOH,OAAO,EAAa,KAAK,gBAAgB,EAAE,MAAM,kBAAkB,CAAC;AAEpE;;;;;GAKG;AACH,eAAO,MAAM,oBAAoB,oBAAoB,CAAC;AAEtD;;;;;;GAMG;AACH,eAAO,MAAM,sBAAsB,oBAAoB,CAAC;AAExD,sEAAsE;AACtE,eAAO,MAAM,6BAA6B,KAAK,CAAC;AAEhD;;;;;;;;GAQG;AACH,eAAO,MAAM,8BAA8B,OAAO,CAAC;AAEnD;;;;;;;;GAQG;AACH,eAAO,MAAM,gCAAgC,KAAK,CAAC;
|
|
1
|
+
{"version":3,"file":"service-token.d.ts","sourceRoot":"","sources":["../../../src/auth/service-token.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AAOH,OAAO,EAAa,KAAK,gBAAgB,EAAE,MAAM,kBAAkB,CAAC;AAEpE;;;;;GAKG;AACH,eAAO,MAAM,oBAAoB,oBAAoB,CAAC;AAEtD;;;;;;GAMG;AACH,eAAO,MAAM,sBAAsB,oBAAoB,CAAC;AAExD,sEAAsE;AACtE,eAAO,MAAM,6BAA6B,KAAK,CAAC;AAEhD;;;;;;;;GAQG;AACH,eAAO,MAAM,8BAA8B,OAAO,CAAC;AAEnD;;;;;;;;GAQG;AACH,eAAO,MAAM,gCAAgC,KAAK,CAAC;AAEnD;;;;;;GAMG;AACH,MAAM,MAAM,cAAc,GAAG,SAAS,GAAG,UAAU,CAAC;AAmIpD;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,kBAAkB,CAChC,KAAK,EAAE,MAAM,EACb,KAAK,GAAE,MAAmB,GACzB,gBAAgB,GAAG,IAAI,CAiFzB"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"service-token.js","sourceRoot":"","sources":["../../../src/auth/service-token.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AAEH,OAAO,GAAG,EAAE,EAAE,iBAAiB,EAAE,iBAAiB,EAAE,MAAM,cAAc,CAAC;AAEzE,OAAO,EAAE,SAAS,EAAE,MAAM,qBA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MAAM,IAAI,SAAS,CAAC,eAAe,EAAE,WAAW,CAAC,CAAC;IACpD,CAAC;IACD,IAAI,GAAG,GAAG,GAAG,GAAG,8BAA8B,EAAE,CAAC;QAC/C,MAAM,CAAC,IAAI,CAAC,yDAAyD,EAAE;YACrE,GAAG;YACH,WAAW,EAAE,GAAG,GAAG,GAAG;YACtB,UAAU,EAAE,8BAA8B;SAC3C,CAAC,CAAC;QACH,MAAM,IAAI,SAAS,CAAC,eAAe,EAAE,cAAc,CAAC,CAAC;IACvD,CAAC;IAED,wEAAwE;IACxE,uEAAuE;IACvE,2EAA2E;IAC3E,6DAA6D;IAC7D,MAAM,MAAM,GAAG,OAAO,KAAK,UAAU,CAAC,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC;IACnE,MAAM,CAAC,+BAA+B,EAAE,EAAE,GAAG,EAAE,OAAO,EAAE,CAAC,CAAC;IAE1D,OAAO,EAAE,IAAI,EAAE,QAAQ,EAAE,GAAG,EAAE,CAAC;AACjC,CAAC"}
|
|
@@ -71,61 +71,74 @@ export const MAX_SERVICE_TOKEN_LIFETIME_SEC = 3600;
|
|
|
71
71
|
*/
|
|
72
72
|
export const MAX_SERVICE_TOKEN_CLOCK_SKEW_SEC = 60;
|
|
73
73
|
/**
|
|
74
|
-
*
|
|
75
|
-
* provisioned.
|
|
76
|
-
*
|
|
77
|
-
* Read per call rather than cached at import so an operator rotating the
|
|
78
|
-
* Railway variable takes effect on the next request rather than the next
|
|
79
|
-
* restart — the same discipline `SERVER_AUTH_TOKEN` already follows.
|
|
74
|
+
* Validate one raw secret value, or `null` when it cannot serve as one.
|
|
80
75
|
*
|
|
81
|
-
* @
|
|
76
|
+
* @param name - Env var name, for the operator-facing log line.
|
|
77
|
+
* @param raw - The raw env value.
|
|
78
|
+
* @returns The trimmed secret, or `null` when unset, blank, or too short.
|
|
82
79
|
*/
|
|
83
|
-
function
|
|
84
|
-
const raw = process.env.BACKEND_SERVICE_JWT_SECRET;
|
|
80
|
+
function readSecret(name, raw) {
|
|
85
81
|
if (typeof raw !== 'string')
|
|
86
82
|
return null;
|
|
87
83
|
const secret = raw.trim();
|
|
88
84
|
if (secret.length === 0)
|
|
89
85
|
return null;
|
|
90
86
|
if (secret.length < MINIMUM_SERVICE_SECRET_LENGTH) {
|
|
91
|
-
logger.error(
|
|
92
|
-
`${MINIMUM_SERVICE_SECRET_LENGTH} characters;
|
|
93
|
-
'Service callers will be rejected until a long enough secret is provisioned.', { secretLength: secret.length });
|
|
87
|
+
logger.error(`[auth] ${name} is set but shorter than the minimum ` +
|
|
88
|
+
`${MINIMUM_SERVICE_SECRET_LENGTH} characters; it will not be used to verify service tokens.`, { secretLength: secret.length });
|
|
94
89
|
return null;
|
|
95
90
|
}
|
|
96
91
|
return secret;
|
|
97
92
|
}
|
|
98
93
|
/**
|
|
99
|
-
*
|
|
94
|
+
* Resolve the service secrets to try, in order, or `null` when the path is not
|
|
95
|
+
* provisioned.
|
|
100
96
|
*
|
|
101
|
-
*
|
|
102
|
-
*
|
|
103
|
-
*
|
|
104
|
-
* subsequent problem with it (expiry, wrong audience, missing subject,
|
|
105
|
-
* over-long lifetime) throws, because at that point the caller's intent is
|
|
106
|
-
* unambiguous and falling through would report a misleading `bad_signature`
|
|
107
|
-
* for what is actually an expired or mis-scoped service token.
|
|
97
|
+
* Read per call rather than cached at import so an operator rotating the
|
|
98
|
+
* Railway variable takes effect on the next request rather than the next
|
|
99
|
+
* restart — the same discipline `SERVER_AUTH_TOKEN` already follows.
|
|
108
100
|
*
|
|
109
|
-
*
|
|
110
|
-
*
|
|
111
|
-
*
|
|
101
|
+
* WHY A SECOND SLOT. With one secret, rotation means every minter and this
|
|
102
|
+
* verifier must change at the same instant or service calls fail in between.
|
|
103
|
+
* `BACKEND_SERVICE_JWT_SECRET_PREVIOUS` lets the verifier accept the outgoing
|
|
104
|
+
* key while minters move to the new one, so the swap is: set PREVIOUS to the
|
|
105
|
+
* current value and the primary to the new one, move the minters, watch the
|
|
106
|
+
* `previous` log line drain to zero, then unset PREVIOUS.
|
|
107
|
+
*
|
|
108
|
+
* The previous slot never stands alone: with no valid primary the whole path
|
|
109
|
+
* stays disabled, so an operator cannot half-finish a rotation into a state
|
|
110
|
+
* where only the retiring key is honoured.
|
|
111
|
+
*
|
|
112
|
+
* @returns The slots to try, primary first, or `null` when unprovisioned.
|
|
112
113
|
*/
|
|
113
|
-
|
|
114
|
-
const
|
|
115
|
-
if (
|
|
114
|
+
function resolveServiceSecrets() {
|
|
115
|
+
const primary = readSecret('BACKEND_SERVICE_JWT_SECRET', process.env.BACKEND_SERVICE_JWT_SECRET);
|
|
116
|
+
if (primary === null) {
|
|
117
|
+
if (process.env.BACKEND_SERVICE_JWT_SECRET_PREVIOUS?.trim()) {
|
|
118
|
+
logger.error('[auth] BACKEND_SERVICE_JWT_SECRET_PREVIOUS is set without a valid ' +
|
|
119
|
+
'BACKEND_SERVICE_JWT_SECRET; the service-principal path is DISABLED. ' +
|
|
120
|
+
'The previous slot only extends a provisioned primary, it never replaces one.');
|
|
121
|
+
}
|
|
116
122
|
return null;
|
|
117
|
-
// A service secret set equal to the user-session secret would let any
|
|
118
|
-
// end-user JWT that carries our issuer/audience become a `server` principal,
|
|
119
|
-
// which bypasses every tenancy scope and role gate. Refuse rather than
|
|
120
|
-
// silently operate with the separation collapsed.
|
|
121
|
-
if (secret === jwtSecret) {
|
|
122
|
-
logger.error('[auth] BACKEND_SERVICE_JWT_SECRET is identical to JWT_SECRET. ' +
|
|
123
|
-
'These MUST be distinct secrets — sharing them collapses the ' +
|
|
124
|
-
'separation between end-user and service identity. Refusing to ' +
|
|
125
|
-
'verify service tokens until they differ.');
|
|
126
|
-
throw new AuthError('invalid_token', 'misconfigured');
|
|
127
123
|
}
|
|
128
|
-
|
|
124
|
+
const slots = [
|
|
125
|
+
{ slot: 'primary', secret: primary },
|
|
126
|
+
];
|
|
127
|
+
const previous = readSecret('BACKEND_SERVICE_JWT_SECRET_PREVIOUS', process.env.BACKEND_SERVICE_JWT_SECRET_PREVIOUS);
|
|
128
|
+
// Equal to the primary adds nothing; skip it so the log never claims a
|
|
129
|
+
// `previous` verification that was really the primary.
|
|
130
|
+
if (previous !== null && previous !== primary) {
|
|
131
|
+
slots.push({ slot: 'previous', secret: previous });
|
|
132
|
+
}
|
|
133
|
+
return slots;
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* Verify a token's signature and bound claims against one secret.
|
|
137
|
+
*
|
|
138
|
+
* @returns The payload, or `null` when the signature is not this secret's.
|
|
139
|
+
* @throws {AuthError} When the signature matched but the token is unacceptable.
|
|
140
|
+
*/
|
|
141
|
+
function verifyAgainst(token, secret) {
|
|
129
142
|
try {
|
|
130
143
|
// Algorithm pinned to HS256 for the same reason path 2 pins it: an
|
|
131
144
|
// unpinned verify accepts `alg: "none"` on some versions, which turns a
|
|
@@ -138,7 +151,7 @@ export function verifyServiceToken(token, nowMs = Date.now()) {
|
|
|
138
151
|
if (typeof verified === 'string') {
|
|
139
152
|
throw new AuthError('invalid_token', 'malformed');
|
|
140
153
|
}
|
|
141
|
-
|
|
154
|
+
return verified;
|
|
142
155
|
}
|
|
143
156
|
catch (error) {
|
|
144
157
|
if (error instanceof AuthError)
|
|
@@ -151,8 +164,9 @@ export function verifyServiceToken(token, nowMs = Date.now()) {
|
|
|
151
164
|
}
|
|
152
165
|
if (error instanceof JsonWebTokenError) {
|
|
153
166
|
const message = (error.message || '').toLowerCase();
|
|
154
|
-
// Signature mismatch means this is simply not
|
|
155
|
-
//
|
|
167
|
+
// Signature mismatch means this is simply not this secret's credential —
|
|
168
|
+
// the caller tries the next slot, then falls through to the app-JWT and
|
|
169
|
+
// Google paths.
|
|
156
170
|
if (message.includes('invalid signature') ||
|
|
157
171
|
message.includes('invalid algorithm')) {
|
|
158
172
|
return null;
|
|
@@ -169,6 +183,49 @@ export function verifyServiceToken(token, nowMs = Date.now()) {
|
|
|
169
183
|
}
|
|
170
184
|
return null;
|
|
171
185
|
}
|
|
186
|
+
}
|
|
187
|
+
/**
|
|
188
|
+
* Attempt to establish a service principal from a bearer token.
|
|
189
|
+
*
|
|
190
|
+
* Returns `null` — meaning "not a service credential, keep looking" — only
|
|
191
|
+
* when the path is unprovisioned or the signature does not belong to the
|
|
192
|
+
* service secret. Once a token IS proven to carry that signature, every
|
|
193
|
+
* subsequent problem with it (expiry, wrong audience, missing subject,
|
|
194
|
+
* over-long lifetime) throws, because at that point the caller's intent is
|
|
195
|
+
* unambiguous and falling through would report a misleading `bad_signature`
|
|
196
|
+
* for what is actually an expired or mis-scoped service token.
|
|
197
|
+
*
|
|
198
|
+
* @param token - Raw bearer token; assumed to be three dot-separated segments.
|
|
199
|
+
* @returns The verified service principal, or `null` to fall through.
|
|
200
|
+
* @throws {AuthError} When the token is ours but unacceptable.
|
|
201
|
+
*/
|
|
202
|
+
export function verifyServiceToken(token, nowMs = Date.now()) {
|
|
203
|
+
const slots = resolveServiceSecrets();
|
|
204
|
+
if (slots === null)
|
|
205
|
+
return null;
|
|
206
|
+
// A service secret set equal to the user-session secret would let any
|
|
207
|
+
// end-user JWT that carries our issuer/audience become a `server` principal,
|
|
208
|
+
// which bypasses every tenancy scope and role gate. Refuse rather than
|
|
209
|
+
// silently operate with the separation collapsed. This holds for the
|
|
210
|
+
// previous slot too: a rotation must not reopen the hole.
|
|
211
|
+
if (slots.some(({ secret }) => secret === jwtSecret)) {
|
|
212
|
+
logger.error('[auth] BACKEND_SERVICE_JWT_SECRET (or its _PREVIOUS slot) is identical to JWT_SECRET. ' +
|
|
213
|
+
'These MUST be distinct secrets — sharing them collapses the ' +
|
|
214
|
+
'separation between end-user and service identity. Refusing to ' +
|
|
215
|
+
'verify service tokens until they differ.');
|
|
216
|
+
throw new AuthError('invalid_token', 'misconfigured');
|
|
217
|
+
}
|
|
218
|
+
let payload = null;
|
|
219
|
+
let keySlot = 'primary';
|
|
220
|
+
for (const { slot, secret } of slots) {
|
|
221
|
+
payload = verifyAgainst(token, secret);
|
|
222
|
+
if (payload !== null) {
|
|
223
|
+
keySlot = slot;
|
|
224
|
+
break;
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
if (payload === null)
|
|
228
|
+
return null;
|
|
172
229
|
const sub = typeof payload.sub === 'string' ? payload.sub.trim() : '';
|
|
173
230
|
if (sub.length === 0) {
|
|
174
231
|
// An unattributable service credential defeats the audit trail this path
|
|
@@ -210,6 +267,12 @@ export function verifyServiceToken(token, nowMs = Date.now()) {
|
|
|
210
267
|
});
|
|
211
268
|
throw new AuthError('invalid_token', 'bad_audience');
|
|
212
269
|
}
|
|
270
|
+
// Which key verified is the rotation's progress signal: once `previous`
|
|
271
|
+
// stops appearing, every minter is on the new key and the slot can go.
|
|
272
|
+
// `previous` logs at info so it shows under any production LOG_LEVEL short
|
|
273
|
+
// of warn; `primary` is the steady state and stays at debug.
|
|
274
|
+
const keyLog = keySlot === 'previous' ? logger.info : logger.debug;
|
|
275
|
+
keyLog('[auth] service token verified', { sub, keySlot });
|
|
213
276
|
return { kind: 'server', sub };
|
|
214
277
|
}
|
|
215
278
|
//# sourceMappingURL=service-token.js.map
|
package/esm/health.d.ts
CHANGED
|
@@ -1,4 +1,62 @@
|
|
|
1
1
|
import { Router } from 'express';
|
|
2
|
+
type DatabaseCheck = 'recent-query' | 'probe';
|
|
3
|
+
/**
|
|
4
|
+
* How recently a query must have completed for `/health` to accept it as
|
|
5
|
+
* proof the database is answering, without issuing a query of its own.
|
|
6
|
+
*
|
|
7
|
+
* Derived from the heartbeat rather than set by hand: it tolerates every
|
|
8
|
+
* heartbeat but the last before reconnection being missed, plus half an
|
|
9
|
+
* interval of slack — `(MAX_HEARTBEAT_FAILURES - 0.5) * HEARTBEAT_INTERVAL_MS`,
|
|
10
|
+
* 75s at the current 3 x 30s. That keeps it longer than one heartbeat interval,
|
|
11
|
+
* so an idle process that the heartbeat keeps refreshing never goes stale, and
|
|
12
|
+
* shorter than the reconnect point, so a database that has stopped answering
|
|
13
|
+
* reads as disconnected before the heartbeat gives up on the client.
|
|
14
|
+
*
|
|
15
|
+
* A saturated pool still completes queries, each bounded by the statement
|
|
16
|
+
* timeout, so saturation alone does not let it go stale — provided that
|
|
17
|
+
* timeout is shorter than this window (see
|
|
18
|
+
* {@link warnIfStatementTimeoutDefeatsFreshness}).
|
|
19
|
+
*/
|
|
20
|
+
export declare const DB_SUCCESS_FRESHNESS_MS: number;
|
|
21
|
+
/**
|
|
22
|
+
* Warn when the configured statement timeout is not shorter than the
|
|
23
|
+
* freshness window.
|
|
24
|
+
*
|
|
25
|
+
* The case for trusting a recent success under saturation is that every query
|
|
26
|
+
* on a busy pool finishes or fails within the statement timeout, so completed
|
|
27
|
+
* queries keep arriving inside {@link DB_SUCCESS_FRESHNESS_MS}. A timeout at or
|
|
28
|
+
* above the window breaks that: a pool full of long-running statements can go
|
|
29
|
+
* a whole window without a completion and `/health` would fall back to a probe
|
|
30
|
+
* that cannot get a connection. A value that is not a positive integer (unset
|
|
31
|
+
* timeouts parse from the default; `0` disables the timeout in Postgres) is
|
|
32
|
+
* treated the same way, since it bounds nothing.
|
|
33
|
+
*
|
|
34
|
+
* @param statementTimeoutMs - The resolved statement timeout (injectable for tests).
|
|
35
|
+
* @returns True if a warning was logged.
|
|
36
|
+
*/
|
|
37
|
+
export declare function warnIfStatementTimeoutDefeatsFreshness(statementTimeoutMs?: number): boolean;
|
|
38
|
+
/**
|
|
39
|
+
* Budget for the fallback probe when no recent success exists. Well under the
|
|
40
|
+
* 10s pool wait so a saturated pool yields an answer inside the platform's
|
|
41
|
+
* probe timeout rather than hanging the request.
|
|
42
|
+
*/
|
|
43
|
+
export declare const HEALTH_PROBE_TIMEOUT_MS = 2000;
|
|
44
|
+
/**
|
|
45
|
+
* Decide whether the database is answering this process, without depending
|
|
46
|
+
* on a free pool connection when there is already evidence that it is.
|
|
47
|
+
*
|
|
48
|
+
* A query that completed within {@link DB_SUCCESS_FRESHNESS_MS} is taken as
|
|
49
|
+
* that evidence and no query is issued. Only when there is none (startup, or a
|
|
50
|
+
* database that has stopped answering) does it fall back to a bounded probe.
|
|
51
|
+
*
|
|
52
|
+
* @param now - Current epoch ms (injectable for tests).
|
|
53
|
+
* @returns The database status, how it was decided, and the success age.
|
|
54
|
+
*/
|
|
55
|
+
export declare function checkDatabaseReadiness(now?: number): Promise<{
|
|
56
|
+
database: 'connected' | 'disconnected';
|
|
57
|
+
databaseCheck: DatabaseCheck;
|
|
58
|
+
lastQuerySucceededSecondsAgo: number | null;
|
|
59
|
+
}>;
|
|
2
60
|
/**
|
|
3
61
|
* Creates an Express router with health-probe endpoints.
|
|
4
62
|
*
|
|
@@ -9,11 +67,16 @@ import { Router } from 'express';
|
|
|
9
67
|
* is reachable (`SELECT 1`), HTTP 503 otherwise. Suitable for Cloud Run
|
|
10
68
|
* startup probes and load-balancer readiness.
|
|
11
69
|
* - `GET /health` — Full health snapshot including memory usage and database
|
|
12
|
-
* status. Returns 503 when the database
|
|
13
|
-
*
|
|
70
|
+
* status. Returns 503 only when the database has stopped answering this
|
|
71
|
+
* process: a query that completed within the freshness window counts as
|
|
72
|
+
* proof without taking a pool connection, so a saturated-but-working pool
|
|
73
|
+
* reads as healthy. With no recent success it falls back to a bounded
|
|
74
|
+
* probe (see {@link checkDatabaseReadiness}). Kept for backward
|
|
75
|
+
* compatibility with existing Railway/uptime checks.
|
|
14
76
|
*
|
|
15
77
|
* All endpoints should be mounted before auth middleware so they remain
|
|
16
78
|
* accessible without authentication.
|
|
17
79
|
*/
|
|
18
80
|
export declare function createHealthRouter(): Router;
|
|
81
|
+
export {};
|
|
19
82
|
//# sourceMappingURL=health.d.ts.map
|
package/esm/health.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"health.d.ts","sourceRoot":"","sources":["../../src/health.ts"],"names":[],"mappings":"AAAA,OAAO,EAAqB,MAAM,EAAE,MAAM,SAAS,CAAC;
|
|
1
|
+
{"version":3,"file":"health.d.ts","sourceRoot":"","sources":["../../src/health.ts"],"names":[],"mappings":"AAAA,OAAO,EAAqB,MAAM,EAAE,MAAM,SAAS,CAAC;AAgDpD,KAAK,aAAa,GAAG,cAAc,GAAG,OAAO,CAAC;AAE9C;;;;;;;;;;;;;;;;GAgBG;AACH,eAAO,MAAM,uBAAuB,QACoB,CAAC;AAEzD;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,sCAAsC,CACpD,kBAAkB,GAAE,MAAoC,GACvD,OAAO,CAgBT;AAED;;;;GAIG;AACH,eAAO,MAAM,uBAAuB,OAAQ,CAAC;AAqD7C;;;;;;;;;;GAUG;AACH,wBAAsB,sBAAsB,CAC1C,GAAG,GAAE,MAAmB,GACvB,OAAO,CAAC;IACT,QAAQ,EAAE,WAAW,GAAG,cAAc,CAAC;IACvC,aAAa,EAAE,aAAa,CAAC;IAC7B,4BAA4B,EAAE,MAAM,GAAG,IAAI,CAAC;CAC7C,CAAC,CAuBD;AAkBD;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAgB,kBAAkB,IAAI,MAAM,CAuD3C"}
|
package/esm/health.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"health.js","sourceRoot":"","sources":["../../src/health.ts"],"names":[],"mappings":"AAAA,OAAO,EAAqB,MAAM,EAAE,MAAM,SAAS,CAAC;AACpD,OAAO,MAAM,MAAM,gBAAgB,CAAC;
|
|
1
|
+
{"version":3,"file":"health.js","sourceRoot":"","sources":["../../src/health.ts"],"names":[],"mappings":"AAAA,OAAO,EAAqB,MAAM,EAAE,MAAM,SAAS,CAAC;AACpD,OAAO,MAAM,EAAE,EACb,uBAAuB,EACvB,qBAAqB,EACrB,sBAAsB,EACtB,yBAAyB,GAC1B,MAAM,gBAAgB,CAAC;AACxB,OAAO,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAC;AAExC,MAAM,YAAY,GAAG,gBAAgB,CAAC;AAEtC;;;GAGG;AACH,SAAS,iBAAiB;IACxB,IAAI,CAAC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AAE,YAAY;YACrB,OAAO,EAAE,eAAe;YACxB,SAAS,EAAE,IAAI,IAAI,EAAE,CAAC,WAAW,EAAE;YACnC,MAAM,EAAE,IAAI,CAAC,KAAK,CAAC,CAAC,IAAI,CAAC,GAAG,EAAE,GAAG,SAAS,CAAC,GAAG,IAAI,CAAC;YACnD,MAAM,EAAE;gBACN,GAAG,EAAE,IAAI,CAAC,KAAK,CAAC,WAAW,CAAC,GAAG,GAAG,IAAI,GAAG,IAAI,CAAC;gBAC9C,QAAQ,EAAE,IAAI,CAAC,KAAK,CAAC,WAAW,CAAC,QAAQ,GAAG,IAAI,GAAG,IAAI,CAAC;gBACxD,SAAS,EAAE,IAAI,CAAC,KAAK,CAAC,WAAW,CAAC,SAAS,GAAG,IAAI,GAAG,IAAI,CAAC;aAC3D;YACD,QAAQ,EAAE,cAAc;YACxB,aAAa,EAAE,SAAS,CAAC,aAAa;YACtC,4BAA4B,EAAE,SAAS,CAAC,4BAA4B;SACrE,CAAC;QAEF,GAAG,CAAC,MAAM,CAAC,UAAU,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IACpC,CAAC,CAAC,CAAC;IAEH,OAAO,MAAM,CAAC;AAChB,CAAC"}
|
package/esm/health.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Router } from 'express';
|
|
2
|
-
import prisma from './prismaClient.mjs';
|
|
2
|
+
import prisma, { getLastQuerySucceededAt, HEARTBEAT_INTERVAL_MS, MAX_HEARTBEAT_FAILURES, resolveStatementTimeoutMs, } from './prismaClient.mjs';
|
|
3
3
|
import { logger } from './utils/logger.mjs';
|
|
4
4
|
const SERVICE_NAME = 'backend-legacy';
|
|
5
5
|
/**
|
|
@@ -18,6 +18,135 @@ function getPackageVersion() {
|
|
|
18
18
|
const PACKAGE_VERSION = getPackageVersion();
|
|
19
19
|
/** Tracks the process start time for uptime calculation */
|
|
20
20
|
const startedAt = Date.now();
|
|
21
|
+
/**
|
|
22
|
+
* How recently a query must have completed for `/health` to accept it as
|
|
23
|
+
* proof the database is answering, without issuing a query of its own.
|
|
24
|
+
*
|
|
25
|
+
* Derived from the heartbeat rather than set by hand: it tolerates every
|
|
26
|
+
* heartbeat but the last before reconnection being missed, plus half an
|
|
27
|
+
* interval of slack — `(MAX_HEARTBEAT_FAILURES - 0.5) * HEARTBEAT_INTERVAL_MS`,
|
|
28
|
+
* 75s at the current 3 x 30s. That keeps it longer than one heartbeat interval,
|
|
29
|
+
* so an idle process that the heartbeat keeps refreshing never goes stale, and
|
|
30
|
+
* shorter than the reconnect point, so a database that has stopped answering
|
|
31
|
+
* reads as disconnected before the heartbeat gives up on the client.
|
|
32
|
+
*
|
|
33
|
+
* A saturated pool still completes queries, each bounded by the statement
|
|
34
|
+
* timeout, so saturation alone does not let it go stale — provided that
|
|
35
|
+
* timeout is shorter than this window (see
|
|
36
|
+
* {@link warnIfStatementTimeoutDefeatsFreshness}).
|
|
37
|
+
*/
|
|
38
|
+
export const DB_SUCCESS_FRESHNESS_MS = (MAX_HEARTBEAT_FAILURES - 0.5) * HEARTBEAT_INTERVAL_MS;
|
|
39
|
+
/**
|
|
40
|
+
* Warn when the configured statement timeout is not shorter than the
|
|
41
|
+
* freshness window.
|
|
42
|
+
*
|
|
43
|
+
* The case for trusting a recent success under saturation is that every query
|
|
44
|
+
* on a busy pool finishes or fails within the statement timeout, so completed
|
|
45
|
+
* queries keep arriving inside {@link DB_SUCCESS_FRESHNESS_MS}. A timeout at or
|
|
46
|
+
* above the window breaks that: a pool full of long-running statements can go
|
|
47
|
+
* a whole window without a completion and `/health` would fall back to a probe
|
|
48
|
+
* that cannot get a connection. A value that is not a positive integer (unset
|
|
49
|
+
* timeouts parse from the default; `0` disables the timeout in Postgres) is
|
|
50
|
+
* treated the same way, since it bounds nothing.
|
|
51
|
+
*
|
|
52
|
+
* @param statementTimeoutMs - The resolved statement timeout (injectable for tests).
|
|
53
|
+
* @returns True if a warning was logged.
|
|
54
|
+
*/
|
|
55
|
+
export function warnIfStatementTimeoutDefeatsFreshness(statementTimeoutMs = resolveStatementTimeoutMs()) {
|
|
56
|
+
if (Number.isFinite(statementTimeoutMs) &&
|
|
57
|
+
statementTimeoutMs > 0 &&
|
|
58
|
+
statementTimeoutMs < DB_SUCCESS_FRESHNESS_MS) {
|
|
59
|
+
return false;
|
|
60
|
+
}
|
|
61
|
+
logger.warn('DATABASE_STATEMENT_TIMEOUT_MS is not shorter than the /health freshness window — a saturated pool can read as disconnected', {
|
|
62
|
+
statementTimeoutMs,
|
|
63
|
+
freshnessWindowMs: DB_SUCCESS_FRESHNESS_MS,
|
|
64
|
+
});
|
|
65
|
+
return true;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Budget for the fallback probe when no recent success exists. Well under the
|
|
69
|
+
* 10s pool wait so a saturated pool yields an answer inside the platform's
|
|
70
|
+
* probe timeout rather than hanging the request.
|
|
71
|
+
*/
|
|
72
|
+
export const HEALTH_PROBE_TIMEOUT_MS = 2_000;
|
|
73
|
+
/** The one probe query this module may have outstanding on the pool. */
|
|
74
|
+
let outstandingProbe;
|
|
75
|
+
/**
|
|
76
|
+
* Probe the live client with `SELECT 1`, giving up after `timeoutMs`.
|
|
77
|
+
*
|
|
78
|
+
* Reads `global.prisma` rather than the module-level import, because the
|
|
79
|
+
* heartbeat's reconnect path replaces `global.prisma` and the import stays
|
|
80
|
+
* pinned to the original (by then disconnected) client.
|
|
81
|
+
*
|
|
82
|
+
* At most one probe query is ever outstanding: a caller that arrives while a
|
|
83
|
+
* previous probe is still queued on the pool waits on that same query instead
|
|
84
|
+
* of adding another waiter, so frequent health checks cannot themselves deepen
|
|
85
|
+
* a saturated pool's queue.
|
|
86
|
+
*
|
|
87
|
+
* @param timeoutMs - How long to wait for the probe before calling it failed.
|
|
88
|
+
* @returns True if the probe completed within the budget.
|
|
89
|
+
*/
|
|
90
|
+
async function probeLiveClient(timeoutMs) {
|
|
91
|
+
if (!outstandingProbe) {
|
|
92
|
+
const client = global.prisma ?? prisma;
|
|
93
|
+
outstandingProbe = Promise.resolve(client.$queryRaw `SELECT 1`).finally(() => {
|
|
94
|
+
outstandingProbe = undefined;
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
const probe = outstandingProbe;
|
|
98
|
+
let timer;
|
|
99
|
+
const timedOut = new Promise((resolve) => {
|
|
100
|
+
timer = setTimeout(() => resolve(false), timeoutMs);
|
|
101
|
+
});
|
|
102
|
+
try {
|
|
103
|
+
return await Promise.race([
|
|
104
|
+
probe.then(() => true, (dbError) => {
|
|
105
|
+
logger.warn('Health check: database probe failed', {
|
|
106
|
+
error: dbError instanceof Error ? dbError.message : String(dbError),
|
|
107
|
+
});
|
|
108
|
+
return false;
|
|
109
|
+
}),
|
|
110
|
+
timedOut,
|
|
111
|
+
]);
|
|
112
|
+
}
|
|
113
|
+
finally {
|
|
114
|
+
clearTimeout(timer);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Decide whether the database is answering this process, without depending
|
|
119
|
+
* on a free pool connection when there is already evidence that it is.
|
|
120
|
+
*
|
|
121
|
+
* A query that completed within {@link DB_SUCCESS_FRESHNESS_MS} is taken as
|
|
122
|
+
* that evidence and no query is issued. Only when there is none (startup, or a
|
|
123
|
+
* database that has stopped answering) does it fall back to a bounded probe.
|
|
124
|
+
*
|
|
125
|
+
* @param now - Current epoch ms (injectable for tests).
|
|
126
|
+
* @returns The database status, how it was decided, and the success age.
|
|
127
|
+
*/
|
|
128
|
+
export async function checkDatabaseReadiness(now = Date.now()) {
|
|
129
|
+
const lastSuccess = getLastQuerySucceededAt();
|
|
130
|
+
const ageMs = lastSuccess === undefined ? undefined : now - lastSuccess;
|
|
131
|
+
if (ageMs !== undefined && ageMs <= DB_SUCCESS_FRESHNESS_MS) {
|
|
132
|
+
return {
|
|
133
|
+
database: 'connected',
|
|
134
|
+
databaseCheck: 'recent-query',
|
|
135
|
+
lastQuerySucceededSecondsAgo: Math.max(0, Math.floor(ageMs / 1000)),
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
const answered = await probeLiveClient(HEALTH_PROBE_TIMEOUT_MS);
|
|
139
|
+
// A successful probe goes through the same client extension and refreshes
|
|
140
|
+
// the stamp; report the age as it stands after the probe.
|
|
141
|
+
const after = getLastQuerySucceededAt();
|
|
142
|
+
return {
|
|
143
|
+
database: answered ? 'connected' : 'disconnected',
|
|
144
|
+
databaseCheck: 'probe',
|
|
145
|
+
lastQuerySucceededSecondsAgo: after === undefined
|
|
146
|
+
? null
|
|
147
|
+
: Math.max(0, Math.floor((Date.now() - after) / 1000)),
|
|
148
|
+
};
|
|
149
|
+
}
|
|
21
150
|
/**
|
|
22
151
|
* Checks database connectivity by issuing a lightweight query.
|
|
23
152
|
* Returns 'connected' or 'disconnected'. Never throws.
|
|
@@ -44,13 +173,18 @@ async function checkDatabase() {
|
|
|
44
173
|
* is reachable (`SELECT 1`), HTTP 503 otherwise. Suitable for Cloud Run
|
|
45
174
|
* startup probes and load-balancer readiness.
|
|
46
175
|
* - `GET /health` — Full health snapshot including memory usage and database
|
|
47
|
-
* status. Returns 503 when the database
|
|
48
|
-
*
|
|
176
|
+
* status. Returns 503 only when the database has stopped answering this
|
|
177
|
+
* process: a query that completed within the freshness window counts as
|
|
178
|
+
* proof without taking a pool connection, so a saturated-but-working pool
|
|
179
|
+
* reads as healthy. With no recent success it falls back to a bounded
|
|
180
|
+
* probe (see {@link checkDatabaseReadiness}). Kept for backward
|
|
181
|
+
* compatibility with existing Railway/uptime checks.
|
|
49
182
|
*
|
|
50
183
|
* All endpoints should be mounted before auth middleware so they remain
|
|
51
184
|
* accessible without authentication.
|
|
52
185
|
*/
|
|
53
186
|
export function createHealthRouter() {
|
|
187
|
+
warnIfStatementTimeoutDefeatsFreshness();
|
|
54
188
|
const router = Router();
|
|
55
189
|
router.get('/livez', (_req, res) => {
|
|
56
190
|
res.status(200).json({
|
|
@@ -73,7 +207,8 @@ export function createHealthRouter() {
|
|
|
73
207
|
});
|
|
74
208
|
});
|
|
75
209
|
router.get('/health', async (_req, res) => {
|
|
76
|
-
const
|
|
210
|
+
const readiness = await checkDatabaseReadiness();
|
|
211
|
+
const databaseStatus = readiness.database;
|
|
77
212
|
const isHealthy = databaseStatus === 'connected';
|
|
78
213
|
const statusCode = isHealthy ? 200 : 503;
|
|
79
214
|
const memoryUsage = process.memoryUsage();
|
|
@@ -89,6 +224,8 @@ export function createHealthRouter() {
|
|
|
89
224
|
heapTotal: Math.round(memoryUsage.heapTotal / 1024 / 1024),
|
|
90
225
|
},
|
|
91
226
|
database: databaseStatus,
|
|
227
|
+
databaseCheck: readiness.databaseCheck,
|
|
228
|
+
lastQuerySucceededSecondsAgo: readiness.lastQuerySucceededSecondsAgo,
|
|
92
229
|
};
|
|
93
230
|
res.status(statusCode).json(body);
|
|
94
231
|
});
|
package/esm/prismaClient.d.ts
CHANGED
|
@@ -29,6 +29,28 @@ export declare const DEFAULT_FIND_MANY_TAKE = 50000;
|
|
|
29
29
|
* @returns The row ceiling to apply to an unqualified `findMany`.
|
|
30
30
|
*/
|
|
31
31
|
export declare function resolveFindManyTake(): number;
|
|
32
|
+
/**
|
|
33
|
+
* When a query on this process's Prisma client last completed successfully.
|
|
34
|
+
*
|
|
35
|
+
* This is the evidence `/health` uses for "the database is answering this
|
|
36
|
+
* process". It is recorded as a side effect of ordinary traffic and of the
|
|
37
|
+
* heartbeat, so reading it never needs a pool connection — which is the point:
|
|
38
|
+
* a pool that is saturated but still completing work keeps this fresh, and a
|
|
39
|
+
* database that has stopped answering lets it go stale.
|
|
40
|
+
*
|
|
41
|
+
* @returns Epoch milliseconds of the last success, or undefined if none yet.
|
|
42
|
+
*/
|
|
43
|
+
export declare function getLastQuerySucceededAt(): number | undefined;
|
|
44
|
+
/**
|
|
45
|
+
* Run a query and stamp the success time if it resolves.
|
|
46
|
+
*
|
|
47
|
+
* A rejected query leaves the stamp alone; any completed round trip, including
|
|
48
|
+
* one issued by a caller that has since given up waiting on it, refreshes it.
|
|
49
|
+
*
|
|
50
|
+
* @param run - Executes the underlying operation.
|
|
51
|
+
* @returns Whatever the operation returned.
|
|
52
|
+
*/
|
|
53
|
+
export declare function stampQuerySuccess<T>(run: () => Promise<T>): Promise<T>;
|
|
32
54
|
/**
|
|
33
55
|
* Apply the unqualified-`findMany` ceiling to a Prisma client.
|
|
34
56
|
*
|
|
@@ -47,6 +69,22 @@ export declare function resolveFindManyTake(): number;
|
|
|
47
69
|
* @returns The client with the ceiling applied.
|
|
48
70
|
*/
|
|
49
71
|
export declare function withFindManyGuard(client: PrismaClient): PrismaClient;
|
|
72
|
+
/** Default statement timeout (30s) prevents hung queries from blocking pool slots indefinitely */
|
|
73
|
+
export declare const DEFAULT_STATEMENT_TIMEOUT_MS = 30000;
|
|
74
|
+
/** Heartbeat interval: how often we verify the DB connection is alive */
|
|
75
|
+
export declare const HEARTBEAT_INTERVAL_MS = 30000;
|
|
76
|
+
/** Consecutive heartbeat failures before triggering client reconnection */
|
|
77
|
+
export declare const MAX_HEARTBEAT_FAILURES = 3;
|
|
78
|
+
/**
|
|
79
|
+
* Resolve the per-statement timeout appended to a direct Postgres URL.
|
|
80
|
+
*
|
|
81
|
+
* Parsed exactly as the URL builder has always parsed it, so a value that is
|
|
82
|
+
* not a positive integer comes back as-is (NaN, 0, negative) for the caller to
|
|
83
|
+
* judge rather than being silently replaced.
|
|
84
|
+
*
|
|
85
|
+
* @returns The statement timeout in milliseconds.
|
|
86
|
+
*/
|
|
87
|
+
export declare function resolveStatementTimeoutMs(): number;
|
|
50
88
|
declare const prisma: PrismaClient;
|
|
51
89
|
/**
|
|
52
90
|
* Starts the periodic database heartbeat monitor.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"prismaClient.d.ts","sourceRoot":"","sources":["../../src/prismaClient.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAW9C;;GAEG;AACH,OAAO,CAAC,MAAM,CAAC;I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uCAAuC;IACvC,KAAK,SAAS,EAAE,CAAC;IAEjB,OAAO,GAAG,EAAE,CAAC,2BAA2B,EAAE,CAAC;AAC7C,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,2BAA2B;IACzC,IAAI,cAAc,EAAE,CAAC;QACnB,aAAa,CAAC,cAAc,CAAC,CAAC;QAC9B,cAAc,GAAG,SAAS,CAAC;QAC3B,MAAM,CAAC,IAAI,CAAC,4CAA4C,CAAC,CAAC;IAC5D,CAAC;AACH,CAAC;AAED;;;GAGG;AACH,MAAM,CAAC,KAAK,UAAU,qBAAqB,CAAC,SAAS,GAAG,IAAI;IAC1D,2BAA2B,EAAE,CAAC;IAE9B,IAAI,CAAC,MAAM,CAAC,MAAM;QAAE,OAAO;IAE3B,IAAI,CAAC;QACH,MAAM,OAAO,CAAC,IAAI,CAAC;YACjB,MAAM,CAAC,MAAM,CAAC,WAAW,EAAE;YAC3B,IAAI,OAAO,CAAO,CAAC,CAAC,EAAE,MAAM,EAAE,EAAE,CAC9B,UAAU,CACR,GAAG,EAAE,CACH,MAAM,CACJ,IAAI,KAAK,CAAC,qCAAqC,SAAS,IAAI,CAAC,CAC9D,EACH,SAAS,CACV,CACF;SACF,CAAC,CAAC;QACH,MAAM,CAAC,IAAI,CAAC,0CAA0C,CAAC,CAAC;IAC1D,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,MAAM,CAAC,IAAI,CAAC,8CAA8C,EAAE;YAC1D,KAAK,EAAE,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC;SAC9D,CAAC,CAAC;IACL,CAAC;AACH,CAAC;AAED,eAAe,MAAM,CAAC"}
|
package/esm/prismaClient.mjs
CHANGED
|
@@ -42,6 +42,36 @@ export function resolveFindManyTake() {
|
|
|
42
42
|
}
|
|
43
43
|
return parsed;
|
|
44
44
|
}
|
|
45
|
+
/** Wall-clock ms of the most recent query that completed successfully, or undefined before the first. */
|
|
46
|
+
let lastQuerySucceededAt;
|
|
47
|
+
/**
|
|
48
|
+
* When a query on this process's Prisma client last completed successfully.
|
|
49
|
+
*
|
|
50
|
+
* This is the evidence `/health` uses for "the database is answering this
|
|
51
|
+
* process". It is recorded as a side effect of ordinary traffic and of the
|
|
52
|
+
* heartbeat, so reading it never needs a pool connection — which is the point:
|
|
53
|
+
* a pool that is saturated but still completing work keeps this fresh, and a
|
|
54
|
+
* database that has stopped answering lets it go stale.
|
|
55
|
+
*
|
|
56
|
+
* @returns Epoch milliseconds of the last success, or undefined if none yet.
|
|
57
|
+
*/
|
|
58
|
+
export function getLastQuerySucceededAt() {
|
|
59
|
+
return lastQuerySucceededAt;
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Run a query and stamp the success time if it resolves.
|
|
63
|
+
*
|
|
64
|
+
* A rejected query leaves the stamp alone; any completed round trip, including
|
|
65
|
+
* one issued by a caller that has since given up waiting on it, refreshes it.
|
|
66
|
+
*
|
|
67
|
+
* @param run - Executes the underlying operation.
|
|
68
|
+
* @returns Whatever the operation returned.
|
|
69
|
+
*/
|
|
70
|
+
export async function stampQuerySuccess(run) {
|
|
71
|
+
const result = await run();
|
|
72
|
+
lastQuerySucceededAt = Date.now();
|
|
73
|
+
return result;
|
|
74
|
+
}
|
|
45
75
|
/**
|
|
46
76
|
* Apply the unqualified-`findMany` ceiling to a Prisma client.
|
|
47
77
|
*
|
|
@@ -61,7 +91,18 @@ export function resolveFindManyTake() {
|
|
|
61
91
|
*/
|
|
62
92
|
export function withFindManyGuard(client) {
|
|
63
93
|
const ceiling = resolveFindManyTake();
|
|
64
|
-
|
|
94
|
+
// The success stamp rides this wrapper because it is the one applied at both
|
|
95
|
+
// client assignment sites (initial and reconnect), so the stamp survives a
|
|
96
|
+
// reconnect without the reconnect path having to know about it.
|
|
97
|
+
const stamped = client.$extends({
|
|
98
|
+
name: 'query-success-stamp',
|
|
99
|
+
query: {
|
|
100
|
+
async $allOperations({ args, query }) {
|
|
101
|
+
return stampQuerySuccess(() => query(args));
|
|
102
|
+
},
|
|
103
|
+
},
|
|
104
|
+
});
|
|
105
|
+
const extended = stamped.$extends({
|
|
65
106
|
name: 'find-many-ceiling',
|
|
66
107
|
query: {
|
|
67
108
|
$allModels: {
|
|
@@ -85,11 +126,24 @@ export function withFindManyGuard(client) {
|
|
|
85
126
|
return extended;
|
|
86
127
|
}
|
|
87
128
|
/** Default statement timeout (30s) prevents hung queries from blocking pool slots indefinitely */
|
|
88
|
-
const DEFAULT_STATEMENT_TIMEOUT_MS = 30000;
|
|
129
|
+
export const DEFAULT_STATEMENT_TIMEOUT_MS = 30000;
|
|
89
130
|
/** Heartbeat interval: how often we verify the DB connection is alive */
|
|
90
|
-
const HEARTBEAT_INTERVAL_MS = 30000;
|
|
131
|
+
export const HEARTBEAT_INTERVAL_MS = 30000;
|
|
91
132
|
/** Consecutive heartbeat failures before triggering client reconnection */
|
|
92
|
-
const MAX_HEARTBEAT_FAILURES = 3;
|
|
133
|
+
export const MAX_HEARTBEAT_FAILURES = 3;
|
|
134
|
+
/**
|
|
135
|
+
* Resolve the per-statement timeout appended to a direct Postgres URL.
|
|
136
|
+
*
|
|
137
|
+
* Parsed exactly as the URL builder has always parsed it, so a value that is
|
|
138
|
+
* not a positive integer comes back as-is (NaN, 0, negative) for the caller to
|
|
139
|
+
* judge rather than being silently replaced.
|
|
140
|
+
*
|
|
141
|
+
* @returns The statement timeout in milliseconds.
|
|
142
|
+
*/
|
|
143
|
+
export function resolveStatementTimeoutMs() {
|
|
144
|
+
return parseInt(process.env.DATABASE_STATEMENT_TIMEOUT_MS ||
|
|
145
|
+
String(DEFAULT_STATEMENT_TIMEOUT_MS), 10);
|
|
146
|
+
}
|
|
93
147
|
/** Heartbeat query timeout (5s) — shorter than pool timeout to detect issues early */
|
|
94
148
|
const HEARTBEAT_TIMEOUT_MS = 5000;
|
|
95
149
|
/**
|
|
@@ -198,8 +252,7 @@ function buildDatabaseUrl() {
|
|
|
198
252
|
poolTimeoutMs: poolTimeout,
|
|
199
253
|
environment: process.env.NODE_ENV || 'development',
|
|
200
254
|
});
|
|
201
|
-
const statementTimeoutMs =
|
|
202
|
-
String(DEFAULT_STATEMENT_TIMEOUT_MS), 10);
|
|
255
|
+
const statementTimeoutMs = resolveStatementTimeoutMs();
|
|
203
256
|
return `${baseUrl}${separator}connection_limit=${poolSize}&pool_timeout=${Math.floor(poolTimeout / 1000)}&statement_timeout=${statementTimeoutMs}&idle_in_transaction_session_timeout=${statementTimeoutMs}`;
|
|
204
257
|
}
|
|
205
258
|
// Create a singleton that works in all environments
|
package/health.cjs
CHANGED
|
@@ -1,11 +1,44 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var
|
|
3
|
-
|
|
4
|
-
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
5
35
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
exports.HEALTH_PROBE_TIMEOUT_MS = exports.DB_SUCCESS_FRESHNESS_MS = void 0;
|
|
37
|
+
exports.warnIfStatementTimeoutDefeatsFreshness = warnIfStatementTimeoutDefeatsFreshness;
|
|
38
|
+
exports.checkDatabaseReadiness = checkDatabaseReadiness;
|
|
6
39
|
exports.createHealthRouter = createHealthRouter;
|
|
7
40
|
const express_1 = require("express");
|
|
8
|
-
const prismaClient_1 =
|
|
41
|
+
const prismaClient_1 = __importStar(require("./prismaClient.cjs"));
|
|
9
42
|
const logger_1 = require("./utils/logger.cjs");
|
|
10
43
|
const SERVICE_NAME = 'backend-legacy';
|
|
11
44
|
/**
|
|
@@ -24,6 +57,136 @@ function getPackageVersion() {
|
|
|
24
57
|
const PACKAGE_VERSION = getPackageVersion();
|
|
25
58
|
/** Tracks the process start time for uptime calculation */
|
|
26
59
|
const startedAt = Date.now();
|
|
60
|
+
/**
|
|
61
|
+
* How recently a query must have completed for `/health` to accept it as
|
|
62
|
+
* proof the database is answering, without issuing a query of its own.
|
|
63
|
+
*
|
|
64
|
+
* Derived from the heartbeat rather than set by hand: it tolerates every
|
|
65
|
+
* heartbeat but the last before reconnection being missed, plus half an
|
|
66
|
+
* interval of slack — `(MAX_HEARTBEAT_FAILURES - 0.5) * HEARTBEAT_INTERVAL_MS`,
|
|
67
|
+
* 75s at the current 3 x 30s. That keeps it longer than one heartbeat interval,
|
|
68
|
+
* so an idle process that the heartbeat keeps refreshing never goes stale, and
|
|
69
|
+
* shorter than the reconnect point, so a database that has stopped answering
|
|
70
|
+
* reads as disconnected before the heartbeat gives up on the client.
|
|
71
|
+
*
|
|
72
|
+
* A saturated pool still completes queries, each bounded by the statement
|
|
73
|
+
* timeout, so saturation alone does not let it go stale — provided that
|
|
74
|
+
* timeout is shorter than this window (see
|
|
75
|
+
* {@link warnIfStatementTimeoutDefeatsFreshness}).
|
|
76
|
+
*/
|
|
77
|
+
exports.DB_SUCCESS_FRESHNESS_MS = (prismaClient_1.MAX_HEARTBEAT_FAILURES - 0.5) * prismaClient_1.HEARTBEAT_INTERVAL_MS;
|
|
78
|
+
/**
|
|
79
|
+
* Warn when the configured statement timeout is not shorter than the
|
|
80
|
+
* freshness window.
|
|
81
|
+
*
|
|
82
|
+
* The case for trusting a recent success under saturation is that every query
|
|
83
|
+
* on a busy pool finishes or fails within the statement timeout, so completed
|
|
84
|
+
* queries keep arriving inside {@link DB_SUCCESS_FRESHNESS_MS}. A timeout at or
|
|
85
|
+
* above the window breaks that: a pool full of long-running statements can go
|
|
86
|
+
* a whole window without a completion and `/health` would fall back to a probe
|
|
87
|
+
* that cannot get a connection. A value that is not a positive integer (unset
|
|
88
|
+
* timeouts parse from the default; `0` disables the timeout in Postgres) is
|
|
89
|
+
* treated the same way, since it bounds nothing.
|
|
90
|
+
*
|
|
91
|
+
* @param statementTimeoutMs - The resolved statement timeout (injectable for tests).
|
|
92
|
+
* @returns True if a warning was logged.
|
|
93
|
+
*/
|
|
94
|
+
function warnIfStatementTimeoutDefeatsFreshness(statementTimeoutMs = (0, prismaClient_1.resolveStatementTimeoutMs)()) {
|
|
95
|
+
if (Number.isFinite(statementTimeoutMs) &&
|
|
96
|
+
statementTimeoutMs > 0 &&
|
|
97
|
+
statementTimeoutMs < exports.DB_SUCCESS_FRESHNESS_MS) {
|
|
98
|
+
return false;
|
|
99
|
+
}
|
|
100
|
+
logger_1.logger.warn('DATABASE_STATEMENT_TIMEOUT_MS is not shorter than the /health freshness window — a saturated pool can read as disconnected', {
|
|
101
|
+
statementTimeoutMs,
|
|
102
|
+
freshnessWindowMs: exports.DB_SUCCESS_FRESHNESS_MS,
|
|
103
|
+
});
|
|
104
|
+
return true;
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Budget for the fallback probe when no recent success exists. Well under the
|
|
108
|
+
* 10s pool wait so a saturated pool yields an answer inside the platform's
|
|
109
|
+
* probe timeout rather than hanging the request.
|
|
110
|
+
*/
|
|
111
|
+
exports.HEALTH_PROBE_TIMEOUT_MS = 2000;
|
|
112
|
+
/** The one probe query this module may have outstanding on the pool. */
|
|
113
|
+
let outstandingProbe;
|
|
114
|
+
/**
|
|
115
|
+
* Probe the live client with `SELECT 1`, giving up after `timeoutMs`.
|
|
116
|
+
*
|
|
117
|
+
* Reads `global.prisma` rather than the module-level import, because the
|
|
118
|
+
* heartbeat's reconnect path replaces `global.prisma` and the import stays
|
|
119
|
+
* pinned to the original (by then disconnected) client.
|
|
120
|
+
*
|
|
121
|
+
* At most one probe query is ever outstanding: a caller that arrives while a
|
|
122
|
+
* previous probe is still queued on the pool waits on that same query instead
|
|
123
|
+
* of adding another waiter, so frequent health checks cannot themselves deepen
|
|
124
|
+
* a saturated pool's queue.
|
|
125
|
+
*
|
|
126
|
+
* @param timeoutMs - How long to wait for the probe before calling it failed.
|
|
127
|
+
* @returns True if the probe completed within the budget.
|
|
128
|
+
*/
|
|
129
|
+
async function probeLiveClient(timeoutMs) {
|
|
130
|
+
var _a;
|
|
131
|
+
if (!outstandingProbe) {
|
|
132
|
+
const client = (_a = global.prisma) !== null && _a !== void 0 ? _a : prismaClient_1.default;
|
|
133
|
+
outstandingProbe = Promise.resolve(client.$queryRaw `SELECT 1`).finally(() => {
|
|
134
|
+
outstandingProbe = undefined;
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
const probe = outstandingProbe;
|
|
138
|
+
let timer;
|
|
139
|
+
const timedOut = new Promise((resolve) => {
|
|
140
|
+
timer = setTimeout(() => resolve(false), timeoutMs);
|
|
141
|
+
});
|
|
142
|
+
try {
|
|
143
|
+
return await Promise.race([
|
|
144
|
+
probe.then(() => true, (dbError) => {
|
|
145
|
+
logger_1.logger.warn('Health check: database probe failed', {
|
|
146
|
+
error: dbError instanceof Error ? dbError.message : String(dbError),
|
|
147
|
+
});
|
|
148
|
+
return false;
|
|
149
|
+
}),
|
|
150
|
+
timedOut,
|
|
151
|
+
]);
|
|
152
|
+
}
|
|
153
|
+
finally {
|
|
154
|
+
clearTimeout(timer);
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* Decide whether the database is answering this process, without depending
|
|
159
|
+
* on a free pool connection when there is already evidence that it is.
|
|
160
|
+
*
|
|
161
|
+
* A query that completed within {@link DB_SUCCESS_FRESHNESS_MS} is taken as
|
|
162
|
+
* that evidence and no query is issued. Only when there is none (startup, or a
|
|
163
|
+
* database that has stopped answering) does it fall back to a bounded probe.
|
|
164
|
+
*
|
|
165
|
+
* @param now - Current epoch ms (injectable for tests).
|
|
166
|
+
* @returns The database status, how it was decided, and the success age.
|
|
167
|
+
*/
|
|
168
|
+
async function checkDatabaseReadiness(now = Date.now()) {
|
|
169
|
+
const lastSuccess = (0, prismaClient_1.getLastQuerySucceededAt)();
|
|
170
|
+
const ageMs = lastSuccess === undefined ? undefined : now - lastSuccess;
|
|
171
|
+
if (ageMs !== undefined && ageMs <= exports.DB_SUCCESS_FRESHNESS_MS) {
|
|
172
|
+
return {
|
|
173
|
+
database: 'connected',
|
|
174
|
+
databaseCheck: 'recent-query',
|
|
175
|
+
lastQuerySucceededSecondsAgo: Math.max(0, Math.floor(ageMs / 1000)),
|
|
176
|
+
};
|
|
177
|
+
}
|
|
178
|
+
const answered = await probeLiveClient(exports.HEALTH_PROBE_TIMEOUT_MS);
|
|
179
|
+
// A successful probe goes through the same client extension and refreshes
|
|
180
|
+
// the stamp; report the age as it stands after the probe.
|
|
181
|
+
const after = (0, prismaClient_1.getLastQuerySucceededAt)();
|
|
182
|
+
return {
|
|
183
|
+
database: answered ? 'connected' : 'disconnected',
|
|
184
|
+
databaseCheck: 'probe',
|
|
185
|
+
lastQuerySucceededSecondsAgo: after === undefined
|
|
186
|
+
? null
|
|
187
|
+
: Math.max(0, Math.floor((Date.now() - after) / 1000)),
|
|
188
|
+
};
|
|
189
|
+
}
|
|
27
190
|
/**
|
|
28
191
|
* Checks database connectivity by issuing a lightweight query.
|
|
29
192
|
* Returns 'connected' or 'disconnected'. Never throws.
|
|
@@ -50,13 +213,18 @@ async function checkDatabase() {
|
|
|
50
213
|
* is reachable (`SELECT 1`), HTTP 503 otherwise. Suitable for Cloud Run
|
|
51
214
|
* startup probes and load-balancer readiness.
|
|
52
215
|
* - `GET /health` — Full health snapshot including memory usage and database
|
|
53
|
-
* status. Returns 503 when the database
|
|
54
|
-
*
|
|
216
|
+
* status. Returns 503 only when the database has stopped answering this
|
|
217
|
+
* process: a query that completed within the freshness window counts as
|
|
218
|
+
* proof without taking a pool connection, so a saturated-but-working pool
|
|
219
|
+
* reads as healthy. With no recent success it falls back to a bounded
|
|
220
|
+
* probe (see {@link checkDatabaseReadiness}). Kept for backward
|
|
221
|
+
* compatibility with existing Railway/uptime checks.
|
|
55
222
|
*
|
|
56
223
|
* All endpoints should be mounted before auth middleware so they remain
|
|
57
224
|
* accessible without authentication.
|
|
58
225
|
*/
|
|
59
226
|
function createHealthRouter() {
|
|
227
|
+
warnIfStatementTimeoutDefeatsFreshness();
|
|
60
228
|
const router = (0, express_1.Router)();
|
|
61
229
|
router.get('/livez', (_req, res) => {
|
|
62
230
|
res.status(200).json({
|
|
@@ -79,7 +247,8 @@ function createHealthRouter() {
|
|
|
79
247
|
});
|
|
80
248
|
});
|
|
81
249
|
router.get('/health', async (_req, res) => {
|
|
82
|
-
const
|
|
250
|
+
const readiness = await checkDatabaseReadiness();
|
|
251
|
+
const databaseStatus = readiness.database;
|
|
83
252
|
const isHealthy = databaseStatus === 'connected';
|
|
84
253
|
const statusCode = isHealthy ? 200 : 503;
|
|
85
254
|
const memoryUsage = process.memoryUsage();
|
|
@@ -95,6 +264,8 @@ function createHealthRouter() {
|
|
|
95
264
|
heapTotal: Math.round(memoryUsage.heapTotal / 1024 / 1024),
|
|
96
265
|
},
|
|
97
266
|
database: databaseStatus,
|
|
267
|
+
databaseCheck: readiness.databaseCheck,
|
|
268
|
+
lastQuerySucceededSecondsAgo: readiness.lastQuerySucceededSecondsAgo,
|
|
98
269
|
};
|
|
99
270
|
res.status(statusCode).json(body);
|
|
100
271
|
});
|
package/health.d.ts
CHANGED
|
@@ -1,4 +1,62 @@
|
|
|
1
1
|
import { Router } from 'express';
|
|
2
|
+
type DatabaseCheck = 'recent-query' | 'probe';
|
|
3
|
+
/**
|
|
4
|
+
* How recently a query must have completed for `/health` to accept it as
|
|
5
|
+
* proof the database is answering, without issuing a query of its own.
|
|
6
|
+
*
|
|
7
|
+
* Derived from the heartbeat rather than set by hand: it tolerates every
|
|
8
|
+
* heartbeat but the last before reconnection being missed, plus half an
|
|
9
|
+
* interval of slack — `(MAX_HEARTBEAT_FAILURES - 0.5) * HEARTBEAT_INTERVAL_MS`,
|
|
10
|
+
* 75s at the current 3 x 30s. That keeps it longer than one heartbeat interval,
|
|
11
|
+
* so an idle process that the heartbeat keeps refreshing never goes stale, and
|
|
12
|
+
* shorter than the reconnect point, so a database that has stopped answering
|
|
13
|
+
* reads as disconnected before the heartbeat gives up on the client.
|
|
14
|
+
*
|
|
15
|
+
* A saturated pool still completes queries, each bounded by the statement
|
|
16
|
+
* timeout, so saturation alone does not let it go stale — provided that
|
|
17
|
+
* timeout is shorter than this window (see
|
|
18
|
+
* {@link warnIfStatementTimeoutDefeatsFreshness}).
|
|
19
|
+
*/
|
|
20
|
+
export declare const DB_SUCCESS_FRESHNESS_MS: number;
|
|
21
|
+
/**
|
|
22
|
+
* Warn when the configured statement timeout is not shorter than the
|
|
23
|
+
* freshness window.
|
|
24
|
+
*
|
|
25
|
+
* The case for trusting a recent success under saturation is that every query
|
|
26
|
+
* on a busy pool finishes or fails within the statement timeout, so completed
|
|
27
|
+
* queries keep arriving inside {@link DB_SUCCESS_FRESHNESS_MS}. A timeout at or
|
|
28
|
+
* above the window breaks that: a pool full of long-running statements can go
|
|
29
|
+
* a whole window without a completion and `/health` would fall back to a probe
|
|
30
|
+
* that cannot get a connection. A value that is not a positive integer (unset
|
|
31
|
+
* timeouts parse from the default; `0` disables the timeout in Postgres) is
|
|
32
|
+
* treated the same way, since it bounds nothing.
|
|
33
|
+
*
|
|
34
|
+
* @param statementTimeoutMs - The resolved statement timeout (injectable for tests).
|
|
35
|
+
* @returns True if a warning was logged.
|
|
36
|
+
*/
|
|
37
|
+
export declare function warnIfStatementTimeoutDefeatsFreshness(statementTimeoutMs?: number): boolean;
|
|
38
|
+
/**
|
|
39
|
+
* Budget for the fallback probe when no recent success exists. Well under the
|
|
40
|
+
* 10s pool wait so a saturated pool yields an answer inside the platform's
|
|
41
|
+
* probe timeout rather than hanging the request.
|
|
42
|
+
*/
|
|
43
|
+
export declare const HEALTH_PROBE_TIMEOUT_MS = 2000;
|
|
44
|
+
/**
|
|
45
|
+
* Decide whether the database is answering this process, without depending
|
|
46
|
+
* on a free pool connection when there is already evidence that it is.
|
|
47
|
+
*
|
|
48
|
+
* A query that completed within {@link DB_SUCCESS_FRESHNESS_MS} is taken as
|
|
49
|
+
* that evidence and no query is issued. Only when there is none (startup, or a
|
|
50
|
+
* database that has stopped answering) does it fall back to a bounded probe.
|
|
51
|
+
*
|
|
52
|
+
* @param now - Current epoch ms (injectable for tests).
|
|
53
|
+
* @returns The database status, how it was decided, and the success age.
|
|
54
|
+
*/
|
|
55
|
+
export declare function checkDatabaseReadiness(now?: number): Promise<{
|
|
56
|
+
database: 'connected' | 'disconnected';
|
|
57
|
+
databaseCheck: DatabaseCheck;
|
|
58
|
+
lastQuerySucceededSecondsAgo: number | null;
|
|
59
|
+
}>;
|
|
2
60
|
/**
|
|
3
61
|
* Creates an Express router with health-probe endpoints.
|
|
4
62
|
*
|
|
@@ -9,11 +67,16 @@ import { Router } from 'express';
|
|
|
9
67
|
* is reachable (`SELECT 1`), HTTP 503 otherwise. Suitable for Cloud Run
|
|
10
68
|
* startup probes and load-balancer readiness.
|
|
11
69
|
* - `GET /health` — Full health snapshot including memory usage and database
|
|
12
|
-
* status. Returns 503 when the database
|
|
13
|
-
*
|
|
70
|
+
* status. Returns 503 only when the database has stopped answering this
|
|
71
|
+
* process: a query that completed within the freshness window counts as
|
|
72
|
+
* proof without taking a pool connection, so a saturated-but-working pool
|
|
73
|
+
* reads as healthy. With no recent success it falls back to a bounded
|
|
74
|
+
* probe (see {@link checkDatabaseReadiness}). Kept for backward
|
|
75
|
+
* compatibility with existing Railway/uptime checks.
|
|
14
76
|
*
|
|
15
77
|
* All endpoints should be mounted before auth middleware so they remain
|
|
16
78
|
* accessible without authentication.
|
|
17
79
|
*/
|
|
18
80
|
export declare function createHealthRouter(): Router;
|
|
81
|
+
export {};
|
|
19
82
|
//# sourceMappingURL=health.d.ts.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@adaptic/backend-legacy",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.1044",
|
|
4
4
|
"description": "Backend executable CRUD functions with dynamic variables construction, and type definitions for the Adaptic AI platform.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"types": "index.d.ts",
|
package/prismaClient.cjs
CHANGED
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.DEFAULT_FIND_MANY_TAKE = void 0;
|
|
3
|
+
exports.MAX_HEARTBEAT_FAILURES = exports.HEARTBEAT_INTERVAL_MS = exports.DEFAULT_STATEMENT_TIMEOUT_MS = exports.DEFAULT_FIND_MANY_TAKE = void 0;
|
|
4
4
|
exports.resolveFindManyTake = resolveFindManyTake;
|
|
5
|
+
exports.getLastQuerySucceededAt = getLastQuerySucceededAt;
|
|
6
|
+
exports.stampQuerySuccess = stampQuerySuccess;
|
|
5
7
|
exports.withFindManyGuard = withFindManyGuard;
|
|
8
|
+
exports.resolveStatementTimeoutMs = resolveStatementTimeoutMs;
|
|
6
9
|
exports.startConnectionHealthMonitor = startConnectionHealthMonitor;
|
|
7
10
|
exports.stopConnectionHealthMonitor = stopConnectionHealthMonitor;
|
|
8
11
|
exports.disconnectWithTimeout = disconnectWithTimeout;
|
|
@@ -50,6 +53,36 @@ function resolveFindManyTake() {
|
|
|
50
53
|
}
|
|
51
54
|
return parsed;
|
|
52
55
|
}
|
|
56
|
+
/** Wall-clock ms of the most recent query that completed successfully, or undefined before the first. */
|
|
57
|
+
let lastQuerySucceededAt;
|
|
58
|
+
/**
|
|
59
|
+
* When a query on this process's Prisma client last completed successfully.
|
|
60
|
+
*
|
|
61
|
+
* This is the evidence `/health` uses for "the database is answering this
|
|
62
|
+
* process". It is recorded as a side effect of ordinary traffic and of the
|
|
63
|
+
* heartbeat, so reading it never needs a pool connection — which is the point:
|
|
64
|
+
* a pool that is saturated but still completing work keeps this fresh, and a
|
|
65
|
+
* database that has stopped answering lets it go stale.
|
|
66
|
+
*
|
|
67
|
+
* @returns Epoch milliseconds of the last success, or undefined if none yet.
|
|
68
|
+
*/
|
|
69
|
+
function getLastQuerySucceededAt() {
|
|
70
|
+
return lastQuerySucceededAt;
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Run a query and stamp the success time if it resolves.
|
|
74
|
+
*
|
|
75
|
+
* A rejected query leaves the stamp alone; any completed round trip, including
|
|
76
|
+
* one issued by a caller that has since given up waiting on it, refreshes it.
|
|
77
|
+
*
|
|
78
|
+
* @param run - Executes the underlying operation.
|
|
79
|
+
* @returns Whatever the operation returned.
|
|
80
|
+
*/
|
|
81
|
+
async function stampQuerySuccess(run) {
|
|
82
|
+
const result = await run();
|
|
83
|
+
lastQuerySucceededAt = Date.now();
|
|
84
|
+
return result;
|
|
85
|
+
}
|
|
53
86
|
/**
|
|
54
87
|
* Apply the unqualified-`findMany` ceiling to a Prisma client.
|
|
55
88
|
*
|
|
@@ -69,7 +102,18 @@ function resolveFindManyTake() {
|
|
|
69
102
|
*/
|
|
70
103
|
function withFindManyGuard(client) {
|
|
71
104
|
const ceiling = resolveFindManyTake();
|
|
72
|
-
|
|
105
|
+
// The success stamp rides this wrapper because it is the one applied at both
|
|
106
|
+
// client assignment sites (initial and reconnect), so the stamp survives a
|
|
107
|
+
// reconnect without the reconnect path having to know about it.
|
|
108
|
+
const stamped = client.$extends({
|
|
109
|
+
name: 'query-success-stamp',
|
|
110
|
+
query: {
|
|
111
|
+
async $allOperations({ args, query }) {
|
|
112
|
+
return stampQuerySuccess(() => query(args));
|
|
113
|
+
},
|
|
114
|
+
},
|
|
115
|
+
});
|
|
116
|
+
const extended = stamped.$extends({
|
|
73
117
|
name: 'find-many-ceiling',
|
|
74
118
|
query: {
|
|
75
119
|
$allModels: {
|
|
@@ -93,11 +137,24 @@ function withFindManyGuard(client) {
|
|
|
93
137
|
return extended;
|
|
94
138
|
}
|
|
95
139
|
/** Default statement timeout (30s) prevents hung queries from blocking pool slots indefinitely */
|
|
96
|
-
|
|
140
|
+
exports.DEFAULT_STATEMENT_TIMEOUT_MS = 30000;
|
|
97
141
|
/** Heartbeat interval: how often we verify the DB connection is alive */
|
|
98
|
-
|
|
142
|
+
exports.HEARTBEAT_INTERVAL_MS = 30000;
|
|
99
143
|
/** Consecutive heartbeat failures before triggering client reconnection */
|
|
100
|
-
|
|
144
|
+
exports.MAX_HEARTBEAT_FAILURES = 3;
|
|
145
|
+
/**
|
|
146
|
+
* Resolve the per-statement timeout appended to a direct Postgres URL.
|
|
147
|
+
*
|
|
148
|
+
* Parsed exactly as the URL builder has always parsed it, so a value that is
|
|
149
|
+
* not a positive integer comes back as-is (NaN, 0, negative) for the caller to
|
|
150
|
+
* judge rather than being silently replaced.
|
|
151
|
+
*
|
|
152
|
+
* @returns The statement timeout in milliseconds.
|
|
153
|
+
*/
|
|
154
|
+
function resolveStatementTimeoutMs() {
|
|
155
|
+
return parseInt(process.env.DATABASE_STATEMENT_TIMEOUT_MS ||
|
|
156
|
+
String(exports.DEFAULT_STATEMENT_TIMEOUT_MS), 10);
|
|
157
|
+
}
|
|
101
158
|
/** Heartbeat query timeout (5s) — shorter than pool timeout to detect issues early */
|
|
102
159
|
const HEARTBEAT_TIMEOUT_MS = 5000;
|
|
103
160
|
/**
|
|
@@ -206,8 +263,7 @@ function buildDatabaseUrl() {
|
|
|
206
263
|
poolTimeoutMs: poolTimeout,
|
|
207
264
|
environment: process.env.NODE_ENV || 'development',
|
|
208
265
|
});
|
|
209
|
-
const statementTimeoutMs =
|
|
210
|
-
String(DEFAULT_STATEMENT_TIMEOUT_MS), 10);
|
|
266
|
+
const statementTimeoutMs = resolveStatementTimeoutMs();
|
|
211
267
|
return `${baseUrl}${separator}connection_limit=${poolSize}&pool_timeout=${Math.floor(poolTimeout / 1000)}&statement_timeout=${statementTimeoutMs}&idle_in_transaction_session_timeout=${statementTimeoutMs}`;
|
|
212
268
|
}
|
|
213
269
|
// Create a singleton that works in all environments
|
|
@@ -347,7 +403,7 @@ async function heartbeat() {
|
|
|
347
403
|
consecutiveFailures: heartbeatFailures,
|
|
348
404
|
error: errorMessage,
|
|
349
405
|
});
|
|
350
|
-
if (heartbeatFailures >= MAX_HEARTBEAT_FAILURES) {
|
|
406
|
+
if (heartbeatFailures >= exports.MAX_HEARTBEAT_FAILURES) {
|
|
351
407
|
logger_1.logger.error('Database heartbeat exceeded failure threshold, reconnecting Prisma client', {
|
|
352
408
|
consecutiveFailures: heartbeatFailures,
|
|
353
409
|
});
|
|
@@ -417,12 +473,12 @@ function startConnectionHealthMonitor() {
|
|
|
417
473
|
if (heartbeatTimer)
|
|
418
474
|
return () => stopConnectionHealthMonitor();
|
|
419
475
|
logger_1.logger.info('Starting database connection health monitor', {
|
|
420
|
-
intervalMs: HEARTBEAT_INTERVAL_MS,
|
|
421
|
-
failureThreshold: MAX_HEARTBEAT_FAILURES,
|
|
476
|
+
intervalMs: exports.HEARTBEAT_INTERVAL_MS,
|
|
477
|
+
failureThreshold: exports.MAX_HEARTBEAT_FAILURES,
|
|
422
478
|
});
|
|
423
479
|
heartbeatTimer = setInterval(() => {
|
|
424
480
|
void heartbeat();
|
|
425
|
-
}, HEARTBEAT_INTERVAL_MS);
|
|
481
|
+
}, exports.HEARTBEAT_INTERVAL_MS);
|
|
426
482
|
// Run an initial heartbeat immediately
|
|
427
483
|
void heartbeat();
|
|
428
484
|
return () => stopConnectionHealthMonitor();
|
package/prismaClient.d.ts
CHANGED
|
@@ -29,6 +29,28 @@ export declare const DEFAULT_FIND_MANY_TAKE = 50000;
|
|
|
29
29
|
* @returns The row ceiling to apply to an unqualified `findMany`.
|
|
30
30
|
*/
|
|
31
31
|
export declare function resolveFindManyTake(): number;
|
|
32
|
+
/**
|
|
33
|
+
* When a query on this process's Prisma client last completed successfully.
|
|
34
|
+
*
|
|
35
|
+
* This is the evidence `/health` uses for "the database is answering this
|
|
36
|
+
* process". It is recorded as a side effect of ordinary traffic and of the
|
|
37
|
+
* heartbeat, so reading it never needs a pool connection — which is the point:
|
|
38
|
+
* a pool that is saturated but still completing work keeps this fresh, and a
|
|
39
|
+
* database that has stopped answering lets it go stale.
|
|
40
|
+
*
|
|
41
|
+
* @returns Epoch milliseconds of the last success, or undefined if none yet.
|
|
42
|
+
*/
|
|
43
|
+
export declare function getLastQuerySucceededAt(): number | undefined;
|
|
44
|
+
/**
|
|
45
|
+
* Run a query and stamp the success time if it resolves.
|
|
46
|
+
*
|
|
47
|
+
* A rejected query leaves the stamp alone; any completed round trip, including
|
|
48
|
+
* one issued by a caller that has since given up waiting on it, refreshes it.
|
|
49
|
+
*
|
|
50
|
+
* @param run - Executes the underlying operation.
|
|
51
|
+
* @returns Whatever the operation returned.
|
|
52
|
+
*/
|
|
53
|
+
export declare function stampQuerySuccess<T>(run: () => Promise<T>): Promise<T>;
|
|
32
54
|
/**
|
|
33
55
|
* Apply the unqualified-`findMany` ceiling to a Prisma client.
|
|
34
56
|
*
|
|
@@ -47,6 +69,22 @@ export declare function resolveFindManyTake(): number;
|
|
|
47
69
|
* @returns The client with the ceiling applied.
|
|
48
70
|
*/
|
|
49
71
|
export declare function withFindManyGuard(client: PrismaClient): PrismaClient;
|
|
72
|
+
/** Default statement timeout (30s) prevents hung queries from blocking pool slots indefinitely */
|
|
73
|
+
export declare const DEFAULT_STATEMENT_TIMEOUT_MS = 30000;
|
|
74
|
+
/** Heartbeat interval: how often we verify the DB connection is alive */
|
|
75
|
+
export declare const HEARTBEAT_INTERVAL_MS = 30000;
|
|
76
|
+
/** Consecutive heartbeat failures before triggering client reconnection */
|
|
77
|
+
export declare const MAX_HEARTBEAT_FAILURES = 3;
|
|
78
|
+
/**
|
|
79
|
+
* Resolve the per-statement timeout appended to a direct Postgres URL.
|
|
80
|
+
*
|
|
81
|
+
* Parsed exactly as the URL builder has always parsed it, so a value that is
|
|
82
|
+
* not a positive integer comes back as-is (NaN, 0, negative) for the caller to
|
|
83
|
+
* judge rather than being silently replaced.
|
|
84
|
+
*
|
|
85
|
+
* @returns The statement timeout in milliseconds.
|
|
86
|
+
*/
|
|
87
|
+
export declare function resolveStatementTimeoutMs(): number;
|
|
50
88
|
declare const prisma: PrismaClient;
|
|
51
89
|
/**
|
|
52
90
|
* Starts the periodic database heartbeat monitor.
|