Connection Pooling
By default, Node.js creates a new TCP connection for every outgoing HTTP request, or reuses connections from an unbounded pool. Under high load this can exhaust file descriptors or overwhelm upstream subgraphs with too many simultaneous connections.
Providing a custom http.Agent (or https.Agent) via the customAgent option lets you tune every
aspect of the connection pool — capping concurrency, reusing keep-alive connections, controlling
idle socket lifetime, and more — giving you explicit control over how Hive Gateway interacts with
your subgraphs at the network level.
Hive Gateway passes the agent to the underlying fetch call, so it works in both Node.js and Bun.
Limiting Concurrent Connections
maxSockets caps the number of concurrent TCP connections Hive Gateway will open to each upstream
origin. Requests that exceed this limit are queued internally and sent as connections become
available, which protects downstream services from being flooded during traffic spikes.
Keep-Alive Connections
By default Node.js closes connections after each request. Enabling keepAlive reuses open TCP
sockets for subsequent requests, eliminating the TCP handshake and (for HTTPS) TLS negotiation
overhead on every call.
| Option | Description |
|---|---|
keepAlive | Reuse connections for multiple requests. Defaults to false. |
keepAliveMsecs | Milliseconds between keep-alive probes when keepAlive is true. Defaults to 1000. |
maxSockets | Maximum concurrent sockets per origin. Defaults to Infinity. |
maxFreeSockets | Maximum idle keep-alive sockets to retain per origin. Defaults to 256. |
Socket Timeout
Use timeout to set a socket-level inactivity timeout (in milliseconds). Internally this calls
socket.setTimeout(), which emits a 'timeout' event on the socket after the specified duration of
inactivity — it does not automatically close the connection. This is useful for detecting
unresponsive subgraphs early, but your application is responsible for handling the event (e.g. by
destroying the socket).
Request Scheduling
The scheduling option controls which idle keep-alive socket is picked for each new request:
"fifo"(default) — picks the socket that has been idle the longest, spreading load evenly."lifo"— picks the most recently used socket, which keeps fewer sockets warm and is more cache-friendly under lower concurrency.
Using HTTPS
For subgraphs served over HTTPS, use https.Agent instead. It accepts all of the same options as
http.Agent plus TLS-specific ones:
Per-Origin Agents
The customAgent callback receives the target url, so you can apply different tuning to
different upstream origins: