INNER CODE UNIT · TypeScript
DEFAULT_SHUTDOWN_TIMEOUT
yagop/node-telegram-bot-api · src/node/server.ts:83
const DEFAULT_SHUTDOWN_TIMEOUT = 10_000; // 10s, then force-close whatever is left
/**
* Begin a non-hanging shutdown of `server`: stop accepting, drop idle keep-alive
* sockets at once (else `close` waits for them forever), and force-close anything
* still busy past `timeoutMs`. Returns the force-close timer so the caller can
* cancel it once `close` completes on its own. The connection helpers need Node
* 18.2+ and are optional-chained so a non-Node runtime is a safe no-op.
*/
export function gracefulClose(
server: http.Server,
timeoutMs: number
): ReturnType<typeof setTimeout> {
server.close(); // stop accepting; resolves once existing connections end
server.closeIdleConnections?.(); // drop idle keep-alive sockets now
const forceTimer = setTimeout(() => server.closeAllConnections?.(), timeoutMs);
forceTimer.unref?.(); // don't let the timer itself hold the loop open
return forceTimer;