diff --git a/clients/javascript/lib/baseClient.ts b/clients/javascript/lib/baseClient.ts index 14b42c19..b26eeb8d 100644 --- a/clients/javascript/lib/baseClient.ts +++ b/clients/javascript/lib/baseClient.ts @@ -15,8 +15,7 @@ import { sanitizeErrorData, } from './helpers/errors' import type cacheableLookupType from 'cacheable-lookup' -import type httpsType from 'node:https' -import type httpType from 'node:http' +import type * as agentkeepaliveModuleType from 'agentkeepalive' /** * Configuration for the keep alive feature @@ -40,10 +39,6 @@ export type KeepAliveConfig = { * Maximum number of free sockets to keep alive */ maxFreeSockets?: number - /** - * Keep alive milliseconds (how long to keep the connection alive) - */ - keepAliveMsecs?: number } /** @@ -419,13 +414,15 @@ export const createAxiosInstanceBackend = async ( if (args.baseUrl.startsWith('https')) { // Import the module here to avoid loading this module in the browser - const https: typeof httpsType = require('node:https') + const agentkeepalive: typeof agentkeepaliveModuleType = require('agentkeepalive') // Default values are what we evaluated to be good for our load - const httpsAgent = new https.Agent({ + const httpsAgent = new agentkeepalive.HttpsAgent({ keepAlive: true, maxSockets: args.keepAlive.maxSockets ?? 75, maxFreeSockets: args.keepAlive.maxFreeSockets ?? 10, - keepAliveMsecs: args.keepAlive.keepAliveMsecs ?? 60000, + freeSocketTimeout: 15000, // closes idle connections after 15s + socketActiveTTL: 600000, // force recycle even active sockets after 10min + timeout: 60000, // timeout after 1min }) if (args.keepAlive.useCacheableLookupDNS) { @@ -434,13 +431,15 @@ export const createAxiosInstanceBackend = async ( config.httpsAgent = httpsAgent } else { // Import the module here to avoid loading this module in the browser - const http: typeof httpType = require('node:http') + const agentkeepalive: typeof agentkeepaliveModuleType = require('agentkeepalive') // Default values are what we evaluated to be good for our load - const httpAgent = new http.Agent({ + const httpAgent = new agentkeepalive.HttpAgent({ keepAlive: true, maxSockets: args.keepAlive.maxSockets ?? 75, maxFreeSockets: args.keepAlive.maxFreeSockets ?? 10, - keepAliveMsecs: args.keepAlive.keepAliveMsecs ?? 60000, + freeSocketTimeout: 15000, // closes idle connections after 15s + socketActiveTTL: 600000, // force recycle even active sockets after 10min + timeout: 60000, // timeout after 1min }) if (args.keepAlive.useCacheableLookupDNS) { diff --git a/clients/javascript/package.json b/clients/javascript/package.json index aeb06aab..27e29e24 100644 --- a/clients/javascript/package.json +++ b/clients/javascript/package.json @@ -41,6 +41,7 @@ "author": "", "license": "MIT", "dependencies": { + "agentkeepalive": "4.6.0", "axios": "1.12.2", "axios-retry": "4.5.0", "cacheable-lookup": "^6.1.0", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index d2d07d9b..b5604c52 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -14,6 +14,9 @@ importers: clients/javascript: dependencies: + agentkeepalive: + specifier: 4.6.0 + version: 4.6.0 axios: specifier: 1.12.2 version: 1.12.2 @@ -892,6 +895,10 @@ packages: engines: {node: '>=0.4.0'} hasBin: true + agentkeepalive@4.6.0: + resolution: {integrity: sha512-kja8j7PjmncONqaTsB8fQ+wE2mSU2DJ9D4XKoJ5PFWIdRMa6SLSN1ff4mOr4jCbfRSsxR4keIiySJU0N9T5hIQ==} + engines: {node: '>= 8.0.0'} + ansi-escapes@4.3.2: resolution: {integrity: sha512-gKXj5ALrKWQLsYG9jlTRmR/xKluxHV+Z9QEwNIgCfM1/uwPMCuzVVnh5mwTd+OuBZcwSIMbqssNWRm1lE51QaQ==} engines: {node: '>=8'} @@ -1329,6 +1336,9 @@ packages: resolution: {integrity: sha512-B4FFZ6q/T2jhhksgkbEW3HBvWIfDW85snkQgawt07S7J5QXTk6BkNV+0yAeZrM5QpMAdYlocGoljn0sJ/WQkFw==} engines: {node: '>=10.17.0'} + humanize-ms@1.2.1: + resolution: {integrity: sha512-Fl70vYtsAFb/C06PTS9dZBo7ihau+Tu/DNCk/OyHhea07S+aeMWpFFkUaXRa8fI+ScZbEI8dfSxwY7gxZ9SAVQ==} + import-local@3.2.0: resolution: {integrity: sha512-2SPlun1JUPWoM6t3F0dw0FkCF/jWY8kttcY4f599GLTSjh2OCuuhdTkJQsEcZzBqbXZGKMK2OqW1oZsjtf/gQA==} engines: {node: '>=8'} @@ -3002,6 +3012,10 @@ snapshots: acorn@8.15.0: {} + agentkeepalive@4.6.0: + dependencies: + humanize-ms: 1.2.1 + ansi-escapes@4.3.2: dependencies: type-fest: 0.21.3 @@ -3456,6 +3470,10 @@ snapshots: human-signals@2.1.0: {} + humanize-ms@1.2.1: + dependencies: + ms: 2.1.3 + import-local@3.2.0: dependencies: pkg-dir: 4.2.0