[CSHARP-1947] Subscription to cluster events leads to new connection pool created Created: 19/Mar/17  Updated: 27/Oct/23  Resolved: 28/Apr/17

Status: Closed
Project: C# Driver
Component/s: Configuration
Affects Version/s: 2.4.2
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: sergii Assignee: Robert Stam
Resolution: Works as Designed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

I am working on Web API project that should be hosted on azure environment. I would like to log commands execution time and details per web request. To make it possible I subscribe to CommandStartedEvent, CommandSucceededEvent and CommandFailedEvent events that give me all necessary information. But as I should link mongo command to web request I have to create new MongoClient per web request and subscribe to events for every new MongoClient. As result mongo driver create new connection pool for every new MongoClient. On Azure environment amount of opened tcp connections is limited so my application start to response with mongo timeout exception just after few second being under load.
After digging into source code of mongo driver I found that clusters are cached in ClusterRegistry based on ClusterKey. (internal ICluster GetOrCreateCluster(ClusterKey clusterKey)). But overloaded of method Equals of ClusterKey compares references of Actions (object.ReferenceEquals(_clusterConfigurator, rhs._clusterConfigurator)) what is almost impossible to achieve under real circumstances.
I would propose to move events subscription to MongoClient level so different clients would be able to share single MongoCluster but still receive events notification separately



 Comments   
Comment by Robert Stam [ 28/Apr/17 ]

You would have to use some mechanism outside of the driver to correlate driver events to web requests.

You could look into .NET classes like:

  • AsyncLocal<T>
  • HttpContext

to find ways of correlating driver events to web requests.

Comment by sergii [ 20/Mar/17 ]

Thank you for response.
It is not a problem to have a single MongoClient, this is what I had to do as temporary solution. But my goal is to track all mongo commands that were generated by application server grouped by scope of web request.
For instance:
End point should create comment for a post. To create comment my application should get:

  • information about current user from DB
  • information about post from DB
  • save comment in DB
    As result for every request to this endpoint my application will send 3 commands to MongoDB. I would like to log this commands with reference to web request. So I create new MongoClient per web request and subscribe to cluster events what leads me to enormous amount of connections pool.
    Is it possible to achieve the same but using one instance of MongoClient?
Comment by Robert Stam [ 20/Mar/17 ]

The intent is that all MongoClient instances that have the same settings should share a single underlying connection pool.

For some data types determining whether two values are equal is difficult, as is the case with the ClusterConfigurator property of MongoClientSettings. This can lead to multiple connection pools being created when only one is desired.

Your simplest approach is to only create a single instance of MongoClient and use this single instance for all your web requests. MongoClient is thread-safe and can be safely used by multiple web requests at the same time.

Generated at Wed Feb 07 21:41:09 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.