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

Method createConnection

src/cmap/connection_pool.ts:603–685  ·  view source on GitHub ↗
(callback: Callback<Connection>)

Source from the content-addressed store, hash-verified

601 }
602
603 private createConnection(callback: Callback<Connection>) {
604 // Note that metadata may have changed on the client but have
605 // been frozen here, so we pull the metadata promise always from the client
606 // no matter what options were set at the construction of the pool.
607 const connectOptions: ConnectionOptions = {
608 ...this.options,
609 id: this.connectionCounter.next().value,
610 generation: this.generation,
611 cancellationToken: this.cancellationToken,
612 mongoLogger: this.mongoLogger,
613 authProviders: this.server.topology.client.s.authProviders,
614 metadata: this.server.topology.client.options.metadata
615 };
616
617 this.pending++;
618 // This is our version of a "virtual" no-I/O connection as the spec requires
619 const connectionCreatedTime = processTimeMS();
620 this.emitAndLog(
621 ConnectionPool.CONNECTION_CREATED,
622 new ConnectionCreatedEvent(this, { id: connectOptions.id })
623 );
624
625 connect(connectOptions).then(
626 connection => {
627 // The pool might have closed since we started trying to create a connection
628 if (this.poolState !== PoolState.ready) {
629 this.pending--;
630 connection.destroy();
631 callback(this.closed ? new PoolClosedError(this) : new PoolClearedError(this));
632 return;
633 }
634
635 // forward all events from the connection to the pool
636 for (const event of [...APM_EVENTS, Connection.CLUSTER_TIME_RECEIVED]) {
637 connection.on(event, (e: any) => this.emit(event, e));
638 }
639
640 if (this.loadBalanced) {
641 connection.on(Connection.PINNED, pinType => this.metrics.markPinned(pinType));
642 connection.on(Connection.UNPINNED, pinType => this.metrics.markUnpinned(pinType));
643
644 const serviceId = connection.serviceId;
645 if (serviceId) {
646 let generation;
647 const sid = serviceId.toHexString();
648 if ((generation = this.serviceGenerations.get(sid))) {
649 connection.generation = generation;
650 } else {
651 this.serviceGenerations.set(sid, 0);
652 connection.generation = 0;
653 }
654 }
655 }
656
657 connection.markAvailable();
658 this.emitAndLog(
659 ConnectionPool.CONNECTION_READY,
660 new ConnectionReadyEvent(this, connection, connectionCreatedTime)

Callers 5

ensureMinPoolSizeMethod · 0.95
processWaitQueueMethod · 0.95
makeSocketFunction · 0.80
makeSocks5ConnectionFunction · 0.80
kmsRequestMethod · 0.80

Calls 14

processTimeMSFunction · 0.90
connectFunction · 0.90
emitAndLogMethod · 0.80
onMethod · 0.80
markPinnedMethod · 0.80
markUnpinnedMethod · 0.80
setMethod · 0.80
markAvailableMethod · 0.80
emitMethod · 0.65
callbackFunction · 0.50
nextMethod · 0.45
destroyMethod · 0.45

Tested by

no test coverage detected