@@ -178,9 +178,9 @@ class Topology extends EventEmitter {
178
178
clusterTime : null ,
179
179
180
180
// timer management
181
- monitorTimers : [ ] ,
182
- iterationTimers : [ ] ,
183
- connectionTimers : [ ]
181
+ monitorTimers : new Set ( ) ,
182
+ iterationTimers : new Set ( ) ,
183
+ connectionTimers : new Set ( )
184
184
} ;
185
185
186
186
// amend options for server instance creation
@@ -334,14 +334,9 @@ class Topology extends EventEmitter {
334
334
}
335
335
336
336
// 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 ) ;
345
340
346
341
if ( this . s . sessionPool ) {
347
342
this . s . sessions . forEach ( session => session . endSession ( ) ) ;
@@ -427,9 +422,7 @@ class Topology extends EventEmitter {
427
422
return ;
428
423
}
429
424
430
- // clear out any existing iteration timers
431
- this . s . iterationTimers . map ( timer => clearTimeout ( timer ) ) ;
432
- this . s . iterationTimers = [ ] ;
425
+ drainTimerQueue ( this . s . iterationTimers ) ;
433
426
434
427
selectServers (
435
428
this ,
@@ -848,10 +841,11 @@ function selectServers(topology, selector, timeout, start, callback) {
848
841
} , timeout - duration ) ;
849
842
850
843
const connectHandler = ( ) => {
851
- clearTimeout ( failToConnectTimer ) ;
844
+ clearAndRemoveTimerFrom ( failToConnectTimer , topology . s . connectionTimers ) ;
852
845
selectServers ( topology , selector , timeout , process . hrtime ( ) , callback ) ;
853
846
} ;
854
847
848
+ topology . s . connectionTimers . add ( failToConnectTimer ) ;
855
849
topology . once ( 'connect' , connectHandler ) ;
856
850
return ;
857
851
}
@@ -884,8 +878,7 @@ function selectServers(topology, selector, timeout, start, callback) {
884
878
885
879
const retrySelection = ( ) => {
886
880
// clear all existing monitor timers
887
- topology . s . monitorTimers . map ( timer => clearTimeout ( timer ) ) ;
888
- topology . s . monitorTimers = [ ] ;
881
+ drainTimerQueue ( topology . s . monitorTimers ) ;
889
882
890
883
// ensure all server monitors attempt monitoring soon
891
884
topology . s . servers . forEach ( server => {
@@ -894,7 +887,7 @@ function selectServers(topology, selector, timeout, start, callback) {
894
887
TOPOLOGY_DEFAULTS . minHeartbeatFrequencyMS
895
888
) ;
896
889
897
- topology . s . monitorTimers . push ( timer ) ;
890
+ topology . s . monitorTimers . add ( timer ) ;
898
891
} ) ;
899
892
900
893
const iterationTimer = setTimeout ( ( ) => {
@@ -909,15 +902,14 @@ function selectServers(topology, selector, timeout, start, callback) {
909
902
910
903
const descriptionChangedHandler = ( ) => {
911
904
// successful iteration, clear the check timer
912
- removeTimerFrom ( iterationTimer , topology . s . iterationTimers ) ;
913
- clearTimeout ( iterationTimer ) ;
905
+ clearAndRemoveTimerFrom ( iterationTimer , topology . s . iterationTimers ) ;
914
906
915
907
// topology description has changed due to monitoring, reattempt server selection
916
908
selectServers ( topology , selector , timeout , start , callback ) ;
917
909
} ;
918
910
919
911
// 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 ) ;
921
913
922
914
topology . once ( 'topologyDescriptionChanged' , descriptionChangedHandler ) ;
923
915
} ;
@@ -940,11 +932,11 @@ function createAndConnectServer(topology, serverDescription, connectDelay) {
940
932
941
933
if ( connectDelay ) {
942
934
const connectTimer = setTimeout ( ( ) => {
943
- removeTimerFrom ( connectTimer , topology . s . connectionTimers ) ;
935
+ clearAndRemoveTimerFrom ( connectTimer , topology . s . connectionTimers ) ;
944
936
server . connect ( ) ;
945
937
} , connectDelay ) ;
946
938
947
- topology . s . connectionTimers . push ( connectTimer ) ;
939
+ topology . s . connectionTimers . add ( connectTimer ) ;
948
940
return server ;
949
941
}
950
942
@@ -971,9 +963,14 @@ function resetServer(topology, serverDescription) {
971
963
topology . s . servers . set ( serverDescription . address , newServer ) ;
972
964
}
973
965
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 ) ;
977
974
}
978
975
979
976
/**
0 commit comments