http.Agent keep-alive pooling maxTotalSockets change in Node.js 18+
0 reputation · 13 Nov 2020, 06:18 UTC
0 reputation · 13 Nov 2020, 06:18 UTC
Moving an HTTP client that relies on Node.js http.Agent keep-alive pooling across a Node.js major version boundary raises a configuration decision about socket limits.
The affected feature is keep-alive socket reuse governed by maxSockets per origin and, in newer releases, maxTotalSockets for the default agent. agent.destroy() does not guarantee immediate closure of sockets attached to in-flight requests, and socket.setTimeout emits 'timeout' without destroying the socket, so stalled keep-alive connections can remain open.
With keepAlive disabled, each request can open a new socket, while with keepAlive enabled reuse depends on pool size and idle timeout behavior. The interaction between per-host and total limits is version-sensitive and affects how bursts are handled.
What is the effective concurrent socket cap for the default agent in Node.js 18+ when maxSockets is unspecified? Does keepAlive false prevent socket reuse and increase creation rate under bursts? How does maxTotalSockets interact with per-host maxSockets when multiple hosts share the default agent?
35275 reputation · 13 Nov 2020, 10:53 UTC
When maxSockets is left unspecified (defaults to Infinity) on the default http.globalAgent in Node.js 18+, the agent is limited by maxTotalSockets, which defaults to 50. Thus the effective concurrent socket cap is 50 sockets across all origins.
http.globalAgent.maxSockets defaults to Infinity in Node 18+.http.globalAgent.maxTotalSockets defaults to 50.keepAlive: false only omits the HTTP Keep‑Alive header; the agent may still hold sockets briefly for pipelining, but it does not reuse them across requests.maxTotalSockets limit; excess requests queue until a socket becomes free.maxTotalSockets is a hard global ceiling. If multiple hosts share the default agent, the sum of active sockets for all hosts cannot exceed this value. A per‑host maxSockets (if set) acts as a secondary cap, but the global limit wins when the two conflict.node -e "console.log(require('http').globalAgent.maxSockets, require('http').globalAgent.maxTotalSockets)" to confirm the defaults.http.request calls to several hosts using the default agent and count active sockets via process._getActiveHandles().filter(h => h.constructor.name === 'Socket').length.Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.