From e1b6ba85492a39ab44978071d25a35cf62e88cd1 Mon Sep 17 00:00:00 2001 From: bzp2010 Date: Tue, 25 Feb 2025 17:11:25 +0800 Subject: [PATCH] feat(backend): enable keepalive for backend api call --- libs/backend-api7/src/index.ts | 18 +++++++++++++++--- libs/backend-apisix/src/index.ts | 17 +++++++++++++++-- 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/libs/backend-api7/src/index.ts b/libs/backend-api7/src/index.ts index 58ca523..2ea3b6d 100644 --- a/libs/backend-api7/src/index.ts +++ b/libs/backend-api7/src/index.ts @@ -4,7 +4,14 @@ import { JSONSchema4 } from 'json-schema'; import { Listr, ListrTask } from 'listr2'; import { isEmpty, isNil } from 'lodash'; import { readFileSync } from 'node:fs'; -import { AgentOptions, Agent as httpsAgent } from 'node:https'; +import { + Agent as httpAgent, + AgentOptions as httpAgentOptions, +} from 'node:http'; +import { + Agent as httpsAgent, + AgentOptions as httpsAgentOptions, +} from 'node:https'; import semver, { SemVer } from 'semver'; import { Fetcher } from './fetcher'; @@ -23,17 +30,22 @@ export class BackendAPI7 implements ADCSDK.Backend { private defaultValue: ADCSDK.DefaultValue; constructor(private readonly opts: ADCSDK.BackendOptions) { + const keepAlive: httpAgentOptions = { + keepAlive: true, + keepAliveMsecs: 60000, + }; const config: CreateAxiosDefaults = { baseURL: `${opts.server}`, headers: { 'X-API-KEY': opts.token, 'Content-Type': 'application/json', - //'User-Agent': 'ADC/0.9.0-alpha.9 (ADC-API7-BACKEND)', }, + httpAgent: new httpAgent(keepAlive), }; if (opts.server.startsWith('https')) { - const agentConfig: AgentOptions = { + const agentConfig: httpsAgentOptions = { + ...keepAlive, rejectUnauthorized: !opts?.tlsSkipVerify, }; diff --git a/libs/backend-apisix/src/index.ts b/libs/backend-apisix/src/index.ts index d8a8a56..c7deee2 100644 --- a/libs/backend-apisix/src/index.ts +++ b/libs/backend-apisix/src/index.ts @@ -2,7 +2,14 @@ import * as ADCSDK from '@api7/adc-sdk'; import axios, { Axios, CreateAxiosDefaults } from 'axios'; import { Listr, ListrTask } from 'listr2'; import { readFileSync } from 'node:fs'; -import { AgentOptions, Agent as httpsAgent } from 'node:https'; +import { + Agent as httpAgent, + AgentOptions as httpAgentOptions, +} from 'node:http'; +import { + Agent as httpsAgent, + AgentOptions as httpsAgentOptions, +} from 'node:https'; import semver from 'semver'; import { Fetcher } from './fetcher'; @@ -14,16 +21,22 @@ export class BackendAPISIX implements ADCSDK.Backend { private static logScope = ['APISIX']; constructor(private readonly opts: ADCSDK.BackendOptions) { + const keepAlive: httpAgentOptions = { + keepAlive: true, + keepAliveMsecs: 60000, + }; const config: CreateAxiosDefaults = { baseURL: `${opts.server}`, headers: { 'Content-Type': 'application/json', 'X-API-KEY': opts.token, }, + httpAgent: new httpAgent(keepAlive), }; if (opts.server.startsWith('https')) { - const agentConfig: AgentOptions = { + const agentConfig: httpsAgentOptions = { + ...keepAlive, rejectUnauthorized: !opts?.tlsSkipVerify, };