Skip to content

Commit c59c70b

Browse files
authored
feat(node): allow keepAlive override (#6161)
We want to reduce the network pressure during profiling and reuse the connection if possible so that we dont block test execution when profiling tests. This change allows us to leverage http connection keepalive by overriding the default SDK value (false). Since the tests that we profile run on node 18, we should not see any memory leaks and it is probably a good practice to allow an override here anyways.
1 parent ccb3ad6 commit c59c70b

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

packages/node/src/transports/http.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ export interface NodeTransportOptions extends BaseTransportOptions {
2323
caCerts?: string | Buffer | Array<string | Buffer>;
2424
/** Custom HTTP module. Defaults to the native 'http' and 'https' modules. */
2525
httpModule?: HTTPModule;
26+
/** Allow overriding connection keepAlive, defaults to false */
27+
keepAlive?: boolean;
2628
}
2729

2830
// Estimated maximum size for reasonable standalone event
@@ -56,12 +58,13 @@ export function makeNodeTransport(options: NodeTransportOptions): Transport {
5658
);
5759

5860
const nativeHttpModule = isHttps ? https : http;
61+
const keepAlive = options.keepAlive === undefined ? false : options.keepAlive;
5962

6063
// TODO(v7): Evaluate if we can set keepAlive to true. This would involve testing for memory leaks in older node
6164
// versions(>= 8) as they had memory leaks when using it: #2555
6265
const agent = proxy
6366
? (new (require('https-proxy-agent'))(proxy) as http.Agent)
64-
: new nativeHttpModule.Agent({ keepAlive: false, maxSockets: 30, timeout: 2000 });
67+
: new nativeHttpModule.Agent({ keepAlive, maxSockets: 30, timeout: 2000 });
6568

6669
const requestExecutor = createRequestExecutor(options, options.httpModule ?? nativeHttpModule, agent);
6770
return createTransport(options, requestExecutor);

packages/node/test/transports/http.test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,20 @@ describe('makeNewHttpTransport()', () => {
108108
await transport.send(EVENT_ENVELOPE);
109109
});
110110

111+
it('allows overriding keepAlive', async () => {
112+
await setupTestServer({ statusCode: SUCCESS }, req => {
113+
expect(req.headers).toEqual(
114+
expect.objectContaining({
115+
// node http module lower-cases incoming headers
116+
connection: 'keep-alive',
117+
}),
118+
);
119+
});
120+
121+
const transport = makeNodeTransport({ keepAlive: true, ...defaultOptions });
122+
await transport.send(EVENT_ENVELOPE);
123+
});
124+
111125
it('should correctly send user-provided headers to server', async () => {
112126
await setupTestServer({ statusCode: SUCCESS }, req => {
113127
expect(req.headers).toEqual(

0 commit comments

Comments
 (0)