Skip to content

Commit 16eae4b

Browse files
committed
feat: re-use sockets when sending data to kubernetes upstream
1 parent 76ee627 commit 16eae4b

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

src/transmitter/index.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1+
import { parse } from 'url';
12
import { queue } from 'async';
23
import * as needle from 'needle';
3-
import { NeedleResponse, NeedleHttpVerbs, NeedleOptions } from 'needle';
44
import * as sleep from 'sleep-promise';
5+
import { Agent as HttpAgent } from 'http';
6+
import { Agent as HttpsAgent } from 'https';
7+
import { NeedleResponse, NeedleHttpVerbs, NeedleOptions } from 'needle';
8+
59
import { logger } from '../common/logger';
610
import { config } from '../common/config';
711
import {
@@ -28,6 +32,16 @@ interface HomebaseRequest {
2832
const upstreamUrl =
2933
config.INTEGRATION_API || config.DEFAULT_KUBERNETES_UPSTREAM_URL;
3034

35+
let agent = new HttpAgent({
36+
keepAlive: true,
37+
});
38+
39+
if (parse(upstreamUrl).protocol?.startsWith('https')) {
40+
agent = new HttpsAgent({
41+
keepAlive: true,
42+
});
43+
}
44+
3145
// Async queue wraps around the call to retryRequest in order to limit
3246
// the number of requests in flight to Homebase at any one time.
3347
const reqQueue = queue(async function (req: HomebaseRequest) {
@@ -227,7 +241,9 @@ async function retryRequest(
227241
const options: NeedleOptions = {
228242
json: true,
229243
compressed: true,
244+
agent,
230245
};
246+
231247
if (config.HTTP_PROXY || config.HTTPS_PROXY) {
232248
options.agent = getProxyAgent(config, url);
233249
}

0 commit comments

Comments
 (0)