diff --git a/lib/utils/http.js b/lib/utils/http.js index d668644..78a177f 100644 --- a/lib/utils/http.js +++ b/lib/utils/http.js @@ -1,6 +1,8 @@ import assert from "assert"; import axios from "axios"; import config from "exp-config"; +import http from "http"; +import https from "https"; import { debugMeta, logger } from "lu-logger"; import util from "util"; @@ -9,6 +11,11 @@ import getUrl from "./get-url.js"; const backends = buildBackends(); +const httpAgent = new http.Agent({ keepAlive: false }); +const httpsAgent = new https.Agent({ keepAlive: false }); +const httpAgentKeepAlive = new http.Agent({ keepAlive: true }); +const httpsAgentKeepAlive = new https.Agent({ keepAlive: true }); + async function performRequest(method, params) { assert(config.appName, "appName must be set in config"); @@ -46,6 +53,14 @@ async function performRequest(method, params) { opts.paramsSerializer = params.paramsSerializer; } + if (params.keepAlive) { + opts.httpAgent = httpAgentKeepAlive; + opts.httpsAgent = httpsAgentKeepAlive; + } else { + opts.httpAgent = httpAgent; + opts.httpsAgent = httpsAgent; + } + let response; try { response = await axios(url, opts);