Share redis connection

Hi!
I have a simple web service with an endpoint (/foo). For each request on /foo I need to perform a redis request to get fresh data. I'm wondering the best way to handle the redis connection on the web service.

For the moment, I have a struct like this:

pub struct Deps {
  pub redis_conn: Mutex<redis::Connection>>
}

And when my service start, I wrap my deps with web::Data (Arc)

let deps = Deps::new();
let deps_data = web::Data::new(deps);
HttpServer::new(move || {
        App::new()
            .app_data(deps_data.clone())

And inside the endpoint when I need the redis connection:

let mut redis_conn = deps.redis_conn.lock().unwrap()

The point whish affraid me is that the lock of Mutex block the current thread.
What do you think? It's ok with this method or should I use the async connection of redis?

Typically you would want to use a connection pool. E.g. r2d2 for sync and bb8 for async.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.