Skip to content

Commit

Permalink
Don't enable HTTP keepalive by default
Browse files Browse the repository at this point in the history
HTTP keepalive was enabled by default in Node 20, and is apparently causing errors for axios. See e.g. nodejs/node#47130, axios/axios#6113
  • Loading branch information
MattiasOlla committed Jul 29, 2024
1 parent 3dd07c9 commit ae75f39
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions lib/utils/http.js
Original file line number Diff line number Diff line change
@@ -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";

Expand All @@ -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");

Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit ae75f39

Please sign in to comment.