Skip to content

Commit 3a5832b

Browse files
committed
feat(NODE-6245): add keepAliveInitialDelay config
1 parent f0b8739 commit 3a5832b

File tree

4 files changed

+98
-2
lines changed

4 files changed

+98
-2
lines changed

src/cmap/connect.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,7 @@ export const LEGAL_TLS_SOCKET_OPTIONS = [
289289
export const LEGAL_TCP_SOCKET_OPTIONS = [
290290
'autoSelectFamily',
291291
'autoSelectFamilyAttemptTimeout',
292+
'keepAliveInitialDelay',
292293
'family',
293294
'hints',
294295
'localAddress',
@@ -376,7 +377,7 @@ export async function makeSocket(options: MakeConnectionOptions): Promise<Stream
376377
socket = net.createConnection(parseConnectOptions(options));
377378
}
378379

379-
socket.setKeepAlive(true, 300000);
380+
socket.setKeepAlive(true, options.keepAliveInitialDelay ?? 120000);
380381
socket.setTimeout(connectTimeoutMS);
381382
socket.setNoDelay(noDelay);
382383

src/connection_string.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -872,6 +872,9 @@ export const OPTIONS = {
872872
return wc;
873873
}
874874
},
875+
keepAliveInitialDelay: {
876+
type: 'uint'
877+
},
875878
loadBalanced: {
876879
default: false,
877880
type: 'boolean'

src/mongo_client.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,11 @@ export type SupportedTLSSocketOptions = Pick<
115115

116116
/** @public */
117117
export type SupportedSocketOptions = Pick<
118-
TcpNetConnectOpts & { autoSelectFamily?: boolean; autoSelectFamilyAttemptTimeout?: number },
118+
TcpNetConnectOpts & {
119+
autoSelectFamily?: boolean;
120+
autoSelectFamilyAttemptTimeout?: number;
121+
keepAliveInitialDelay?: number;
122+
},
119123
(typeof LEGAL_TCP_SOCKET_OPTIONS)[number]
120124
>;
121125

test/integration/node-specific/mongo_client.test.ts

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { expect } from 'chai';
22
import { once } from 'events';
33
import * as net from 'net';
4+
import { Socket } from 'net';
45
import * as sinon from 'sinon';
56

67
import {
@@ -135,6 +136,93 @@ describe('class MongoClient', function () {
135136
expect(error).to.be.instanceOf(MongoServerSelectionError);
136137
});
137138

139+
describe('#connect', function () {
140+
context('when keepAliveInitialDelay is provided', function () {
141+
context('when the value is 0', function () {
142+
const options = { keepAliveInitialDelay: 0 };
143+
let client;
144+
let spy;
145+
146+
beforeEach(async function () {
147+
spy = sinon.spy(Socket.prototype, 'setKeepAlive');
148+
client = this.configuration.newClient(options);
149+
await client.connect();
150+
});
151+
152+
afterEach(async function () {
153+
await client?.close();
154+
spy.restore();
155+
});
156+
157+
it('passes through the option', function () {
158+
expect(spy).to.have.been.calledWith(true, 0);
159+
});
160+
});
161+
162+
context('when the value is positive', function () {
163+
const options = { keepAliveInitialDelay: 100 };
164+
let client;
165+
let spy;
166+
167+
beforeEach(async function () {
168+
spy = sinon.spy(Socket.prototype, 'setKeepAlive');
169+
client = this.configuration.newClient(options);
170+
await client.connect();
171+
});
172+
173+
afterEach(async function () {
174+
await client?.close();
175+
spy.restore();
176+
});
177+
178+
it('passes through the option', function () {
179+
expect(spy).to.have.been.calledWith(true, 100);
180+
});
181+
});
182+
183+
context('when the value is negative', function () {
184+
const options = { keepAliveInitialDelay: -100 };
185+
let client;
186+
let spy;
187+
188+
beforeEach(async function () {
189+
spy = sinon.spy(Socket.prototype, 'setKeepAlive');
190+
client = this.configuration.newClient(options);
191+
await client.connect();
192+
});
193+
194+
afterEach(async function () {
195+
await client?.close();
196+
spy.restore();
197+
});
198+
199+
it('passes through the option', function () {
200+
expect(spy).to.have.been.calledWith(true, -100);
201+
});
202+
});
203+
});
204+
205+
context('when keepAliveInitialDelay is not provided', function () {
206+
let client;
207+
let spy;
208+
209+
beforeEach(async function () {
210+
spy = sinon.spy(Socket.prototype, 'setKeepAlive');
211+
client = this.configuration.newClient();
212+
await client.connect();
213+
});
214+
215+
afterEach(async function () {
216+
await client?.close();
217+
spy.restore();
218+
});
219+
220+
it('sets keepalive to 120000', function () {
221+
expect(spy).to.have.been.calledWith(true, 120000);
222+
});
223+
});
224+
});
225+
138226
it('Should correctly pass through appname', {
139227
metadata: {
140228
requires: {

0 commit comments

Comments
 (0)