diff --git a/lib/request.js b/lib/request.js index 756cb8fc..d8ff7dce 100644 --- a/lib/request.js +++ b/lib/request.js @@ -24,8 +24,17 @@ if (proxyAddress && localAddresses) { console.error('Both env vars HTTPS_PROXY/HTTP_PROXY and LOCAL_ADDRESS are not supported.') process.exit(1) } -let getAgent = () => null + +const plainAgent = new HttpsAgent({ + keepAlive: true, +}) +let getAgent = () => plainAgent + if (proxyAddress) { + // todo: this doesn't honor `keepAlive: true` + // related: + // - https://github.com/TooTallNate/node-https-proxy-agent/pull/112 + // - https://github.com/TooTallNate/node-agent-base/issues/5 const agent = new ProxyAgent(proxyAddress) getAgent = () => agent } else if (localAddresses) { @@ -33,7 +42,10 @@ if (proxyAddress) { .map((addr) => { const family = isIP(addr) if (family === 0) throw new Error('invalid local address:' + addr) - return new HttpsAgent({localAddress: addr, family}) + return new HttpsAgent({ + localAddress: addr, family, + keepAlive: true, + }) }) const pool = roundRobin(agents) getAgent = () => pool.get() @@ -74,7 +86,8 @@ const request = (ctx, userAgent, reqData) => { 'Content-Type': 'application/json', 'Accept-Encoding': 'gzip, br, deflate', 'Accept': 'application/json', - 'user-agent': randomizeUserAgent(userAgent) + 'user-agent': randomizeUserAgent(userAgent), + 'connection': 'keep-alive', // prevent excessive re-connecting }, redirect: 'follow', query: {}