use HTTP keep-alive

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
This commit is contained in:
Jannis R 2021-12-09 18:53:18 +01:00
parent 8645661cbc
commit 2fd06941b5
No known key found for this signature in database
GPG key ID: 0FE83946296A88A5

View file

@ -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: {}