fix: align api keepalive with gateway pool

This commit is contained in:
hectorzhao
2026-08-25 17:35:18 +08:00
parent 76e7c8c401
commit 394949f9f6
8 changed files with 64 additions and 1 deletions
+21
View File
@@ -0,0 +1,21 @@
import type { Server } from 'node:http';
const DEFAULT_KEEP_ALIVE_TIMEOUT_MS = 120_000;
const DEFAULT_HEADERS_TIMEOUT_MS = 125_000;
export function configureApiHttpServerTimeouts(
server: Pick<Server, 'keepAliveTimeout' | 'headersTimeout'>,
env: NodeJS.ProcessEnv,
) {
const keepAliveTimeoutMs = positiveInteger(env.API_HTTP_KEEP_ALIVE_TIMEOUT_MS, DEFAULT_KEEP_ALIVE_TIMEOUT_MS);
const configuredHeadersTimeoutMs = positiveInteger(env.API_HTTP_HEADERS_TIMEOUT_MS, DEFAULT_HEADERS_TIMEOUT_MS);
const headersTimeoutMs = Math.max(configuredHeadersTimeoutMs, keepAliveTimeoutMs + 1_000);
server.keepAliveTimeout = keepAliveTimeoutMs;
server.headersTimeout = headersTimeoutMs;
return { keepAliveTimeoutMs, headersTimeoutMs };
}
function positiveInteger(value: string | undefined, fallback: number) {
const parsed = Number(value);
return Number.isInteger(parsed) && parsed > 0 ? parsed : fallback;
}