@@ -38,6 +38,7 @@ import { Timeout, TimeoutContext, TimeoutError } from '../timeout';
3838import type { Transaction } from '../transactions' ;
3939import {
4040 type Callback ,
41+ csotMin ,
4142 type EventEmitterWithState ,
4243 HostAddress ,
4344 List ,
@@ -460,20 +461,13 @@ export class Topology extends TypedEventEmitter<TopologyEvents> {
460461 }
461462 }
462463
463- const timeoutMS = this . client . s . options . timeoutMS ;
464- const serverSelectionTimeoutMS = this . client . s . options . serverSelectionTimeoutMS ;
464+ const timeoutMS = this . client . options . timeoutMS ;
465+ const timeout = timeoutMS != null ? Timeout . expires ( timeoutMS ) : undefined ;
465466 const readPreference = options . readPreference ?? ReadPreference . primary ;
466-
467- const timeoutContext = TimeoutContext . create ( {
468- timeoutMS,
469- serverSelectionTimeoutMS,
470- waitQueueTimeoutMS : this . client . s . options . waitQueueTimeoutMS
471- } ) ;
472-
473467 const selectServerOptions = {
474468 operationName : 'ping' ,
475- ... options ,
476- timeoutContext
469+ timeout ,
470+ ... options
477471 } ;
478472 try {
479473 const server = await this . selectServer (
@@ -483,7 +477,7 @@ export class Topology extends TypedEventEmitter<TopologyEvents> {
483477
484478 const skipPingOnConnect = this . s . options [ Symbol . for ( '@@mdb.skipPingOnConnect' ) ] === true ;
485479 if ( ! skipPingOnConnect && server && this . s . credentials ) {
486- await server . command ( ns ( 'admin.$cmd' ) , { ping : 1 } , { timeoutContext } ) ;
480+ await server . command ( ns ( 'admin.$cmd' ) , { ping : 1 } , { timeout } ) ;
487481 stateTransition ( this , STATE_CONNECTED ) ;
488482 this . emit ( Topology . OPEN , this ) ;
489483 this . emit ( Topology . CONNECT , this ) ;
@@ -572,10 +566,24 @@ export class Topology extends TypedEventEmitter<TopologyEvents> {
572566 new ServerSelectionStartedEvent ( selector , this . description , options . operationName )
573567 ) ;
574568 }
575- let timeout ;
576- if ( options . timeoutContext ) timeout = options . timeoutContext . serverSelectionTimeout ;
577- else {
578- timeout = Timeout . expires ( options . serverSelectionTimeoutMS ?? 0 ) ;
569+ const serverSelectionTimeoutMS = options . serverSelectionTimeoutMS ?? 0 ;
570+ let timeout : Timeout | null ;
571+ if ( options . timeout ) {
572+ // CSOT Enabled
573+ if ( options . timeout . duration > 0 || serverSelectionTimeoutMS > 0 ) {
574+ if (
575+ options . timeout . duration === serverSelectionTimeoutMS ||
576+ csotMin ( options . timeout . duration , serverSelectionTimeoutMS ) < serverSelectionTimeoutMS
577+ ) {
578+ timeout = options . timeout ;
579+ } else {
580+ timeout = Timeout . expires ( serverSelectionTimeoutMS ) ;
581+ }
582+ } else {
583+ timeout = null ;
584+ }
585+ } else {
586+ timeout = Timeout . expires ( serverSelectionTimeoutMS ) ;
579587 }
580588
581589 const isSharded = this . description . type === TopologyType . Sharded ;
@@ -599,7 +607,7 @@ export class Topology extends TypedEventEmitter<TopologyEvents> {
599607 )
600608 ) ;
601609 }
602- if ( options . timeoutContext ?. clearServerSelectionTimeout ) timeout ?. clear ( ) ;
610+ if ( timeout !== options . timeout ) timeout ?. clear ( ) ;
603611 return transaction . server ;
604612 }
605613
@@ -649,7 +657,7 @@ export class Topology extends TypedEventEmitter<TopologyEvents> {
649657 ) ;
650658 }
651659
652- if ( options . timeoutContext ?. csotEnabled ( ) ) {
660+ if ( options . timeout ) {
653661 throw new MongoOperationTimeoutError ( 'Timed out during server selection' , {
654662 cause : timeoutError
655663 } ) ;
@@ -659,7 +667,7 @@ export class Topology extends TypedEventEmitter<TopologyEvents> {
659667 // Other server selection error
660668 throw error ;
661669 } finally {
662- if ( options . timeoutContext ?. clearServerSelectionTimeout ) timeout ?. clear ( ) ;
670+ if ( timeout !== options . timeout ) timeout ?. clear ( ) ;
663671 }
664672 }
665673 /**
0 commit comments