You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/mongo_client.ts
+16-15
Original file line number
Diff line number
Diff line change
@@ -656,45 +656,46 @@ export class MongoClient extends TypedEventEmitter<MongoClientEvents> implements
656
656
/**
657
657
* Cleans up resources managed by the MongoClient.
658
658
*
659
-
* The close method clears and closes all resources that client is responsible for.
659
+
* The close method clears and closes all resources that the client is responsible for.
660
660
* Please refer to the `MongoClient` class documentation for a breakdown of resource responsibilities.
661
661
*
662
-
* **However,** the close method should not serve as a replacement for clean up of all created resources.
662
+
* **However,** the close method is not a replacement for clean up of explicitly created resources.
663
663
* This method is written as a "best effort" attempt to leave behind the least amount of resources server-side when possible.
664
664
*
665
-
* The following list defines ideal preconditions and pitfalls if they are not met.
665
+
* The following list defines ideal preconditions and consequent pitfalls if they are not met.
666
666
*
667
667
* The close method performs the following in the order listed:
668
668
* - Client-side:
669
669
* - **Close in-use connections**: Any connections that are currently waiting on a response from the server will be closed.
670
-
* This is performed _first_ to avoid reaching the next step (server-side clean up) and having no available connections to checkout.
671
-
* - _Ideal_: All operations have been awaited or cancelled, the outcome regardless of success or failure have been processed before closing the client servicing the operation.
672
-
* - _Pitfall_: If all connections are in-use when client.close is called then no connections will remain for re-use for the next steps.
670
+
* This is performed _first_ to avoid reaching the next step (server-side clean up) and having no available connections to check out.
671
+
* - _Ideal_: All operations have been awaited or cancelled, and the outcomes, regardless of success or failure, have been processed before closing the client servicing the operation.
672
+
* - _Pitfall_: When `client.close()` is called and all connections are inuse, after closing them, the client must create new connections for cleanup operations, which comes at the cost of new TLS/TCP handshakes and authentication steps.
673
673
* - Server-side:
674
-
* - **Close active cursors**: All cursors that haven't been completed will have a `killCursor` operation sent to the server they were initialized on, freeing the serverside resource.
675
-
* - _Ideal_: Cursors are closed before MongoClient close is called.
674
+
* - **Close active cursors**: All cursors that haven't been completed will have a `killCursor` operation sent to the server they were initialized on, freeing the server-side resource.
675
+
* - _Ideal_: Cursors are explicitly closed or completed before `client.close()` is called.
676
676
* - _Pitfall_: `killCursors` may have to build a new connection if the in-use closure ended all pooled connections.
677
-
* - **End active sessions**: Sessions created with `client.startSession()` or `client.withSession()` will have their `.endSession()` method called.
678
-
* Contrary to the name of the method `endSession()` returns the session to the client's pool of sessions rather than inform the server.
679
-
* - _Ideal_: Transactions are settled and sessions are ended before `client.close()`
677
+
* - **End active sessions**: In-use sessions created with `client.startSession()` or `client.withSession()` or implicitly by the driver will have their `.endSession()` method called.
678
+
* Contrary to the name of the method, `endSession()` returns the session to the client's pool of sessions rather than end them on the server.
679
+
* - _Ideal_: Transaction outcomes are awaited and their corresponding explcit session is ended before `client.close()` is called.
680
680
* - _Pitfall_: **This step aborts in-progress transactions**. It is advisable to observe the outcome of a transaction before closing your client.
681
-
* - **End all pooled sessions**: The `endSessions` command with all session IDs the client has pooled are sent to the server to inform the cluster it can clean them up.
682
-
* - _Ideal_: No user intervention is expected, session pooling is intended to be transparent along with their clean up.
681
+
* - **End all pooled sessions**: The `endSessions` command with all session IDs the client has pooled is sent to the server to inform the cluster it can clean them up.
682
+
* - _Ideal_: No user intervention is expected.
683
683
* - _Pitfall_: None.
684
684
*
685
685
* The remaining shutdown is of the MongoClient resources that are intended to be entirely internal but is documented here as their existence relates to the JS event loop.
686
686
*
687
687
* - Client-side (again):
688
688
* - **Stop all server monitoring**: Connections kept live for detecting cluster changes and roundtrip time measurements are shutdown.
689
-
* - **Close all pooled connections**: Each server node in the cluster has a corresponding connection pool and all connections in the pool are closed. Any operations waiting to checkout will have an error thrown instead of a connection returned.
689
+
* - **Close all pooled connections**: Each server node in the cluster has a corresponding connection pool and all connections in the pool are closed. Any operations waiting to check out a connection will have an error thrown instead of a connection returned.
690
690
* - **Clear out server selection queue**: Any operations that are in the process of waiting for a server to be selected will have an error thrown instead of a server returned.
691
-
* - **Close encryption-related resources**: An internal MongoClient created for communicating with `mongocryptd` or other encryption purposes is closed. (using this same method of course!)
691
+
* - **Close encryption-related resources**: An internal MongoClient created for communicating with `mongocryptd` or other encryption purposes is closed. (Using this same method of course!)
692
692
*
693
693
* After the close method completes there should be no MongoClient related resources [ref-ed in Node.js' event loop](https://docs.libuv.org/en/v1.x/handle.html#reference-counting).
694
694
* This should allow Node.js to exit gracefully if MongoClient resources were the only active handles in the event loop.
695
695
*
696
696
* @param _force - currently an unused flag that has no effect. Defaults to `false`.
0 commit comments