File tree Expand file tree Collapse file tree 1 file changed +15
-0
lines changed Expand file tree Collapse file tree 1 file changed +15
-0
lines changed Original file line number Diff line number Diff line change @@ -24,6 +24,7 @@ import {
24
24
type MongoDriverError ,
25
25
MongoError ,
26
26
MongoErrorLabel ,
27
+ MongoNetworkTimeoutError ,
27
28
MongoOperationTimeoutError ,
28
29
MongoRuntimeError ,
29
30
MongoServerSelectionError ,
@@ -44,6 +45,7 @@ import {
44
45
makeStateMachine ,
45
46
now ,
46
47
ns ,
48
+ once ,
47
49
promiseWithResolvers ,
48
50
shuffle
49
51
} from '../utils' ;
@@ -440,6 +442,8 @@ export class Topology extends TypedEventEmitter<TopologyEvents> {
440
442
)
441
443
) ;
442
444
445
+ // Create a wait condition that blocks until we get at least once successful heartbeat
446
+ const heartbeatWaitCond = once ( this , Server . SERVER_HEARTBEAT_SUCCEEDED ) ;
443
447
// connect all known servers, then attempt server selection to connect
444
448
const serverDescriptions = Array . from ( this . s . description . servers . values ( ) ) ;
445
449
this . s . servers = new Map (
@@ -486,6 +490,17 @@ export class Topology extends TypedEventEmitter<TopologyEvents> {
486
490
this . emit ( Topology . CONNECT , this ) ;
487
491
488
492
return this ;
493
+ } else if ( ! this . s . options . loadBalanced ) {
494
+ try {
495
+ await Promise . race ( [
496
+ Timeout . expires ( this . client . s . options . serverSelectionTimeoutMS ) ,
497
+ heartbeatWaitCond
498
+ ] ) ;
499
+ } catch ( error ) {
500
+ throw new MongoNetworkTimeoutError (
501
+ `Failed to contact server after ${ this . client . s . options . serverSelectionTimeoutMS } ms`
502
+ ) ;
503
+ }
489
504
}
490
505
491
506
stateTransition ( this , STATE_CONNECTED ) ;
You can’t perform that action at this time.
0 commit comments