Skip to content

Commit

Permalink
fix: reschedule monitoring before emitting heartbeat events
Browse files Browse the repository at this point in the history
  • Loading branch information
mbroadst committed Jan 1, 2020
1 parent a13dc68 commit 7fcbeb5
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 11 deletions.
20 changes: 11 additions & 9 deletions lib/core/sdam/monitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,33 +213,35 @@ function rescheduleMonitoring(monitor, ms) {
return;
}

stateTransition(monitor, STATE_IDLE);

monitor[kLastCheckTime] = process.hrtime();
monitor[kMonitorId] = setTimeout(() => {
monitor[kMonitorId] = undefined;
monitor.requestCheck();
}, ms || heartbeatFrequencyMS);

stateTransition(monitor, STATE_IDLE);
}

function successHandler(monitor, start, isMaster) {
rescheduleMonitoring(monitor);

process.nextTick(() =>
monitor.emit(
'serverHeartbeatSucceeded',
new ServerHeartbeatSucceededEvent(calculateDurationInMs(start), isMaster, monitor.address)
)
);

rescheduleMonitoring(monitor);
}

function failureHandler(monitor, start, err) {
monitor.emit(
'serverHeartbeatFailed',
new ServerHeartbeatFailedEvent(calculateDurationInMs(start), err, monitor.address)
);

rescheduleMonitoring(monitor);

process.nextTick(() =>
monitor.emit(
'serverHeartbeatFailed',
new ServerHeartbeatFailedEvent(calculateDurationInMs(start), err, monitor.address)
)
);
}

module.exports = {
Expand Down
3 changes: 1 addition & 2 deletions test/unit/sdam/monitoring.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,7 @@ describe('monitoring', function() {
acceptConnections = true;
}, 250);

// set `heartbeatFrequencyMS` to 250ms to force a quick monitoring check, and wait 500ms to validate below
const topology = new Topology(mockServer.uri(), { heartbeatFrequencyMS: 250 });
const topology = new Topology(mockServer.uri());
topology.connect(err => {
expect(err).to.not.exist;

Expand Down

0 comments on commit 7fcbeb5

Please sign in to comment.