Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(backend): enable keepalive for backend api call #235

Merged
merged 1 commit into from
Feb 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions libs/backend-api7/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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,
};

Expand Down
17 changes: 15 additions & 2 deletions libs/backend-apisix/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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,
};

Expand Down