(callback: Callback<Connection>)
| 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) |
no test coverage detected