From f6e9e5620c0edb7bfbdb32fca54debd5ea5b366d Mon Sep 17 00:00:00 2001 From: Samuel Bodin Date: Mon, 19 Oct 2020 17:00:33 +0200 Subject: [PATCH] fix(node): reuse http agent across client (#1216) --- .../requester-node-http/src/createNodeHttpRequester.ts | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/packages/requester-node-http/src/createNodeHttpRequester.ts b/packages/requester-node-http/src/createNodeHttpRequester.ts index fa9b9c876..c6bb94c03 100644 --- a/packages/requester-node-http/src/createNodeHttpRequester.ts +++ b/packages/requester-node-http/src/createNodeHttpRequester.ts @@ -12,14 +12,17 @@ export type NodeHttpRequesterOptions = { httpsAgent?: https.Agent; }; +const agentOptions = { keepAlive: true }; +const defaultHttpAgent = new http.Agent(agentOptions); +const defaultHttpsAgent = new https.Agent(agentOptions); + export function createNodeHttpRequester({ agent: userGlobalAgent, httpAgent: userHttpAgent, httpsAgent: userHttpsAgent, }: NodeHttpRequesterOptions = {}): Requester & Destroyable { - const agentOptions = { keepAlive: true }; - const httpAgent = userHttpAgent || userGlobalAgent || new http.Agent(agentOptions); - const httpsAgent = userHttpsAgent || userGlobalAgent || new https.Agent(agentOptions); + const httpAgent = userHttpAgent || userGlobalAgent || defaultHttpAgent; + const httpsAgent = userHttpsAgent || userGlobalAgent || defaultHttpsAgent; return { send(request: Request): Readonly> {