Files
lislgosms/api/src/http-server-timeouts.ts
T

22 lines
917 B
TypeScript

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;
}