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

Merged
merged 12 commits into from
Apr 23, 2025
7 changes: 4 additions & 3 deletions 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 All @@ -306,6 +307,9 @@ function parseConnectOptions(options: ConnectionOptions): SocketConnectOpts {
(result as Document)[name] = options[name];
}
}
result.keepAliveInitialDelay ??= 120000;
result.keepAlive = true;
result.noDelay = options.noDelay ?? true;

if (typeof hostAddress.socketPath === 'string') {
result.path = hostAddress.socketPath;
Expand Down Expand Up @@ -347,7 +351,6 @@ function parseSslOptions(options: MakeConnectionOptions): TLSConnectionOpts {

export async function makeSocket(options: MakeConnectionOptions): Promise<Stream> {
const useTLS = options.tls ?? false;
const noDelay = options.noDelay ?? true;
const connectTimeoutMS = options.connectTimeoutMS ?? 30000;
const existingSocket = options.existingSocket;

Expand Down Expand Up @@ -376,9 +379,7 @@ export async function makeSocket(options: MakeConnectionOptions): Promise<Stream
socket = net.createConnection(parseConnectOptions(options));
}

socket.setKeepAlive(true, 300000);
socket.setTimeout(connectTimeoutMS);
socket.setNoDelay(noDelay);

let cancellationHandler: ((err: Error) => void) | null = null;

Expand Down
1 change: 1 addition & 0 deletions src/connection_string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1273,6 +1273,7 @@ export const OPTIONS = {
requestCert: { type: 'any' },
rejectUnauthorized: { type: 'any' },
checkServerIdentity: { type: 'any' },
keepAliveInitialDelay: { type: 'any' },
ALPNProtocols: { type: 'any' },
SNICallback: { type: 'any' },
session: { type: 'any' },
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 & {
autoSelectFamily?: boolean;
autoSelectFamilyAttemptTimeout?: number;
/** Node.JS socket option to set the time the first keepalive probe is sent on an idle socket. Defaults to 120000ms */
keepAliveInitialDelay?: number;
},
(typeof LEGAL_TCP_SOCKET_OPTIONS)[number]
>;

Expand Down
222 changes: 211 additions & 11 deletions test/integration/node-specific/mongo_client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,206 @@ 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(net, 'createConnection');
const uri = this.configuration.url();
client = new MongoClient(uri, options);
await client.connect();
});

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

it('passes through the option', {
metadata: { requires: { apiVersion: false } },
test: function () {
expect(spy).to.have.been.calledWith(
sinon.match({
keepAlive: true,
keepAliveInitialDelay: 0
})
);
}
});
});

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

beforeEach(async function () {
spy = sinon.spy(net, 'createConnection');
const uri = this.configuration.url();
client = new MongoClient(uri, options);
await client.connect();
});

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

it('passes through the option', {
metadata: { requires: { apiVersion: false } },
test: function () {
expect(spy).to.have.been.calledWith(
sinon.match({
keepAlive: true,
keepAliveInitialDelay: 100
})
);
}
});
});

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

beforeEach(async function () {
spy = sinon.spy(net, 'createConnection');
const uri = this.configuration.url();
client = new MongoClient(uri, options);
await client.connect();
});

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

it('the Node.js runtime sets the option to 0', {
metadata: { requires: { apiVersion: false } },
test: function () {
expect(spy).to.have.been.calledWith(
sinon.match({
keepAlive: true,
keepAliveInitialDelay: 0
})
);
}
});
});

context('when the value is mistyped', function () {
// Set server selection timeout to get the error quicker.
const options = { keepAliveInitialDelay: 'test', serverSelectionTimeoutMS: 1000 };
let client;
let spy;

beforeEach(async function () {
spy = sinon.spy(net, 'createConnection');
const uri = this.configuration.url();
client = new MongoClient(uri, options);
});

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

it('throws an error', {
metadata: { requires: { apiVersion: false } },
test: async function () {
const error = await client.connect().catch(error => error);
expect(error.message).to.include(
'property must be of type number. Received type string'
);
}
});
});
});

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

beforeEach(async function () {
spy = sinon.spy(net, 'createConnection');
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(
sinon.match({
keepAlive: true,
keepAliveInitialDelay: 120000
})
);
});
});

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

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

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

it('sets noDelay to true', function () {
expect(spy).to.have.been.calledWith(
sinon.match({
noDelay: true
})
);
});
});

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

beforeEach(async function () {
const options = { noDelay: false };
spy = sinon.spy(net, 'createConnection');
const uri = this.configuration.url();
client = new MongoClient(uri, options);
await client.connect();
});

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

it('sets noDelay', {
metadata: { requires: { apiVersion: false } },
test: function () {
expect(spy).to.have.been.calledWith(
sinon.match({
noDelay: false
})
);
}
});
});
});

it('Should correctly pass through appname', {
metadata: {
requires: {
Expand Down Expand Up @@ -889,12 +1089,12 @@ describe('class MongoClient', function () {
metadata: { requires: { topology: ['single'] } },
test: async function () {
await client.connect();
expect(netSpy).to.have.been.calledWith({
autoSelectFamily: false,
autoSelectFamilyAttemptTimeout: 100,
host: 'localhost',
port: 27017
});
expect(netSpy).to.have.been.calledWith(
sinon.match({
autoSelectFamily: false,
autoSelectFamilyAttemptTimeout: 100
})
);
}
});
});
Expand All @@ -908,11 +1108,11 @@ describe('class MongoClient', function () {
metadata: { requires: { topology: ['single'] } },
test: async function () {
await client.connect();
expect(netSpy).to.have.been.calledWith({
autoSelectFamily: true,
host: 'localhost',
port: 27017
});
expect(netSpy).to.have.been.calledWith(
sinon.match({
autoSelectFamily: true
})
);
}
});
});
Expand Down
37 changes: 17 additions & 20 deletions test/manual/tls_support.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,19 +77,17 @@ describe('TLS Support', function () {

it('sets the default options', async function () {
await client.connect();
expect(tlsSpy).to.have.been.calledWith({
autoSelectFamily: true,
host: 'localhost',
port: 27017,
servername: 'localhost',
ca: sinon.match.defined,
cert: sinon.match.defined,
key: sinon.match.defined
});
expect(tlsSpy).to.have.been.calledWith(
sinon.match({
ca: sinon.match.defined,
cert: sinon.match.defined,
key: sinon.match.defined
})
);
});
});

context('when auto family options are set', function () {
context('when auto select family options are set', function () {
let tlsSpy;

afterEach(function () {
Expand All @@ -107,16 +105,15 @@ describe('TLS Support', function () {

it('sets the provided options', async function () {
await client.connect();
expect(tlsSpy).to.have.been.calledWith({
autoSelectFamily: false,
autoSelectFamilyAttemptTimeout: 100,
host: 'localhost',
port: 27017,
servername: 'localhost',
ca: sinon.match.defined,
cert: sinon.match.defined,
key: sinon.match.defined
});
expect(tlsSpy).to.have.been.calledWith(
sinon.match({
autoSelectFamily: false,
autoSelectFamilyAttemptTimeout: 100,
ca: sinon.match.defined,
cert: sinon.match.defined,
key: sinon.match.defined
})
);
});
});

Expand Down