Crates.io | deadpool-r2d2 |
lib.rs | deadpool-r2d2 |
version | 0.4.1 |
source | src |
created_at | 2021-10-18 15:14:29.735348 |
updated_at | 2024-05-04 09:27:21.527404 |
description | Dead simple async pool for r2d2 managers |
homepage | |
repository | https://github.com/bikeshedder/deadpool |
max_upload_size | |
id | 466830 |
size | 22,495 |
Deadpool is a dead simple async pool for connections and objects of any type.
This crate implements a deadpool
manager for r2d2
managers.
Feature | Description | Extra dependencies | Default |
---|---|---|---|
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 |
serde |
Enable support for serde crate | deadpool/serde |
no |
tracing |
Enable support for tracing by propagating Spans in the interact() calls. Enable this if you use the tracing crate and you want to get useful traces from within interact() calls. |
deadpool-sync/tracing , tracing |
no |
use std::env;
use deadpool_r2d2::Runtime;
use r2d2_postgres::postgres::Error as PgError;
type PgManager = deadpool_r2d2::Manager<
r2d2_postgres::PostgresConnectionManager<r2d2_postgres::postgres::NoTls>,
>;
type PgPool = deadpool_r2d2::Pool<PgManager>;
fn create_pool(max_size: usize) -> PgPool {
let mut pg_config = r2d2_postgres::postgres::Config::new();
pg_config.host(&env::var("PG__HOST").unwrap());
pg_config.user(&env::var("PG__USER").unwrap());
pg_config.password(&env::var("PG__PASSWORD").unwrap());
pg_config.dbname(&env::var("PG__DBNAME").unwrap());
let r2d2_manager =
r2d2_postgres::PostgresConnectionManager::new(pg_config, r2d2_postgres::postgres::NoTls);
let manager = PgManager::new(r2d2_manager, Runtime::Tokio1);
let pool = PgPool::builder(manager)
.max_size(max_size)
.build()
.unwrap();
pool
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let pool = create_pool(2);
let client = pool.get().await.unwrap();
let answer: i32 = client
.interact(|client| client.query_one("SELECT 42", &[]).map(|row| row.get(0)))
.await??;
assert_eq!(answer, 42);
Ok(())
}
Licensed under either of
at your option.