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
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 4 additions & 1 deletion src/cmap/connect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,10 @@ function parseConnectOptions(options: ConnectionOptions): SocketConnectOpts {
(result as Document)[name] = options[name];
}
}
if (result.keepAliveInitialDelay == null) {
result.keepAliveInitialDelay = 120000;
}
result.keepAlive = true;

if (typeof hostAddress.socketPath === 'string') {
result.path = hostAddress.socketPath;
Expand Down Expand Up @@ -377,7 +381,6 @@ export async function makeSocket(options: MakeConnectionOptions): Promise<Stream
socket = net.createConnection(parseConnectOptions(options));
}

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

Expand Down
4 changes: 1 addition & 3 deletions src/connection_string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -872,9 +872,6 @@ export const OPTIONS = {
return wc;
}
},
keepAliveInitialDelay: {
type: 'uint'
},
loadBalanced: {
default: false,
type: 'boolean'
Expand Down Expand Up @@ -1276,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
59 changes: 46 additions & 13 deletions test/integration/node-specific/mongo_client.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
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 @@ -144,8 +143,9 @@ describe('class MongoClient', function () {
let spy;

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

Expand All @@ -155,7 +155,12 @@ describe('class MongoClient', function () {
});

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

Expand All @@ -165,8 +170,9 @@ describe('class MongoClient', function () {
let spy;

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

Expand All @@ -176,17 +182,39 @@ describe('class MongoClient', function () {
});

it('passes through the option', function () {
expect(spy).to.have.been.calledWith(true, 100);
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();
});

it('raises an error', function () {
expect(() => {
this.configuration.newClient(options);
}).to.throw(/keepAliveInitialDelay can only be a positive int value/);
afterEach(async function () {
await client?.close();
spy.restore();
});

it('sets the option to 0', function () {
expect(spy).to.have.been.calledWith(
sinon.match({
keepAlive: true,
keepAliveInitialDelay: 0
})
);
});
});
});
Expand All @@ -196,7 +224,7 @@ describe('class MongoClient', function () {
let spy;

beforeEach(async function () {
spy = sinon.spy(Socket.prototype, 'setKeepAlive');
spy = sinon.spy(net, 'createConnection');
client = this.configuration.newClient();
await client.connect();
});
Expand All @@ -207,7 +235,12 @@ describe('class MongoClient', function () {
});

it('sets keepalive to 120000', function () {
expect(spy).to.have.been.calledWith(true, 120000);
expect(spy).to.have.been.calledWith(
sinon.match({
keepAlive: true,
keepAliveInitialDelay: 120000
})
);
});
});
});
Expand Down