@@ -392,9 +392,7 @@ export class Server extends TypedEventEmitter<ServerEvents> {
392392 return ;
393393 }
394394
395- const isStaleError =
396- error . connectionGeneration && error . connectionGeneration < this . pool . generation ;
397- if ( isStaleError ) {
395+ if ( isStaleError ( this , error ) ) {
398396 return ;
399397 }
400398
@@ -421,19 +419,17 @@ export class Server extends TypedEventEmitter<ServerEvents> {
421419 // error would be wrapped in a ServerHeartbeatFailedEvent, which would mark the
422420 // server unknown and clear the pool. Can we remove this?
423421 if ( isStateChangeError ( error ) || error instanceof MongoParseError ) {
424- if ( shouldHandleStateChangeError ( this , error ) ) {
425- const shouldClearPool = isNodeShuttingDownError ( error ) ;
426- if ( this . loadBalanced && connection && shouldClearPool ) {
427- this . pool . clear ( { serviceId : connection . serviceId } ) ;
428- }
422+ const shouldClearPool = isNodeShuttingDownError ( error ) ;
423+ if ( this . loadBalanced && connection && shouldClearPool ) {
424+ this . pool . clear ( { serviceId : connection . serviceId } ) ;
425+ }
429426
430- if ( ! this . loadBalanced ) {
431- if ( shouldClearPool ) {
432- error . addErrorLabel ( MongoErrorLabel . ResetPool ) ;
433- }
434- markServerUnknown ( this , error ) ;
435- process . nextTick ( ( ) => this . requestCheck ( ) ) ;
427+ if ( ! this . loadBalanced ) {
428+ if ( shouldClearPool ) {
429+ error . addErrorLabel ( MongoErrorLabel . ResetPool ) ;
436430 }
431+ markServerUnknown ( this , error ) ;
432+ process . nextTick ( ( ) => this . requestCheck ( ) ) ;
437433 }
438434 }
439435 }
@@ -568,12 +564,6 @@ function connectionIsStale(pool: ConnectionPool, connection: Connection) {
568564 return connection . generation !== pool . generation ;
569565}
570566
571- function shouldHandleStateChangeError ( server : Server , err : MongoError ) {
572- const etv = err . topologyVersion ;
573- const stv = server . description . topologyVersion ;
574- return compareTopologyVersion ( stv , etv ) < 0 ;
575- }
576-
577567function inActiveTransaction ( session : ClientSession | undefined , cmd : Document ) {
578568 return session && session . inTransaction ( ) && ! isTransactionCommand ( cmd ) ;
579569}
@@ -583,3 +573,15 @@ function inActiveTransaction(session: ClientSession | undefined, cmd: Document)
583573function isRetryableWritesEnabled ( topology : Topology ) {
584574 return topology . s . options . retryWrites !== false ;
585575}
576+
577+ function isStaleError ( server : Server , error : MongoError ) : boolean {
578+ const currentGeneration = server . pool . generation ;
579+ const generation = error . connectionGeneration ;
580+
581+ if ( generation && generation < currentGeneration ) {
582+ return true ;
583+ }
584+
585+ const currentTopologyVersion = server . description . topologyVersion ;
586+ return compareTopologyVersion ( currentTopologyVersion , error . topologyVersion ) >= 0 ;
587+ }
0 commit comments