Skip to content

feat(NODE-6245): add keepAliveInitialDelay config #4510

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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion src/cmap/connect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,7 @@ export const LEGAL_TLS_SOCKET_OPTIONS = [
export const LEGAL_TCP_SOCKET_OPTIONS = [
'autoSelectFamily',
'autoSelectFamilyAttemptTimeout',
'keepAliveInitialDelay',
'family',
'hints',
'localAddress',
Expand Down Expand Up @@ -376,7 +377,7 @@ export async function makeSocket(options: MakeConnectionOptions): Promise<Stream
socket = net.createConnection(parseConnectOptions(options));
}

socket.setKeepAlive(true, 300000);
socket.setKeepAlive(true, options.keepAliveInitialDelay ?? 120000);
socket.setTimeout(connectTimeoutMS);
socket.setNoDelay(noDelay);

Expand Down
3 changes: 3 additions & 0 deletions src/connection_string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -872,6 +872,9 @@ export const OPTIONS = {
return wc;
}
},
keepAliveInitialDelay: {
type: 'uint'
},
loadBalanced: {
default: false,
type: 'boolean'
Expand Down
7 changes: 6 additions & 1 deletion src/mongo_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,12 @@ export type SupportedTLSSocketOptions = Pick<

/** @public */
export type SupportedSocketOptions = Pick<
TcpNetConnectOpts & { autoSelectFamily?: boolean; autoSelectFamilyAttemptTimeout?: number },
TcpNetConnectOpts & {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We also have AC to add API docs and mongo manual docs for the new option - is that something we want to revisit?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll update the API docs. Mongo manual I flagged on the ticket with doc changes needed.

autoSelectFamily?: boolean;
autoSelectFamilyAttemptTimeout?: number;
/** Node.JS socket option to set the time the first keepalive probe is sent on an idle socket. */
keepAliveInitialDelay?: number;
},
(typeof LEGAL_TCP_SOCKET_OPTIONS)[number]
>;

Expand Down
77 changes: 77 additions & 0 deletions test/integration/node-specific/mongo_client.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { expect } from 'chai';
import { once } from 'events';
import * as net from 'net';
import { Socket } from 'net';
import * as sinon from 'sinon';

import {
Expand Down Expand Up @@ -135,6 +136,82 @@ describe('class MongoClient', function () {
expect(error).to.be.instanceOf(MongoServerSelectionError);
});

describe('#connect', function () {
context('when keepAliveInitialDelay is provided', function () {
context('when the value is 0', function () {
const options = { keepAliveInitialDelay: 0 };
let client;
let spy;

beforeEach(async function () {
spy = sinon.spy(Socket.prototype, 'setKeepAlive');
client = this.configuration.newClient(options);
await client.connect();
});

afterEach(async function () {
await client?.close();
spy.restore();
});

it('passes through the option', function () {
expect(spy).to.have.been.calledWith(true, 0);
});
});

context('when the value is positive', function () {
const options = { keepAliveInitialDelay: 100 };
let client;
let spy;

beforeEach(async function () {
spy = sinon.spy(Socket.prototype, 'setKeepAlive');
client = this.configuration.newClient(options);
await client.connect();
});

afterEach(async function () {
await client?.close();
spy.restore();
});

it('passes through the option', function () {
expect(spy).to.have.been.calledWith(true, 100);
});
});

context('when the value is negative', function () {
Copy link
Contributor

@dariakp dariakp Apr 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The AC on the ticket says:

allow it to be a pass-through (no validation)

do we need to revisit this requirement as a team?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any mongo client option has to be added to the connection string options, which is going to validate since the number is set as a uint. A negative number for this setting isn't valid anyways, so there's really no reason to set the type in the connection string module as something that can be invalid.

const options = { keepAliveInitialDelay: -100 };

it('raises an error', function () {
expect(() => {
this.configuration.newClient(options);
}).to.throw(/keepAliveInitialDelay can only be a positive int value/);
});
});
});

context('when keepAliveInitialDelay is not provided', function () {
let client;
let spy;

beforeEach(async function () {
spy = sinon.spy(Socket.prototype, 'setKeepAlive');
client = this.configuration.newClient();
await client.connect();
});

afterEach(async function () {
await client?.close();
spy.restore();
});

it('sets keepalive to 120000', function () {
expect(spy).to.have.been.calledWith(true, 120000);
});
});
});

it('Should correctly pass through appname', {
metadata: {
requires: {
Expand Down