Skip to content

Commit fd8d112

Browse files
authored
feat(backend): use global connection pool (#376)
1 parent fac3bc6 commit fd8d112

File tree

19 files changed

+221
-164
lines changed

19 files changed

+221
-164
lines changed

apps/cli/src/server/sync.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { DifferV3 } from '@api7/adc-differ';
22
import * as ADCSDK from '@api7/adc-sdk';
3+
import { HttpAgent, HttpOptions, HttpsAgent } from 'agentkeepalive';
34
import type { RequestHandler } from 'express';
45
import { omit, toString } from 'lodash';
56
import { lastValueFrom, toArray } from 'rxjs';
@@ -14,6 +15,25 @@ import { check } from '../linter';
1415
import { logger } from './logger';
1516
import { SyncInput, type SyncInputType } from './schema';
1617

18+
// create connection pool
19+
const keepAlive: HttpOptions = {
20+
keepAlive: true,
21+
maxSockets: 256, // per host
22+
maxFreeSockets: 16, // per host free
23+
freeSocketTimeout: 60000,
24+
};
25+
const httpAgent = new HttpAgent(keepAlive);
26+
27+
//TODO: dynamic rejectUnauthorized and support mTLS
28+
const httpsAgent = new HttpsAgent({
29+
rejectUnauthorized: true,
30+
...keepAlive,
31+
});
32+
const httpsInsecureAgent = new HttpsAgent({
33+
rejectUnauthorized: false,
34+
...keepAlive,
35+
});
36+
1737
export const syncHandler: RequestHandler<
1838
unknown,
1939
unknown,
@@ -46,12 +66,13 @@ export const syncHandler: RequestHandler<
4666
fillLabels(local, task.opts.labelSelector);
4767

4868
// load and filter remote configuration
49-
//TODO: merged with the listr task
5069
const backend = loadBackend(task.opts.backend, {
5170
...task.opts,
5271
server: Array.isArray(task.opts.server)
5372
? task.opts.server.join(',')
5473
: task.opts.server,
74+
httpAgent,
75+
httpsAgent: task.opts.tlsSkipVerify ? httpsInsecureAgent : httpsAgent,
5576
});
5677

5778
backend.on('AXIOS_DEBUG', ({ description, response }) =>

apps/cli/src/tasks/init_backend.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import * as ADCSDK from '@api7/adc-sdk';
2+
import { HttpAgent, HttpOptions, HttpsAgent } from 'agentkeepalive';
23
import { ListrTask } from 'listr2';
4+
import { readFileSync } from 'node:fs';
35

46
import { loadBackend } from '../command/utils';
57

@@ -8,6 +10,28 @@ export const InitializeBackendTask = (
810
opts: ADCSDK.BackendOptions,
911
): ListrTask => ({
1012
task: async (ctx) => {
11-
ctx.backend = loadBackend(type, { ...opts, cacheKey: 'default' });
13+
const keepAlive: HttpOptions = {
14+
keepAlive: true,
15+
maxSockets: 256, // per host
16+
maxFreeSockets: 16, // per host free
17+
freeSocketTimeout: 60000,
18+
};
19+
20+
ctx.backend = loadBackend(type, {
21+
...opts,
22+
cacheKey: 'default',
23+
httpAgent: new HttpAgent(keepAlive),
24+
httpsAgent: new HttpsAgent({
25+
rejectUnauthorized: !opts?.tlsSkipVerify,
26+
...keepAlive,
27+
...(opts?.caCertFile ? { ca: readFileSync(opts.caCertFile) } : {}),
28+
...(opts?.tlsClientCertFile
29+
? { cert: readFileSync(opts.tlsClientCertFile) }
30+
: {}),
31+
...(opts?.tlsClientKeyFile
32+
? { key: readFileSync(opts.tlsClientKeyFile) }
33+
: {}),
34+
}),
35+
});
1236
},
1337
});

libs/backend-api7/e2e/default-value.e2e-spec.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
import { globalAgent as httpAgent } from 'node:http';
12
import { gte, lt } from 'semver';
23

34
import { BackendAPI7 } from '../src';
45
import {
56
conditionalIt,
7+
generateHTTPSAgent,
68
getDefaultValue,
79
semverCondition,
810
} from './support/utils';
@@ -12,10 +14,13 @@ describe('Default Value', () => {
1214

1315
beforeAll(() => {
1416
backend = new BackendAPI7({
15-
server: process.env.SERVER,
16-
token: process.env.TOKEN,
17+
server: process.env.SERVER!,
18+
token: process.env.TOKEN!,
1719
tlsSkipVerify: true,
1820
gatewayGroup: process.env.GATEWAY_GROUP,
21+
cacheKey: 'default',
22+
httpAgent,
23+
httpsAgent: generateHTTPSAgent(),
1924
});
2025
});
2126

libs/backend-api7/e2e/label-selector.e2e-spec.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as ADCSDK from '@api7/adc-sdk';
22
import { unset } from 'lodash';
33
import { readFileSync } from 'node:fs';
4+
import { globalAgent as httpAgent } from 'node:http';
45
import { join } from 'node:path';
56
import { gte, lt } from 'semver';
67

@@ -10,16 +11,20 @@ import {
1011
createEvent,
1112
deleteEvent,
1213
dumpConfiguration,
14+
generateHTTPSAgent,
1315
semverCondition,
1416
syncEvents,
1517
} from './support/utils';
1618

1719
describe('Label Selector', () => {
1820
const commonBackendOpts = {
19-
server: process.env.SERVER,
20-
token: process.env.TOKEN,
21+
server: process.env.SERVER!,
22+
token: process.env.TOKEN!,
2123
tlsSkipVerify: true,
2224
gatewayGroup: process.env.GATEWAY_GROUP,
25+
cacheKey: 'default',
26+
httpAgent,
27+
httpsAgent: generateHTTPSAgent(),
2328
};
2429
let backend: BackendAPI7;
2530

libs/backend-api7/e2e/misc.e2e-spec.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import * as ADCSDK from '@api7/adc-sdk';
22
import { toString } from 'lodash';
3+
import { globalAgent as httpAgent } from 'node:http';
34

45
import { BackendAPI7 } from '../src';
56
import {
67
createEvent,
78
deleteEvent,
89
dumpConfiguration,
10+
generateHTTPSAgent,
911
overrideEventResourceId,
1012
syncEvents,
1113
} from './support/utils';
@@ -15,10 +17,13 @@ describe('Miscellaneous', () => {
1517

1618
beforeAll(() => {
1719
backend = new BackendAPI7({
18-
server: process.env.SERVER,
19-
token: process.env.TOKEN,
20+
server: process.env.SERVER!,
21+
token: process.env.TOKEN!,
2022
tlsSkipVerify: true,
2123
gatewayGroup: process.env.GATEWAY_GROUP,
24+
cacheKey: 'default',
25+
httpAgent,
26+
httpsAgent: generateHTTPSAgent(),
2227
});
2328
});
2429

libs/backend-api7/e2e/ping.e2e-spec.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
1+
import { globalAgent as httpAgent } from 'node:http';
2+
import { globalAgent as httpsAgent } from 'node:https';
3+
14
import { BackendAPI7 } from '../src';
5+
import { generateHTTPSAgent } from './support/utils';
26

37
describe('Ping', () => {
48
it('should success', async () => {
59
const backend = new BackendAPI7({
6-
server: process.env.SERVER,
7-
token: process.env.TOKEN,
10+
server: process.env.SERVER!,
11+
token: process.env.TOKEN!,
812
tlsSkipVerify: true,
13+
cacheKey: 'default',
14+
httpAgent,
15+
httpsAgent: generateHTTPSAgent(),
916
});
1017
await backend.ping();
1118
});
@@ -14,6 +21,9 @@ describe('Ping', () => {
1421
const backend = new BackendAPI7({
1522
server: 'http://0.0.0.0',
1623
token: '',
24+
cacheKey: 'default',
25+
httpAgent,
26+
httpsAgent,
1727
});
1828
await expect(backend.ping()).rejects.toThrow(
1929
'connect ECONNREFUSED 0.0.0.0:80',
@@ -22,8 +32,11 @@ describe('Ping', () => {
2232

2333
it('should failed (self-signed certificate)', async () => {
2434
const backend = new BackendAPI7({
25-
server: process.env.SERVER,
26-
token: process.env.TOKEN,
35+
server: process.env.SERVER!,
36+
token: process.env.TOKEN!,
37+
cacheKey: 'default',
38+
httpAgent,
39+
httpsAgent,
2740
});
2841
await expect(backend.ping()).rejects.toThrow('self-signed certificate');
2942
});

libs/backend-api7/e2e/resources/consumer.e2e-spec.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as ADCSDK from '@api7/adc-sdk';
2+
import { globalAgent as httpAgent } from 'node:http';
23
import { gte, lt } from 'semver';
34

45
import { BackendAPI7 } from '../../src';
@@ -7,6 +8,7 @@ import {
78
createEvent,
89
deleteEvent,
910
dumpConfiguration,
11+
generateHTTPSAgent,
1012
syncEvents,
1113
updateEvent,
1214
} from '../support/utils';
@@ -16,10 +18,13 @@ describe('Consumer E2E', () => {
1618

1719
beforeAll(() => {
1820
backend = new BackendAPI7({
19-
server: process.env.SERVER,
20-
token: process.env.TOKEN,
21+
server: process.env.SERVER!,
22+
token: process.env.TOKEN!,
2123
tlsSkipVerify: true,
2224
gatewayGroup: process.env.GATEWAY_GROUP,
25+
cacheKey: 'default',
26+
httpAgent,
27+
httpsAgent: generateHTTPSAgent(),
2328
});
2429
});
2530

libs/backend-api7/e2e/resources/route.e2e-spec.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as ADCSDK from '@api7/adc-sdk';
2+
import { globalAgent as httpAgent } from 'node:http';
23
import { gte } from 'semver';
34

45
import { BackendAPI7 } from '../../src';
@@ -7,6 +8,7 @@ import {
78
createEvent,
89
deleteEvent,
910
dumpConfiguration,
11+
generateHTTPSAgent,
1012
semverCondition,
1113
syncEvents,
1214
} from '../support/utils';
@@ -16,10 +18,13 @@ describe('Route E2E', () => {
1618

1719
beforeAll(() => {
1820
backend = new BackendAPI7({
19-
server: process.env.SERVER,
20-
token: process.env.TOKEN,
21+
server: process.env.SERVER!,
22+
token: process.env.TOKEN!,
2123
tlsSkipVerify: true,
2224
gatewayGroup: process.env.GATEWAY_GROUP,
25+
cacheKey: 'default',
26+
httpAgent,
27+
httpsAgent: generateHTTPSAgent(),
2328
});
2429
});
2530

libs/backend-api7/e2e/resources/service-upstream.e2e-spec.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import { Differ } from '@api7/adc-differ';
22
import * as ADCSDK from '@api7/adc-sdk';
33
import { unset } from 'lodash';
4+
import { globalAgent as httpAgent } from 'node:http';
45
import { gte } from 'semver';
56

67
import { BackendAPI7 } from '../../src';
78
import {
89
conditionalDescribe,
910
dumpConfiguration,
11+
generateHTTPSAgent,
1012
semverCondition,
1113
sortResult,
1214
syncEvents,
@@ -24,6 +26,8 @@ conditionalDescribe(semverCondition(gte, '3.5.0'))(
2426
tlsSkipVerify: true,
2527
gatewayGroup: process.env.GATEWAY_GROUP,
2628
cacheKey: 'e2e-service-upstream',
29+
httpAgent,
30+
httpsAgent: generateHTTPSAgent(),
2731
});
2832
});
2933

libs/backend-api7/e2e/support/utils.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as ADCSDK from '@api7/adc-sdk';
22
import { Listr, SilentRenderer } from 'listr2';
3+
import { Agent as httpsAgent } from 'node:https';
34
import { lastValueFrom, toArray } from 'rxjs';
45
import * as semver from 'semver';
56

@@ -109,6 +110,12 @@ export const overrideEventResourceId = (
109110
export const sortResult = <T>(result: Array<T>, field: string) =>
110111
structuredClone(result).sort((a, b) => a[field].localeCompare(b[field]));
111112

113+
export const generateHTTPSAgent = () => {
114+
return new httpsAgent({
115+
rejectUnauthorized: false,
116+
});
117+
};
118+
112119
type cond = boolean | (() => boolean);
113120

114121
export const conditionalDescribe = (cond: cond) =>

0 commit comments

Comments
 (0)