Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ SERVER_TYPE=http
SERVER_PORT=8080
# Server URL - Set your application url
SERVER_URL=http://localhost:8080
# How long (ms) idle keep-alive connections stay open. Keep it above the idle timeout of
# your clients and reverse proxy (Go net/http: 90 s; most proxies: 60 s) to avoid ECONNRESET
# on reused connections. Must be a positive integer; invalid values fall back to the default.
SERVER_KEEPALIVE_TIMEOUT=95000

SSL_CONF_PRIVKEY=/path/to/cert.key
SSL_CONF_FULLCHAIN=/path/to/cert.crt
Expand Down
6 changes: 6 additions & 0 deletions src/config/env.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export type HttpServer = {
URL: string;
DISABLE_DOCS: boolean;
DISABLE_MANAGER: boolean;
KEEPALIVE_TIMEOUT: number;
};

export type HttpMethods = 'POST' | 'GET' | 'PUT' | 'DELETE';
Expand Down Expand Up @@ -470,6 +471,10 @@ export class ConfigService {
}

private envProcess(): Env {
// Must exceed the idle timeout of pooled clients (Go net/http: 90 s) so the client closes idle sockets first.
// Only positive integers are accepted; 0 would disable the timeout and negatives break headersTimeout.
const keepAliveTimeout = Number.parseInt(process.env?.SERVER_KEEPALIVE_TIMEOUT);

return {
SERVER: {
NAME: process.env?.SERVER_NAME || 'evolution',
Expand All @@ -478,6 +483,7 @@ export class ConfigService {
URL: process.env.SERVER_URL,
DISABLE_DOCS: process.env?.SERVER_DISABLE_DOCS === 'true',
DISABLE_MANAGER: process.env?.SERVER_DISABLE_MANAGER === 'true',
KEEPALIVE_TIMEOUT: Number.isInteger(keepAliveTimeout) && keepAliveTimeout > 0 ? keepAliveTimeout : 95000,
},
CORS: {
ORIGIN: process.env.CORS_ORIGIN?.split(',') || ['*'],
Expand Down
7 changes: 7 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,13 @@ async function bootstrap() {
server = ServerUP[httpServer.TYPE];
}

// Node closes idle keep-alive sockets after 5 s (+1 s buffer). Clients that pool connections and ignore the
// `Keep-Alive: timeout` header (Go, Python, Java, most reverse proxies) can reuse a socket right as it closes
// and get ECONNRESET / "socket hang up" on a POST that never reached the API. Keep idle sockets open longer
// than typical client and proxy idle timeouts (Go: 90 s, load balancers/proxies: 60 s) so the client closes them.
server.keepAliveTimeout = httpServer.KEEPALIVE_TIMEOUT;
Comment thread
sourcery-ai[bot] marked this conversation as resolved.
Resolved
server.headersTimeout = httpServer.KEEPALIVE_TIMEOUT + 1000;

eventManager.init(server);

const sentryConfig = configService.get<SentryConfig>('SENTRY');
Expand Down