Skip to content

Commit 4812eef

Browse files
committed
use instantaneous heartbeat duration and not average for ServerHeartbeatSucceededEvent
1 parent 85124d8 commit 4812eef

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

src/sdam/monitor.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,10 @@ export class Monitor extends TypedEventEmitter<MonitorEvents> {
220220
return this.rttSamplesMS.min();
221221
}
222222

223+
get latestRTT(): number {
224+
return this.rttSamplesMS.last ?? 0; // FIXME: Check if this is acceptable
225+
}
226+
223227
addRttSample(rtt: number) {
224228
this.rttSamplesMS.addSample(rtt);
225229
}
@@ -299,12 +303,10 @@ function checkServer(monitor: Monitor, callback: Callback<Document | null>) {
299303
hello.isWritablePrimary = hello[LEGACY_HELLO_COMMAND];
300304
}
301305

302-
// FIXME: Figure out how to set this. Should duration be the instantaneous rtt or the averaged
303-
// rtt?
306+
// NOTE: here we use the latestRTT as this measurment corresponds with the value
307+
// obtained for this successful heartbeat
304308
const duration =
305-
isAwaitable && monitor.rttPinger
306-
? monitor.rttPinger.roundTripTime
307-
: calculateDurationInMs(start);
309+
isAwaitable && monitor.rttPinger ? monitor.rttPinger.latestRTT : calculateDurationInMs(start);
308310

309311
monitor.emitAndLogHeartbeat(
310312
Server.SERVER_HEARTBEAT_SUCCEEDED,
@@ -508,6 +510,10 @@ export class RTTPinger {
508510
return this.monitor.minRoundTripTime;
509511
}
510512

513+
get latestRTT(): number {
514+
return this.monitor.latestRTT;
515+
}
516+
511517
close(): void {
512518
this.closed = true;
513519
clearTimeout(this[kMonitorId]);

src/utils.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1337,6 +1337,7 @@ export function maybeAddIdToDocuments(
13371337
}
13381338

13391339
export class MovingWindow {
1340+
/** Index of the next slot to be overwritten */
13401341
private writeIndex: number;
13411342
private length: number;
13421343
private samples: Float64Array;
@@ -1376,6 +1377,11 @@ export class MovingWindow {
13761377
return sum / this.length;
13771378
}
13781379

1380+
get last(): number | null {
1381+
if (this.length === 0) return null;
1382+
return this.samples[this.writeIndex === 0 ? this.length - 1 : this.writeIndex - 1];
1383+
}
1384+
13791385
clear() {
13801386
this.length = 0;
13811387
this.writeIndex = 0;

0 commit comments

Comments
 (0)