Skip to content

Commit 920cf27

Browse files
committed
refactor(topology): use sets for timer management
1 parent f347313 commit 920cf27

File tree

1 file changed

+23
-26
lines changed

1 file changed

+23
-26
lines changed

lib/core/sdam/topology.js

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -178,9 +178,9 @@ class Topology extends EventEmitter {
178178
clusterTime: null,
179179

180180
// timer management
181-
monitorTimers: [],
182-
iterationTimers: [],
183-
connectionTimers: []
181+
monitorTimers: new Set(),
182+
iterationTimers: new Set(),
183+
connectionTimers: new Set()
184184
};
185185

186186
// amend options for server instance creation
@@ -334,14 +334,9 @@ class Topology extends EventEmitter {
334334
}
335335

336336
// clear all existing monitor timers
337-
this.s.monitorTimers.map(timer => clearTimeout(timer));
338-
this.s.monitorTimers = [];
339-
340-
this.s.iterationTimers.map(timer => clearTimeout(timer));
341-
this.s.iterationTimers = [];
342-
343-
this.s.connectionTimers.map(timer => clearTimeout(timer));
344-
this.s.connectionTimers = [];
337+
drainTimerQueue(this.s.monitorTimers);
338+
drainTimerQueue(this.s.iterationTimers);
339+
drainTimerQueue(this.s.connectionTimers);
345340

346341
if (this.s.sessionPool) {
347342
this.s.sessions.forEach(session => session.endSession());
@@ -427,9 +422,7 @@ class Topology extends EventEmitter {
427422
return;
428423
}
429424

430-
// clear out any existing iteration timers
431-
this.s.iterationTimers.map(timer => clearTimeout(timer));
432-
this.s.iterationTimers = [];
425+
drainTimerQueue(this.s.iterationTimers);
433426

434427
selectServers(
435428
this,
@@ -848,10 +841,11 @@ function selectServers(topology, selector, timeout, start, callback) {
848841
}, timeout - duration);
849842

850843
const connectHandler = () => {
851-
clearTimeout(failToConnectTimer);
844+
clearAndRemoveTimerFrom(failToConnectTimer, topology.s.connectionTimers);
852845
selectServers(topology, selector, timeout, process.hrtime(), callback);
853846
};
854847

848+
topology.s.connectionTimers.add(failToConnectTimer);
855849
topology.once('connect', connectHandler);
856850
return;
857851
}
@@ -884,8 +878,7 @@ function selectServers(topology, selector, timeout, start, callback) {
884878

885879
const retrySelection = () => {
886880
// clear all existing monitor timers
887-
topology.s.monitorTimers.map(timer => clearTimeout(timer));
888-
topology.s.monitorTimers = [];
881+
drainTimerQueue(topology.s.monitorTimers);
889882

890883
// ensure all server monitors attempt monitoring soon
891884
topology.s.servers.forEach(server => {
@@ -894,7 +887,7 @@ function selectServers(topology, selector, timeout, start, callback) {
894887
TOPOLOGY_DEFAULTS.minHeartbeatFrequencyMS
895888
);
896889

897-
topology.s.monitorTimers.push(timer);
890+
topology.s.monitorTimers.add(timer);
898891
});
899892

900893
const iterationTimer = setTimeout(() => {
@@ -909,15 +902,14 @@ function selectServers(topology, selector, timeout, start, callback) {
909902

910903
const descriptionChangedHandler = () => {
911904
// successful iteration, clear the check timer
912-
removeTimerFrom(iterationTimer, topology.s.iterationTimers);
913-
clearTimeout(iterationTimer);
905+
clearAndRemoveTimerFrom(iterationTimer, topology.s.iterationTimers);
914906

915907
// topology description has changed due to monitoring, reattempt server selection
916908
selectServers(topology, selector, timeout, start, callback);
917909
};
918910

919911
// track this timer in case we need to clean it up outside this loop
920-
topology.s.iterationTimers.push(iterationTimer);
912+
topology.s.iterationTimers.add(iterationTimer);
921913

922914
topology.once('topologyDescriptionChanged', descriptionChangedHandler);
923915
};
@@ -940,11 +932,11 @@ function createAndConnectServer(topology, serverDescription, connectDelay) {
940932

941933
if (connectDelay) {
942934
const connectTimer = setTimeout(() => {
943-
removeTimerFrom(connectTimer, topology.s.connectionTimers);
935+
clearAndRemoveTimerFrom(connectTimer, topology.s.connectionTimers);
944936
server.connect();
945937
}, connectDelay);
946938

947-
topology.s.connectionTimers.push(connectTimer);
939+
topology.s.connectionTimers.add(connectTimer);
948940
return server;
949941
}
950942

@@ -971,9 +963,14 @@ function resetServer(topology, serverDescription) {
971963
topology.s.servers.set(serverDescription.address, newServer);
972964
}
973965

974-
function removeTimerFrom(timer, timers) {
975-
const idx = timers.findIndex(t => t === timer);
976-
timers.splice(idx, 1);
966+
function drainTimerQueue(queue) {
967+
queue.forEach(clearTimeout);
968+
queue.clear();
969+
}
970+
971+
function clearAndRemoveTimerFrom(timer, timers) {
972+
clearTimeout(timer);
973+
return timers.delete(timer);
977974
}
978975

979976
/**

0 commit comments

Comments
 (0)