Crate deadpool_r2d2[][src]

Expand description

Deadpool for R2D2 Managers Latest Version Unsafe forbidden Rust 1.54+

Deadpool is a dead simple async pool for connections and objects of any type.

This crate implements a deadpool manager for r2d2 managers.

Features

FeatureDescriptionExtra dependenciesDefault
rt_tokio_1Enable support for tokio cratedeadpool/rt_tokio_1yes
rt_async-std_1Enable support for async-std cratedeadpool/rt_async-std_1no
serdeEnable support for serde cratedeadpool/serdeno

Example

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_path("/run/postgresql");
    pg_config.user(&env::var("USER").unwrap());
    pg_config.dbname("deadpool");
    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(())
}

License

Licensed under either of

at your option.

Structs

Statistics regarding an object returned by the pool

Generic object and connection pool.

Pool configuration.

The current pool status.

This guard is returned when calling SyncWrapper::lock or SyncWrapper::try_lock. This is basicly just a wrapper around a MutexGuard but hides some implementation details.

Wrapper for objects which only provides blocking functions that need to be called on a separate thread.

Timeouts when getting Objects from a Pool.

Enums

Possible errors returned when SyncWrapper::interact() fails.

Possible errors returned by Pool::get() method.

Enumeration for picking a runtime implementation.