Skip to content

Commit dbd5935

Browse files
authored
fix(proxy): configure proxy agent connection limits and IPv4 support (#2303)
* fix: configure axios proxy agent socket limits to prevent connection leaks Add socket pool configuration to HttpProxyAgent and HttpsProxyAgent to prevent connection leaks. fix #2297 * fix(proxy): pass forceIpv4First option to custom proxy agent * fix(proxy): add connection limits and IPv4 support to undici agents
1 parent bb2120c commit dbd5935

File tree

2 files changed

+23
-8
lines changed

2 files changed

+23
-8
lines changed

server/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,10 @@ app
9797

9898
// Register HTTP proxy
9999
if (settings.network.proxy.enabled) {
100-
await createCustomProxyAgent(settings.network.proxy);
100+
await createCustomProxyAgent(
101+
settings.network.proxy,
102+
settings.network.forceIpv4First
103+
);
101104
}
102105

103106
// Migrate library types

server/utils/customProxyAgent.ts

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,14 @@ export let requestInterceptorFunction: (
1111
) => InternalAxiosRequestConfig;
1212

1313
export default async function createCustomProxyAgent(
14-
proxySettings: ProxySettings
14+
proxySettings: ProxySettings,
15+
forceIpv4First?: boolean
1516
) {
16-
const defaultAgent = new Agent({ keepAliveTimeout: 5000 });
17+
const defaultAgent = new Agent({
18+
keepAliveTimeout: 5000,
19+
connections: 50,
20+
connect: forceIpv4First ? { family: 4 } : undefined,
21+
});
1722

1823
const skipUrl = (url: string | URL) => {
1924
const hostname =
@@ -67,16 +72,23 @@ export default async function createCustomProxyAgent(
6772
uri: proxyUrl,
6873
token,
6974
keepAliveTimeout: 5000,
75+
connections: 50,
76+
connect: forceIpv4First ? { family: 4 } : undefined,
7077
});
7178

7279
setGlobalDispatcher(proxyAgent.compose(noProxyInterceptor));
7380

74-
axios.defaults.httpAgent = new HttpProxyAgent(proxyUrl, {
81+
const agentOptions = {
7582
headers: token ? { 'proxy-authorization': token } : undefined,
76-
});
77-
axios.defaults.httpsAgent = new HttpsProxyAgent(proxyUrl, {
78-
headers: token ? { 'proxy-authorization': token } : undefined,
79-
});
83+
keepAlive: true,
84+
maxSockets: 50,
85+
maxFreeSockets: 10,
86+
timeout: 5000,
87+
scheduling: 'lifo' as const,
88+
family: forceIpv4First ? 4 : undefined,
89+
};
90+
axios.defaults.httpAgent = new HttpProxyAgent(proxyUrl, agentOptions);
91+
axios.defaults.httpsAgent = new HttpsProxyAgent(proxyUrl, agentOptions);
8092

8193
requestInterceptorFunction = (config) => {
8294
const url = config.baseURL

0 commit comments

Comments
 (0)