mirror of
https://github.com/public-transport/db-vendo-client.git
synced 2025-02-23 07:09:35 +02:00
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:
parent
8645661cbc
commit
2fd06941b5
1 changed files with 16 additions and 3 deletions
|
@ -24,8 +24,17 @@ if (proxyAddress && localAddresses) {
|
||||||
console.error('Both env vars HTTPS_PROXY/HTTP_PROXY and LOCAL_ADDRESS are not supported.')
|
console.error('Both env vars HTTPS_PROXY/HTTP_PROXY and LOCAL_ADDRESS are not supported.')
|
||||||
process.exit(1)
|
process.exit(1)
|
||||||
}
|
}
|
||||||
let getAgent = () => null
|
|
||||||
|
const plainAgent = new HttpsAgent({
|
||||||
|
keepAlive: true,
|
||||||
|
})
|
||||||
|
let getAgent = () => plainAgent
|
||||||
|
|
||||||
if (proxyAddress) {
|
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)
|
const agent = new ProxyAgent(proxyAddress)
|
||||||
getAgent = () => agent
|
getAgent = () => agent
|
||||||
} else if (localAddresses) {
|
} else if (localAddresses) {
|
||||||
|
@ -33,7 +42,10 @@ if (proxyAddress) {
|
||||||
.map((addr) => {
|
.map((addr) => {
|
||||||
const family = isIP(addr)
|
const family = isIP(addr)
|
||||||
if (family === 0) throw new Error('invalid local address:' + 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)
|
const pool = roundRobin(agents)
|
||||||
getAgent = () => pool.get()
|
getAgent = () => pool.get()
|
||||||
|
@ -74,7 +86,8 @@ const request = (ctx, userAgent, reqData) => {
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
'Accept-Encoding': 'gzip, br, deflate',
|
'Accept-Encoding': 'gzip, br, deflate',
|
||||||
'Accept': 'application/json',
|
'Accept': 'application/json',
|
||||||
'user-agent': randomizeUserAgent(userAgent)
|
'user-agent': randomizeUserAgent(userAgent),
|
||||||
|
'connection': 'keep-alive', // prevent excessive re-connecting
|
||||||
},
|
},
|
||||||
redirect: 'follow',
|
redirect: 'follow',
|
||||||
query: {}
|
query: {}
|
||||||
|
|
Loading…
Add table
Reference in a new issue