Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 1 addition & 22 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@
"@apify/log": "^2.2.6",
"@apify/utilities": "^2.23.2",
"@crawlee/types": "^3.3.0",
"agentkeepalive": "^4.2.1",
"ansi-colors": "^4.1.1",
"async-retry": "^1.3.3",
"axios": "^1.6.7",
Expand Down
44 changes: 33 additions & 11 deletions src/http_client.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import http from 'node:http';
import https from 'node:https';
Comment on lines +1 to +2
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please add both these modules as externals to rsbuild.config.ts (link)? They are not used outside of Node, so we don't need them in the browser bundle. Together, they add around 250kB to the bundle size.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure thing, done in commit f986aa4

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

import os from 'node:os';

import KeepAliveAgent from 'agentkeepalive';
import type { RetryFunction } from 'async-retry';
import retry from 'async-retry';
import type { AxiosError, AxiosInstance, AxiosRequestConfig, AxiosResponse, InternalAxiosRequestConfig } from 'axios';
Expand Down Expand Up @@ -32,9 +33,9 @@ export class HttpClient {

timeoutMillis: number;

httpAgent?: KeepAliveAgent;
httpAgent?: http.Agent;

httpsAgent?: KeepAliveAgent.HttpsAgent;
httpsAgent?: https.Agent;

axios: AxiosInstance;

Expand All @@ -53,16 +54,37 @@ export class HttpClient {

if (isNode()) {
// We want to keep sockets alive for better performance.
// It's important to set the user's timeout also here and not only
// on the axios instance, because even though this timeout
// is for inactive sockets, sometimes the platform would take
// long to process requests and the socket would time-out
// while waiting for the response.
const agentOpts = {
// Enhanced agent configuration based on agentkeepalive best practices:
// - Nagle's algorithm disabled for lower latency
// - Free socket timeout to prevent socket leaks
// - LIFO scheduling to reuse recent sockets
// - Socket TTL for connection freshness
const agentOptions: http.AgentOptions & { scheduling?: 'lifo' | 'fifo' } = {
keepAlive: true,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not the only thing that agentkeepalive adds.

Image

...is it safe to ignore the rest of the changes they mention?

Copy link
Contributor Author

@tducret tducret Nov 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, I've just added commit c424e90 with these optimizations.

// Timeout for inactive sockets
// Prevents socket leaks from idle connections
timeout: this.timeoutMillis,
// Keep alive timeout for free sockets (15 seconds)
// Node.js will close unused sockets after this period
keepAliveMsecs: 15_000,
// Maximum number of sockets per host
maxSockets: 256,
maxFreeSockets: 256,
// LIFO scheduling - reuse most recently used sockets for better performance
scheduling: 'lifo',
};
this.httpAgent = new KeepAliveAgent(agentOpts);
this.httpsAgent = new KeepAliveAgent.HttpsAgent(agentOpts);

this.httpAgent = new http.Agent(agentOptions);
this.httpsAgent = new https.Agent(agentOptions);

// Disable Nagle's algorithm for lower latency
// This sends data immediately instead of buffering small packets
const setNoDelay = (socket: any) => {
socket.setNoDelay(true);
};

this.httpAgent.on('socket', setNoDelay);
this.httpsAgent.on('socket', setNoDelay);
}

this.axios = axios.create({
Expand Down
Loading