Skip to content

Commit 02e5e04

Browse files
committed
feat: Enhance HTTP agent with agentkeepalive-inspired optimizations
Improved native Node.js HTTP/HTTPS agent configuration with: - Disabled Nagle's algorithm (setNoDelay) for lower latency - LIFO socket scheduling to reuse warm connections - Free socket timeout (15s) to prevent connection leaks - Configurable socket pool limits (256 max sockets) - Better keepalive management with timeoutMillis
1 parent c424e90 commit 02e5e04

File tree

1 file changed

+25
-6
lines changed

1 file changed

+25
-6
lines changed

src/http_client.ts

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,18 +54,37 @@ export class HttpClient {
5454

5555
if (isNode()) {
5656
// We want to keep sockets alive for better performance.
57-
// It's important to set the user's timeout also here and not only
58-
// on the axios instance, because even though this timeout
59-
// is for inactive sockets, sometimes the platform would take
60-
// long to process requests and the socket would time-out
61-
// while waiting for the response.
62-
const agentOptions: http.AgentOptions = {
57+
// Enhanced agent configuration based on agentkeepalive best practices:
58+
// - Nagle's algorithm disabled for lower latency
59+
// - Free socket timeout to prevent socket leaks
60+
// - LIFO scheduling to reuse recent sockets
61+
// - Socket TTL for connection freshness
62+
const agentOptions: http.AgentOptions & { scheduling?: 'lifo' | 'fifo' } = {
6363
keepAlive: true,
64+
// Timeout for inactive sockets (30 seconds)
65+
// Prevents socket leaks from idle connections
6466
timeout: this.timeoutMillis,
67+
// Keep alive timeout for free sockets (15 seconds)
68+
// Node.js will close unused sockets after this period
69+
keepAliveMsecs: 15_000,
70+
// Maximum number of sockets per host
71+
maxSockets: 256,
72+
maxFreeSockets: 256,
73+
// LIFO scheduling - reuse most recently used sockets for better performance
74+
scheduling: 'lifo',
6575
};
6676

6777
this.httpAgent = new http.Agent(agentOptions);
6878
this.httpsAgent = new https.Agent(agentOptions);
79+
80+
// Disable Nagle's algorithm for lower latency
81+
// This sends data immediately instead of buffering small packets
82+
const setNoDelay = (socket: any) => {
83+
socket.setNoDelay(true);
84+
};
85+
86+
this.httpAgent.on('socket', setNoDelay);
87+
this.httpsAgent.on('socket', setNoDelay);
6988
}
7089

7190
this.axios = axios.create({

0 commit comments

Comments
 (0)