Deadpool is a dead simple async pool for connections and objects of any type.
This crate implements a deadpool
manager for tokio-postgres
and also provides a statement cache by wrapping tokio_postgres::Client
and tokio_postgres::Transaction.
| Feature | Description | Extra dependencies | Default |
| ------- | ----------- | ------------------ | ------- |
| config | Enable support for config crate | config, serde/derive | yes |
| rt_tokio_1 | Enable support for tokio crate | deadpool/rt_tokio_1 | yes |
| rt_async-std_1 | Enable support for async-std crate | deadpool/rt_async-std_1 | no |
Important: async-std support is currently limited to the
async-std specific timeout function. You still need to enable
the tokio1 feature of async-std in order to use this crate
with async-std.
```rust,ignore use deadpoolpostgres::{Config, Manager, ManagerConfig, Pool, RecyclingMethod}; use tokiopostgres::NoTls;
async fn main() { let mut cfg = Config::new(); cfg.dbname = Some("deadpool".tostring()); cfg.manager = Some(ManagerConfig { recyclingmethod: RecyclingMethod::Fast }); let pool = cfg.createpool(NoTls).unwrap(); for i in 1..10 { let mut client = pool.get().await.unwrap(); let stmt = client.preparecached("SELECT 1 + $1").await.unwrap(); let rows = client.query(&stmt, &[&i]).await.unwrap(); let value: i32 = rows[0].get(0); assert_eq!(value, i + 1); } } ```
config and dotenv crate```env
PG__DBNAME=deadpool ```
```rust use deadpoolpostgres::{Manager, Pool}; use dotenv::dotenv; use serde::Deserialize; use tokiopostgres::NoTls;
struct Config { pg: deadpool_postgres::Config }
impl Config {
pub fn fromenv() -> Result
async fn main() { dotenv().ok(); let mut cfg = Config::fromenv().unwrap(); let pool = cfg.pg.createpool(NoTls).unwrap(); for i in 1..10 { let mut client = pool.get().await.unwrap(); let stmt = client.preparecached("SELECT 1 + $1").await.unwrap(); let rows = client.query(&stmt, &[&i]).await.unwrap(); let value: i32 = rows[0].get(0); asserteq!(value, i + 1); } } ```
Note: The code above uses the crate name config_crate because of the
config feature and both features and dependencies share the same namespace.
In your own code you will probably want to use ::config::ConfigError and
::config::Config instead.
tokio_postgres::Config object```rust,ignore use std::env; use deadpoolpostgres::{Manager, ManagerConfig, Pool, RecyclingMethod}; use tokiopostgres::NoTls;
async fn main() { let mut pgconfig = tokiopostgres::Config::new(); pgconfig.hostpath("/run/postgresql"); pgconfig.hostpath("/tmp"); pgconfig.user(env::var("USER").unwrap().asstr()); pgconfig.dbname("deadpool"); let mgrconfig = ManagerConfig { recyclingmethod: RecyclingMethod::Fast }; let mgr = Manager::fromconfig(pgconfig, NoTls, mgrconfig); let pool = Pool::new(mgr, 16); for i in 1..10 { let mut client = pool.get().await.unwrap(); let stmt = client.preparecached("SELECT 1 + $1").await.unwrap(); let rows = client.query(&stmt, &[&i]).await.unwrap(); let value: i32 = rows[0].get(0); asserteq!(value, i + 1); } } ```
The database is unreachable. Why does the pool creation not fail?
Deadpool has identical startup and runtime behaviour and therefore the pool creation will never fail.
If you want your application to crash on startup if no database
connection can be established just call pool.get().await right after
creating the pool.
Why are connections retrieved from the pool sometimes unuseable?
In deadpool-postgres 0.5.5 a new recycling method was implemented which
is the default since 0.8. With that recycling method the manager no
longer performs a test query prior returning the connection but relies
solely on tokio_postgres::Client::is_closed instead. Under some rare
circumstances (e.g. unreliable networks) this can lead to tokio_postgres
not noticing a disconnect and reporting the connection as useable.
The old and slightly slower recycling method can be enabled by setting
ManagerConfig::recycling_method to RecyclingMethod::Verified or when
using the config crate by setting PG__MANAGER__RECYCLING_METHOD=Verified.
How can I enable features of the tokio-postgres crate?
Make sure that you depend on the same version of tokio-postgres as
deadpool-postgres does and enable the needed features in your own
Crate.toml file:
toml
[dependencies]
deadpool-postgres = { version = "0.7" }
tokio-postgres = { version = "0.7", features = ["with-uuid-0_8"] }
Important: The version numbers of deadpool-postgres and
tokio-postgres do not necessarily match. It is just a coincidence
that both crates have the same MAJOR and MINOR version number at the
time of this writing.
How can I clear the statement cache?
You can call pool.manager().statement_cache.clear() to clear all
statement caches or pool.manager().statement_cache.clear() to remove
a single statement from all caches.
Important: The ClientWrapper also provides a statement_cache
field which has clear() and remove() methods which only affect
a single client.
Licensed under either of
at your option.