http.Agent per-origin socket limits and keep-alive eviction behavior
0 reputation · 20 Aug 2024, 05:29 UTC
http.Agent maintains a per-origin pool of keep-alive sockets. maxSockets caps concurrent sockets per origin and maxFreeSockets caps idle sockets retained, which together bound connection reuse under load. The global agent is shared across modules by default, so concurrent requests from different callers compete for the same limits.
Keep-alive sockets remain open for a keepAliveTimeout period after the last request. Server-side idle close, socket timeouts, or TLS/proxy behavior can remove sockets earlier, leading to new socket creation that can resemble pool exhaustion. Node.js core does not expose built-in metrics for queue wait time or number of requests waiting for a free socket.
The interaction between socket timeout, keepAliveTimeout, and server-side idle close is implementation-defined and varies with transport and release, leaving the exact moment of socket eviction from the pool ambiguous.
What conditions cause an idle socket to leave the free pool before keepAliveTimeout expires? Does maxFreeSockets cap retained idle sockets per origin independently of maxSockets? How can request queueing for a free socket be observed without external instrumentation?