The right was to create mongo connections?


#1

I am using the mongo crate at https://crates.io/crates/mongodb.
In my application, I have multiple functions that need a mongo connection. The functions run in their own thread and create their own mongo connection.
I create a mongo connection for main(), then call spawn threads that call those functions.Looking at the mongo stats, I end up with > 1000 connections.

I have looked at the documentation for mongodb crate and I didn’t find any info on performance.

Does anyone here have experience with similar situation?
thanks


#2

mongodb::Client type is an Arc wrapping the inner ClientInner type, and the ClientInner is Sync, so Client is Send and Clone. So, there’s no problem in just sending the clone of the main connection to all the threads - it will use one physical connection to database, and this physical connection will be closed once all the threads drop their clones.


#3

Yea, you’ll definitely want to just clone the initial connection. If you are only using one database, then you can just clone the database instance and pass that around instead of the client.