From 8de2f80854b84839b7834fbbba996b80d42b476f Mon Sep 17 00:00:00 2001 From: gjsjohnmurray Date: Wed, 18 Sep 2024 20:36:47 +0100 Subject: [PATCH] Reuse Agent across requests --- src/api/index.ts | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/api/index.ts b/src/api/index.ts index 6879b28f..240ce898 100644 --- a/src/api/index.ts +++ b/src/api/index.ts @@ -43,6 +43,7 @@ export interface ConnectionSettings { export class AtelierAPI { private _config: ConnectionSettings; + private _agent?: httpModule.Agent | httpsModule.Agent; private namespace: string; public configName: string; @@ -303,11 +304,13 @@ export class AtelierAPI { const proto = this._config.https ? "https" : "http"; const http = this._config.https ? httpsModule : httpModule; - const agent = new http.Agent({ - keepAlive: true, - maxSockets: 10, - rejectUnauthorized: https && vscode.workspace.getConfiguration("http").get("proxyStrictSSL"), - }); + if (!this._agent) { + this._agent = new http.Agent({ + keepAlive: true, + maxSockets: 10, + rejectUnauthorized: https && vscode.workspace.getConfiguration("http").get("proxyStrictSSL"), + }); + } let pathPrefix = this._config.pathPrefix || ""; if (pathPrefix.length && !pathPrefix.startsWith("/")) { @@ -340,7 +343,7 @@ export class AtelierAPI { const cookie = await auth; const response = await fetch(`${proto}://${host}:${port}${path}`, { method, - agent, + agent: this._agent, body: body ? (typeof body !== "string" ? JSON.stringify(body) : body) : null, headers: { ...headers,