Deadpool
Deadpool is a dead simple async pool for connections and objects of any type.
Backends
Deadpool supports various backends by implementing the deadpool::Manager
trait. The following backends are currently supported:
Backend | Crate |
---|---|
tokio-postgres | deadpool-postgres |
lapin (AMQP) | deadpool-lapin |
redis | deadpool-redis |
Example
use async_trait;
type Pool = Pool;
async
For a more complete example please see
deadpool-postgres
Reasons for yet another pool implementation
Deadpool is by no means the only pool implementation available. It does things a little different and that is the reason for it to exist:
-
Deadpool is compatible with any executor. Objects are returned to the pool using the
Drop
trait. The health of those objects is checked upon next retrieval and not when they are returned. Deadpool never performs any action in the background. This is the reason why deadpool does not need to spawn futures and does not rely on a background thread or task of any type. -
Identical startup and runtime behaviour. When writing long running application there usually should be no difference between startup and runtime if a database connection is temporarily not available. Nobody would expect an application to crash if the database becomes unavailable at runtime. So it should not crash on startup either. Creating the pool never fails and errors are only ever returned when calling
Pool::get()
.If you really want your application to crash on startup if objects can not be created on startup simply call
pool.get().expect("DB connection failed")
right after creating the pool. -
Deadpool is fast. The code which returns connections to the pool contains no blocking code and retrival uses only one locking primitive. Everything else is implemented using non-locking atomic counters.
-
Deadpool is simple. Dead simple. There is very little API surface and the actual code is barely 100 lines of code.
Differences to other pool implementations
-
r2d2
only provides a synchroneous interface. It also is more complex and needs a lot more code. -
mobc
provides anasync/.await
based interface and provides a lot more configuration options. The downside of this being added code complexity and the need for an executor whichdeadpool
does not need.
License
Licensed under either of
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.