Skip to content

Commit

Permalink
fix: used weighted RTT calculation for server selection
Browse files Browse the repository at this point in the history
  • Loading branch information
mbroadst committed Dec 30, 2019
1 parent 33a56cf commit d446be5
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
9 changes: 7 additions & 2 deletions lib/core/sdam/server.js
Expand Up @@ -142,7 +142,7 @@ class Server extends EventEmitter {
this.emit(
'descriptionReceived',
new ServerDescription(this.description.address, null, {
roundTripTime: event.duration,
roundTripTime: calculateRoundTripTime(this.description.roundTripTime, event.duration),
error: event.failure
})
);
Expand All @@ -157,7 +157,7 @@ class Server extends EventEmitter {
this.emit(
'descriptionReceived',
new ServerDescription(this.description.address, event.reply, {
roundTripTime: event.duration
roundTripTime: calculateRoundTripTime(this.description.roundTripTime, event.duration)
})
);

Expand Down Expand Up @@ -450,6 +450,11 @@ Object.defineProperty(Server.prototype, 'clusterTime', {
}
});

function calculateRoundTripTime(oldRtt, duration) {
const alpha = 0.2;
return alpha * duration + (1 - alpha) * oldRtt;
}

function basicReadValidations(server, options) {
if (options.readPreference && !(options.readPreference instanceof ReadPreference)) {
return new MongoError('readPreference must be an instance of ReadPreference');
Expand Down
2 changes: 1 addition & 1 deletion lib/core/sdam/server_description.js
Expand Up @@ -69,7 +69,7 @@ class ServerDescription {

this.address = address;
this.error = options.error || null;
this.roundTripTime = options.roundTripTime || 0;
this.roundTripTime = options.roundTripTime || -1;
this.lastUpdateTime = Date.now();
this.lastWriteDate = ismaster.lastWrite ? ismaster.lastWrite.lastWriteDate : null;
this.opTime = ismaster.lastWrite ? ismaster.lastWrite.opTime : null;
Expand Down

0 comments on commit d446be5

Please sign in to comment.