MCPcopy
hub / github.com/mongodb/node-mongodb-native / _connect

Method _connect

src/sdam/topology.ts:409–491  ·  view source on GitHub ↗
(options?: ConnectOptions)

Source from the content-addressed store, hash-verified

407 }
408
409 private async _connect(options?: ConnectOptions): Promise<Topology> {
410 options = options ?? {};
411 if (this.s.state === STATE_CONNECTED) {
412 return this;
413 }
414
415 stateTransition(this, STATE_CONNECTING);
416
417 // emit SDAM monitoring events
418 this.emitAndLog(Topology.TOPOLOGY_OPENING, new TopologyOpeningEvent(this.s.id));
419
420 // emit an event for the topology change
421 this.emitAndLog(
422 Topology.TOPOLOGY_DESCRIPTION_CHANGED,
423 new TopologyDescriptionChangedEvent(
424 this.s.id,
425 new TopologyDescription(TopologyType.Unknown), // initial is always Unknown
426 this.s.description
427 )
428 );
429
430 // connect all known servers, then attempt server selection to connect
431 const serverDescriptions = Array.from(this.s.description.servers.values());
432 this.s.servers = new Map(
433 serverDescriptions.map(serverDescription => [
434 serverDescription.address,
435 createAndConnectServer(this, serverDescription)
436 ])
437 );
438
439 // In load balancer mode we need to fake a server description getting
440 // emitted from the monitor, since the monitor doesn't exist.
441 if (this.s.options.loadBalanced) {
442 for (const description of serverDescriptions) {
443 const newDescription = new ServerDescription(description.hostAddress, undefined, {
444 loadBalanced: this.s.options.loadBalanced
445 });
446 this.serverUpdateHandler(newDescription);
447 }
448 }
449
450 const serverSelectionTimeoutMS = this.client.s.options.serverSelectionTimeoutMS;
451 const readPreference = options.readPreference ?? ReadPreference.primary;
452 const timeoutContext = TimeoutContext.create({
453 // TODO(NODE-6448): auto-connect ignores timeoutMS; potential future feature
454 timeoutMS: undefined,
455 serverSelectionTimeoutMS,
456 waitQueueTimeoutMS: this.client.s.options.waitQueueTimeoutMS
457 });
458 const selectServerOptions = {
459 operationName: 'handshake',
460 ...options,
461 timeoutContext,
462 deprioritizedServers: new DeprioritizedServers()
463 };
464
465 try {
466 const server = await this.selectServer(

Callers 1

connectMethod · 0.95

Calls 10

serverUpdateHandlerMethod · 0.95
selectServerMethod · 0.95
closeMethod · 0.95
createAndConnectServerFunction · 0.85
emitAndLogMethod · 0.80
checkOutMethod · 0.80
checkInMethod · 0.80
emitMethod · 0.65
mapMethod · 0.45

Tested by

no test coverage detected