The asynchronous connection pool implementation in Rust


#1

I have a Tokio TCP back-end application, which, briefly, after receiving a request, reads something from Redis, writes something to PostgreSQL, uploads something via HTTP, sends something to RabbitMQ etc. Processing each request takes a lot of time, so a separate task for each request is created. As sharing connections is impossible in asynchronous models, some connection pooling is required. For now, new connections are established on each request, and it is extremely excessive.

I have been looking for an asynchronous connection pool implementation in Rust, but have not found any of them up to date.

I would like to hear some advice on how to implement it myself.

The only idea I have come up with is:

  1. Implement a Stream/Sink object with an inner collection of connections. It does not matter whether it is LIFO or FIFO, since the connections are identical. On the application startup, N connections are allocated.
  2. Now I am not sure if it is possible to share such a pool among tasks, but if it were possible, tasks would poll the stream for a connection instance (instead of establishing their own one), use it, and then put back.
  3. If there were no connections available, the stream might establish more of them or ask the task to hang on (depending on its configuration).
  4. If a connection fails, it gets dropped and the pool now contains N-1 connections, so it may decide to allocate a new one on the next request.

So I have two problems I cannot find proper answers anywhere:

  1. Must/can/should I share the stream/sink-pool among tasks in some way? Anyway, I see some Shared futures in the futures crate.
  2. There are some gloomy points in the tokio/futures tutorial. E.g. it does not explain how do I notify the uppermost task, that is, how do I implement the mythical innermost future, which does not pool anything itself, but still has to notify the upper futures.

Or is my approach completely wrong? I could start playing with it by myself, but I have a strong suspicion that I have missed something, e.g. a one-click solution.


#2

I asked in the past if there was something like r2d2 but for futures and I got this reply:

but I’m also interested in how an async pool would work.


#3

This isn’t true. The reqwest::async::Client is a connection pool that can be shared across threads / asynchronously.


#4

Well, that’s what I am saying - we can (I used to have doubts about) share connection pools, but not connections themselves.