Keeping Connections with redis-rs in a Single Threaded Server

I have a single threaded, non-async server, that is connecting to a Redis database using the officially recommended redis-rs crate. As far as I can see there is no mechanism to achieve, what I need: holding a connection as long as it exists even over multiple requests to my server.

Also a single request to my server might start multiple Redis requests. Between all of these requests the redis connection could be interrupted and I'd really like to start at least one try to reconnect before failing the request.

What would a good way to fulfill my requirements? The best I can think of is writing a wrapper around the redis-rs objects myself. Is there a better way?

It sounds like you are looking for a connection pool? There's r2d2 for that.

Thanks for your answer. Yes, I can't deny, that it sound like a solution for me. But I am still struggling with the concept for my particular application:

  1. There will only be one connection per process. That probably means I should set the pool size to 1?
  2. If I want to recover from broken connections (even during a single request to my server), I could use r2d2s test_on_check_out and check out a new connection before each request. But this would issue a PING before each request which is pretty bad for performance.
  3. In this case I would pay a (probably small) penalty for synchronization, when I am absolutely single threaded.

Especially regarding the recovery from a broken connection, what I have in mind is something like this: I always reuse a connection, but when it fails with an IoError, I try to reestablish it before I give up on this request.

I don't have a lot of knowledge about databases, but this seems to me like there should be ready and proven solutions for that. Maybe it is even in r2d2, but I was not able to find it now.

If someone is searching for the same thing: I ended up implementing a very small and simple object that contains the Client and Connection objects. For this I implemented the ConnectionLike trait, which made all the Redis commands available.

This really composes nicely and allows full control over the error handling and retries.