From 2fd06941b5846546590ae8cefec17bfc5b0b1efb Mon Sep 17 00:00:00 2001 From: Jannis R Date: Thu, 9 Dec 2021 18:53:18 +0100 Subject: [PATCH] =?UTF-8?q?use=20HTTP=20keep-alive=20=E2=9A=A1=EF=B8=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1000 locations() requests against DB endpoint, before: min max sum mean stddev 215ms 4351ms 329s 329ms 341ms after: min max sum mean stddev 160ms 579ms 194s 194ms 37ms --- lib/request.js | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) 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: {}