MongoDb: How can I know if my connection is up or down?


#1

I’m using mongodb (https://crates.io/crates/mongodb). I know that when I call Client::connect(…), it doesn’t really connect to the database. The connection will be established only when needed. But how can I know if the connection is up or down ?

I saw https://docs.mongodb.com/manual/reference/command/ping/ saying that it exists a command “ping” to see if the server is reponding. But for now, here is how we try :

            let cmd = doc! { "ping": 1 };
            conn.command(cmd, CommandType::Suppressed, None).map_err(|e| {Error::Other(e.to_string())}).map( |_| {()})

The problem with that is the command itself, it comes back after 30 secondes. So I will know only after 30 secondes if it works or not… And since we wrapped mongodb under r2d2 crate to have a connection pool, it will run that for all connection in the pool so it is too long to know if the connection is up or not.

How can I know quickly if my mongodb connection is up or not ?
Thanks


#2

You can lower the timeouts (I am not sure who sets them, but they should be configurable).