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(node): allow keepAlive override #6161

Merged
merged 4 commits into from Nov 9, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 4 additions & 1 deletion packages/node/src/transports/http.ts
Expand Up @@ -23,6 +23,8 @@ export interface NodeTransportOptions extends BaseTransportOptions {
caCerts?: string | Buffer | Array<string | Buffer>;
/** Custom HTTP module. Defaults to the native 'http' and 'https' modules. */
httpModule?: HTTPModule;
/** Allow overriding connection keepAlive, defaults to false */
keepAlive?: boolean;
}

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

const nativeHttpModule = isHttps ? https : http;
const keepAlive = options.keepAlive === undefined ? false : options.keepAlive;

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

const requestExecutor = createRequestExecutor(options, options.httpModule ?? nativeHttpModule, agent);
return createTransport(options, requestExecutor);
Expand Down
14 changes: 14 additions & 0 deletions packages/node/test/transports/http.test.ts
Expand Up @@ -108,6 +108,20 @@ describe('makeNewHttpTransport()', () => {
await transport.send(EVENT_ENVELOPE);
});

it('allows overriding keepAlive', async () => {
await setupTestServer({ statusCode: SUCCESS }, req => {
expect(req.headers).toEqual(
expect.objectContaining({
// node http module lower-cases incoming headers
connection: 'keep-alive',
}),
);
});

const transport = makeNodeTransport({ keepAlive: true, ...defaultOptions });
await transport.send(EVENT_ENVELOPE);
});

it('should correctly send user-provided headers to server', async () => {
await setupTestServer({ statusCode: SUCCESS }, req => {
expect(req.headers).toEqual(
Expand Down