[CSHARP-4064] Connection TCP ports are not free'd Created: 17/Feb/22 Updated: 27/Oct/23 Resolved: 09/Sep/22 |
|
| Status: | Closed |
| Project: | C# Driver |
| Component/s: | None |
| Affects Version/s: | 2.13.3 |
| Fix Version/s: | None |
| Type: | Question | Priority: | Major - P3 |
| Reporter: | Dennis Hoefakker | Assignee: | James Kovacs |
| Resolution: | Gone away | Votes: | 0 |
| Labels: | None | ||
| Remaining Estimate: | Not Specified | ||
| Time Spent: | Not Specified | ||
| Original Estimate: | Not Specified | ||
| Attachments: |
|
| Description |
| Comments |
| Comment by PM Bot [ 09/Sep/22 ] | |||||||||||
|
There hasn't been any recent activity on this ticket, so we're resolving it. Thanks for reaching out! Please feel free to comment on this if you're able to provide more information. | |||||||||||
| Comment by James Kovacs [ 25/Aug/22 ] | |||||||||||
|
Hi, dennis@valueblue.nl, Thank you for the additional details regarding your multi-tenant setup. As you note, connections are pooled based on the cluster key, which includes the authenticating user. Connection establishment involves many steps including TCP socket setup, TLS handshake, and authentication. Authentication is relatively expensive by design (involving many rounds of credential hashing) to discourage brute force attacks. Thus pooling connections irregardless of authenticating user and re-authenticating before every operation would increase the operation time for every MongoDB operation. Regarding the monitoring threads used by the driver. MongoDB implemented streaming heartbeats in MongoDB 4.4 to reduce failover times during elections. This was implemented by the driver in 2.11 ( We are planning to make MongoClient disposable in the 3.0.0 release (CSHARP-3431) but that release is a ways off and plans/priorities may change. A truly disposable MongoClient is likely the best solution for a multi-tenant situation such as yours as all resources associated with the MongoClient will be properly cleaned up and you can implement some sort of MongoClient caching mechanism based on your tenants' usage statistics. In the meantime we do use a disposable MongoClient in our tests. See DisposableMongoClient in MongoDB.Driver.TestHelpers. I wouldn't recommend using this class directly, but more as an example of how the Dispose method cleans up resources - notably ClusterRegistry.Instance.UnregisterAndDisposeCluster(mongoClient.Cluster). I will admit that this method was designed with testing purposes in mind and not production. I would strongly recommend testing appropriately if you are going to use it in your multi-tenant application. Hopefully this provides you with some guidance and ideas of how to address this issue both now and in the future. Sincerely, | |||||||||||
| Comment by Dennis Hoefakker [ 17/Aug/22 ] | |||||||||||
|
Hi @Boris, Sorry for the late reply, this issue slipped, also caused we reverted back to driver 2.10.3. You remark for "MongoClient per cluster" is something we already do. But our multi-tenant setup enforces us to have a user/password per database. And since the MongoClient is unique per server/database/user/password this causes a huge connection pool(s). Is there a way to tell the new driver to use one connection, or tell the driver to free connections to the OS quicker. I also have an other bug CSHAP-3250 which explains. | |||||||||||
| Comment by PM Bot [ 22/Mar/22 ] | |||||||||||
|
There hasn't been any recent activity on this ticket, so we're resolving it. Thanks for reaching out! Please feel free to comment on this if you're able to provide more information. | |||||||||||
| Comment by Boris Dogadov [ 04/Mar/22 ] | |||||||||||
|
Hello dennis@valueblue.nl The described behavior is expected. Each MongoClient maintains two open connections for each server, in addition to the connections used by operations. This change was introduced in 2.11 CSHARP-2964, while in previous versions MongoClient maintained one connection per server. For better performance, it’s highly recommended having a single MongoClient per cluster across your application. | |||||||||||
| Comment by Boris Dogadov [ 02/Mar/22 ] | |||||||||||
|
Thank you dennis@valueblue.nl, | |||||||||||
| Comment by Dennis Hoefakker [ 02/Mar/22 ] | |||||||||||
|
Hi,
Sorry for the delay but i enclosed a simple example. I cleaned the solution's packages folder so you need to restore nuget packages and other refs using the following command:
When running the project, supply the connection string in the class "TenantMongoClientLocator" I didn't bother putting it in config files trigger a DocumentCount using the request:
What you will notice (after triggering the command above )using currPorts is that running 2.10.3 you see 2 connections to the db server, and after a while it's reduces to 1. When running the 2.13.3 you'll see 3 connections to the db server reducing to 2. Which is double open connections. As stated before we are using multiple databases per customer with own credentials, running multiple microservices on a VM, where the micro's can have connections to the database. | |||||||||||
| Comment by Dennis Hoefakker [ 25/Feb/22 ] | |||||||||||
|
Yes i'll set up an example, was a bit busy this week but will work on it next week! | |||||||||||
| Comment by Boris Dogadov [ 18/Feb/22 ] | |||||||||||
|
Hi dennis@valueblue.nl , thank you for your bug report. I failed to reproduce this issue with the following code:
I have tried 2.13.3 and 2.14.1 drivers, with various heartbeat intervals, max connection lifetimes and pool sizes. Running the sample for an hour, I can see that the connections are established and freed as expected via CurrPorts . To investigate further it would be helpful to receive a self-contained sample code which reproduces the issue. Thanks. |