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

Method _close

src/mongo_client.ts:758–822  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

756
757 /* @internal */
758 private async _close(): Promise<void> {
759 // There's no way to set hasBeenClosed back to false
760 Object.defineProperty(this.s, 'hasBeenClosed', {
761 value: true,
762 enumerable: true,
763 configurable: false,
764 writable: false
765 });
766
767 this.topology?.closeCheckedOutConnections();
768
769 const activeCursorCloses = Array.from(this.s.activeCursors, cursor => cursor.close());
770 this.s.activeCursors.clear();
771
772 await Promise.all(activeCursorCloses);
773
774 const activeSessionEnds = Array.from(this.s.activeSessions, session => session.endSession());
775 this.s.activeSessions.clear();
776
777 await Promise.all(activeSessionEnds);
778
779 if (this.topology == null) {
780 return;
781 }
782
783 const supportsSessions =
784 this.topology.description.type === TopologyType.LoadBalanced ||
785 this.topology.description.logicalSessionTimeoutMinutes != null;
786
787 if (supportsSessions) {
788 await endSessions(this, this.topology);
789 }
790
791 // clear out references to old topology
792 const topology = this.topology;
793 this.topology = undefined;
794
795 topology.close();
796
797 const { encrypter } = this.options;
798 if (encrypter) {
799 await encrypter.close(this);
800 }
801
802 async function endSessions(
803 client: MongoClient,
804 { description: topologyDescription }: Topology
805 ) {
806 // If we would attempt to select a server and get nothing back we short circuit
807 // to avoid the server selection timeout.
808 const selector = readPreferenceServerSelector(ReadPreference.primaryPreferred);
809 const serverDescriptions = Array.from(topologyDescription.servers.values());
810 const servers = selector(topologyDescription, serverDescriptions, new DeprioritizedServers());
811 if (servers.length !== 0) {
812 const endSessions = Array.from(client.s.sessionPool.sessions, ({ id }) => id);
813 if (endSessions.length !== 0) {
814 try {
815 await executeOperation(client, new EndSessionsOperation(endSessions));

Callers 1

closeMethod · 0.95

Calls 4

endSessionMethod · 0.80
closeMethod · 0.45
clearMethod · 0.45

Tested by

no test coverage detected