Skip to content

Commit 2955c6b

Browse files
W-A-Jamesnbbeeken
authored andcommitted
add mechanism to throw error on unreachable server host name
1 parent edf63e4 commit 2955c6b

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

src/sdam/topology.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import {
2424
type MongoDriverError,
2525
MongoError,
2626
MongoErrorLabel,
27+
MongoNetworkTimeoutError,
2728
MongoOperationTimeoutError,
2829
MongoRuntimeError,
2930
MongoServerSelectionError,
@@ -44,6 +45,7 @@ import {
4445
makeStateMachine,
4546
now,
4647
ns,
48+
once,
4749
promiseWithResolvers,
4850
shuffle
4951
} from '../utils';
@@ -440,6 +442,8 @@ export class Topology extends TypedEventEmitter<TopologyEvents> {
440442
)
441443
);
442444

445+
// Create a wait condition that blocks until we get at least once successful heartbeat
446+
const heartbeatWaitCond = once(this, Server.SERVER_HEARTBEAT_SUCCEEDED);
443447
// connect all known servers, then attempt server selection to connect
444448
const serverDescriptions = Array.from(this.s.description.servers.values());
445449
this.s.servers = new Map(
@@ -486,6 +490,17 @@ export class Topology extends TypedEventEmitter<TopologyEvents> {
486490
this.emit(Topology.CONNECT, this);
487491

488492
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+
}
489504
}
490505

491506
stateTransition(this, STATE_CONNECTED);

0 commit comments

Comments
 (0)