From fe4a56ba59188c4ced5b95a6026bd5bed9a2b854 Mon Sep 17 00:00:00 2001 From: Jari Kolehmainen Date: Thu, 18 Nov 2021 16:21:28 +0200 Subject: [PATCH] Add keepalive option in JsonApi.getResponse (#4374) * add keepalive option in JsonApi.getResponse Signed-off-by: Jari Kolehmainen * cleanup Signed-off-by: Jari Kolehmainen --- src/common/k8s-api/json-api.ts | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/common/k8s-api/json-api.ts b/src/common/k8s-api/json-api.ts index 12bccc7a1b..5d207e13a4 100644 --- a/src/common/k8s-api/json-api.ts +++ b/src/common/k8s-api/json-api.ts @@ -21,6 +21,8 @@ // Base http-service / json-api class +import { Agent as HttpAgent } from "http"; +import { Agent as HttpsAgent } from "https"; import { merge } from "lodash"; import fetch, { Response, RequestInit } from "node-fetch"; import { stringify } from "querystring"; @@ -54,6 +56,10 @@ export interface JsonApiConfig { debug?: boolean; getRequestOptions?: () => Promise; } + +const httpAgent = new HttpAgent({ keepAlive: true }); +const httpsAgent = new HttpsAgent({ keepAlive: true }); + export class JsonApi { static reqInitDefault: RequestInit = { headers: { @@ -95,6 +101,10 @@ export class JsonApi { reqInit.method = "get"; } + if (!reqInit.agent) { + reqInit.agent = reqUrl.startsWith("https:") ? httpsAgent : httpAgent; + } + if (query) { const queryString = stringify(query);