Skip to content

Commit f597e52

Browse files
committed
refactor(monitoring): simplify logic for rescheduling monitoring
1 parent 16bca55 commit f597e52

File tree

1 file changed

+9
-11
lines changed

1 file changed

+9
-11
lines changed

lib/core/sdam/monitoring.js

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -130,10 +130,13 @@ function monitorServer(server, options) {
130130
options = options || {};
131131
const heartbeatFrequencyMS = options.heartbeatFrequencyMS || 10000;
132132

133-
if (options.initial === true) {
134-
server.s.monitorId = setTimeout(() => monitorServer(server), heartbeatFrequencyMS);
135-
return;
136-
}
133+
const rescheduleMonitoring = () => {
134+
server.s.monitoring = false;
135+
server.s.monitorId = setTimeout(() => {
136+
server.s.monitorId = undefined;
137+
server.monitor();
138+
}, heartbeatFrequencyMS);
139+
};
137140

138141
// executes a single check of a server
139142
const checkServer = callback => {
@@ -176,16 +179,13 @@ function monitorServer(server, options) {
176179
};
177180

178181
const successHandler = isMaster => {
179-
server.s.monitoring = false;
180-
181182
// emit an event indicating that our description has changed
182183
server.emit('descriptionReceived', new ServerDescription(server.description.address, isMaster));
183184
if (server.s.state === STATE_CLOSED || server.s.state === STATE_CLOSING) {
184185
return;
185186
}
186187

187-
// schedule the next monitoring process
188-
server.s.monitorId = setTimeout(() => monitorServer(server), heartbeatFrequencyMS);
188+
rescheduleMonitoring();
189189
};
190190

191191
// run the actual monitoring loop
@@ -203,15 +203,13 @@ function monitorServer(server, options) {
203203
// otherwise re-attempt monitoring once
204204
checkServer((error, isMaster) => {
205205
if (error) {
206-
server.s.monitoring = false;
207-
208206
// we revert to an `Unknown` by emitting a default description with no isMaster
209207
server.emit(
210208
'descriptionReceived',
211209
new ServerDescription(server.description.address, null, { error })
212210
);
213211

214-
// we do not reschedule monitoring in this case
212+
rescheduleMonitoring();
215213
return;
216214
}
217215

0 commit comments

Comments
 (0)