Skip to content

Commit ddfa41b

Browse files
committed
fix: use duration of handshake if no previous roundTripTime exists
The default `roundTripTime` of a `ServerDescription` is -1, which means if that value is used we can potentially calculate a negative `roundTripTime`. Instead, if no previous `roundTripTime` exists, we use the duration of the initial handshake NODE-2652
1 parent 76249cb commit ddfa41b

File tree

3 files changed

+13
-13
lines changed

3 files changed

+13
-13
lines changed

lib/core/sdam/server.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,10 @@ Object.defineProperty(Server.prototype, 'clusterTime', {
415415
});
416416

417417
function calculateRoundTripTime(oldRtt, duration) {
418+
if (oldRtt === -1) {
419+
return duration;
420+
}
421+
418422
const alpha = 0.2;
419423
return alpha * duration + (1 - alpha) * oldRtt;
420424
}

lib/utils.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -741,7 +741,6 @@ function now() {
741741

742742
function calculateDurationInMs(started) {
743743
if (typeof started !== 'number') {
744-
console.trace('HERE');
745744
throw TypeError('numeric value required to calculate duration');
746745
}
747746

test/unit/sdam/monitoring.test.js

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -87,20 +87,17 @@ describe('monitoring', function() {
8787
const topology = new Topology(mockServer.uri());
8888
topology.connect(err => {
8989
expect(err).to.not.exist;
90+
expect(topology)
91+
.property('description')
92+
.property('servers')
93+
.to.have.length(1);
9094

91-
setTimeout(() => {
92-
expect(topology)
93-
.property('description')
94-
.property('servers')
95-
.to.have.length(1);
96-
97-
const serverDescription = Array.from(topology.description.servers.values())[0];
98-
expect(serverDescription)
99-
.property('roundTripTime')
100-
.to.be.greaterThan(0);
95+
const serverDescription = Array.from(topology.description.servers.values())[0];
96+
expect(serverDescription)
97+
.property('roundTripTime')
98+
.to.be.greaterThan(0);
10199

102-
topology.close(done);
103-
}, 500);
100+
topology.close(done);
104101
});
105102
});
106103

0 commit comments

Comments
 (0)