@@ -22,7 +22,7 @@ import {
2222import {
2323 MongoCompatibilityError ,
2424 type MongoDriverError ,
25- MongoError ,
25+ type MongoError ,
2626 MongoErrorLabel ,
2727 MongoOperationTimeoutError ,
2828 MongoRuntimeError ,
@@ -839,8 +839,6 @@ function createAndConnectServer(topology: Topology, serverDescription: ServerDes
839839 server . on ( event , ( e : any ) => topology . emit ( event , e ) ) ;
840840 }
841841
842- server . on ( Server . DESCRIPTION_RECEIVED , description => topology . serverUpdateHandler ( description ) ) ;
843-
844842 server . connect ( ) ;
845843 return server ;
846844}
@@ -851,58 +849,52 @@ function createAndConnectServer(topology: Topology, serverDescription: ServerDes
851849 */
852850function updateServers ( topology : Topology , incomingServerDescription ?: ServerDescription ) {
853851 // update the internal server's description
854- if ( incomingServerDescription && topology . s . servers . has ( incomingServerDescription . address ) ) {
855- const server = topology . s . servers . get ( incomingServerDescription . address ) ;
856- if ( server ) {
857- server . s . description = incomingServerDescription ;
858- if (
859- incomingServerDescription . error instanceof MongoError &&
860- incomingServerDescription . error . hasErrorLabel ( MongoErrorLabel . ResetPool )
861- ) {
862- const interruptInUseConnections = incomingServerDescription . error . hasErrorLabel (
863- MongoErrorLabel . InterruptInUseConnections
864- ) ;
865-
866- server . pool . clear ( { interruptInUseConnections } ) ;
867- } else if ( incomingServerDescription . error == null ) {
868- const newTopologyType = topology . s . description . type ;
869- const shouldMarkPoolReady =
870- incomingServerDescription . isDataBearing ||
871- ( incomingServerDescription . type !== ServerType . Unknown &&
872- newTopologyType === TopologyType . Single ) ;
873- if ( shouldMarkPoolReady ) {
874- server . pool . ready ( ) ;
875- }
852+ const server =
853+ incomingServerDescription && topology . s . servers . get ( incomingServerDescription . address ) ;
854+
855+ // if the incoming description corresponds to a known server, update that server's description
856+ if ( server ) {
857+ server . s . description = incomingServerDescription ;
858+ if ( incomingServerDescription . error == null ) {
859+ const newTopologyType = topology . s . description . type ;
860+ const shouldMarkPoolReady =
861+ incomingServerDescription . isDataBearing ||
862+ ( incomingServerDescription . type !== ServerType . Unknown &&
863+ newTopologyType === TopologyType . Single ) ;
864+ if ( shouldMarkPoolReady ) {
865+ server . pool . ready ( ) ;
876866 }
877867 }
878868 }
879869
880- // add new servers for all descriptions we currently don't know about locally
881- for ( const serverDescription of topology . description . servers . values ( ) ) {
882- if ( ! topology . s . servers . has ( serverDescription . address ) ) {
883- const server = createAndConnectServer ( topology , serverDescription ) ;
884- topology . s . servers . set ( serverDescription . address , server ) ;
870+ function * filter < T > (
871+ iterable : Iterable < T > ,
872+ predicate : ( arg0 : T ) => boolean
873+ ) : Generator < T , void , unknown > {
874+ for ( const value of iterable ) {
875+ if ( predicate ( value ) ) yield value ;
885876 }
886877 }
887878
888- // for all servers no longer known, remove their descriptions and destroy their instances
889- for ( const entry of topology . s . servers ) {
890- const serverAddress = entry [ 0 ] ;
891- if ( topology . description . hasServer ( serverAddress ) ) {
892- continue ;
893- }
894-
895- if ( ! topology . s . servers . has ( serverAddress ) ) {
896- continue ;
897- }
898-
899- const server = topology . s . servers . get ( serverAddress ) ;
900- topology . s . servers . delete ( serverAddress ) ;
879+ // add new servers for all descriptions we currently don't know about locally
880+ const newlyDiscoveredServers = filter (
881+ topology . description . servers . values ( ) ,
882+ description => ! topology . s . servers . has ( description . address )
883+ ) ;
884+ for ( const serverDescription of newlyDiscoveredServers ) {
885+ const server = createAndConnectServer ( topology , serverDescription ) ;
886+ topology . s . servers . set ( serverDescription . address , server ) ;
887+ }
901888
889+ // for all servers no longer known, remove their descriptions and destroy their instances
890+ const unknownServers = filter (
891+ topology . s . servers ,
892+ ( [ address ] ) => ! topology . description . hasServer ( address )
893+ ) ;
894+ for ( const [ address , server ] of unknownServers ) {
895+ topology . s . servers . delete ( address ) ;
902896 // prepare server for garbage collection
903- if ( server ) {
904- closeServer ( server , topology ) ;
905- }
897+ closeServer ( server , topology ) ;
906898 }
907899}
908900
0 commit comments