fix: align api keepalive with gateway pool
This commit is contained in:
@@ -0,0 +1,20 @@
|
||||
import { configureApiHttpServerTimeouts } from './http-server-timeouts';
|
||||
|
||||
describe('configureApiHttpServerTimeouts', () => {
|
||||
it('keeps the API connection alive longer than the Gateway idle pool', () => {
|
||||
const server = { keepAliveTimeout: 0, headersTimeout: 0 };
|
||||
expect(configureApiHttpServerTimeouts(server, {})).toEqual({
|
||||
keepAliveTimeoutMs: 120_000,
|
||||
headersTimeoutMs: 125_000,
|
||||
});
|
||||
expect(server).toEqual({ keepAliveTimeout: 120_000, headersTimeout: 125_000 });
|
||||
});
|
||||
|
||||
it('keeps headers timeout above a configured keep-alive timeout', () => {
|
||||
const server = { keepAliveTimeout: 0, headersTimeout: 0 };
|
||||
expect(configureApiHttpServerTimeouts(server, {
|
||||
API_HTTP_KEEP_ALIVE_TIMEOUT_MS: '90000',
|
||||
API_HTTP_HEADERS_TIMEOUT_MS: '1000',
|
||||
})).toEqual({ keepAliveTimeoutMs: 90_000, headersTimeoutMs: 91_000 });
|
||||
});
|
||||
});
|
||||
@@ -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;
|
||||
}
|
||||
+3
-1
@@ -7,6 +7,7 @@ import { AppModule } from './app.module';
|
||||
import { MetricsService } from './metrics/metrics.service';
|
||||
import { OpenApiModule } from './open-api/open-api.module';
|
||||
import { configureHttpBodyParsers } from './http-body-limits';
|
||||
import { configureApiHttpServerTimeouts } from './http-server-timeouts';
|
||||
|
||||
Object.defineProperty(BigInt.prototype, 'toJSON', {
|
||||
configurable: true,
|
||||
@@ -43,7 +44,8 @@ async function bootstrap() {
|
||||
const port = Number(process.env.API_PORT ?? 3000);
|
||||
// 生产环境只允许 Nginx 访问管理 API;显式绑定回环,避免默认的全网卡监听绕过入口鉴权与限流。
|
||||
const host = process.env.API_HOST?.trim() || '127.0.0.1';
|
||||
await app.listen(port, host);
|
||||
const apiServer = await app.listen(port, host);
|
||||
configureApiHttpServerTimeouts(apiServer, process.env);
|
||||
|
||||
const metrics = app.get(MetricsService);
|
||||
const metricsHost = process.env.API_METRICS_HOST?.trim() || '127.0.0.1';
|
||||
|
||||
Reference in New Issue
Block a user