[][src]Crate deadpool

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

Example

use async_trait::async_trait;

#[derive(Debug)]
enum Error { Fail }

struct Connection {}

type Pool = deadpool::Pool<Connection, Error>;

impl Connection {
    async fn new() -> Result<Self, Error> {
        Ok(Connection {})
    }
    async fn check_health(&self) -> bool {
        true
    }
    async fn do_something(&self) -> String {
        "Horray!".to_string()
    }
}

struct Manager {}

#[async_trait]
impl deadpool::Manager<Connection, Error> for Manager
{
    async fn create(&self) -> Result<Connection, Error> {
        Connection::new().await
    }
    async fn recycle(&self, conn: Connection) -> Result<Connection, Error> {
        if conn.check_health().await {
            Ok(conn)
        } else {
            Connection::new().await
        }
    }
}

#[tokio::main]
async fn main() {
    let mgr = Manager {};
    let pool = Pool::new(mgr, 16);
    let mut conn = pool.get().await.unwrap();
    let value = conn.do_something().await;
    assert_eq!(value, "Horray!".to_string());
}

For a more complete example please see deadpool-postgres

Structs

Object

A wrapper around the actual pooled object which implements the traits Deref, DerefMut and Drop. Use this object just as it was of type T and upon leaving scope the drop function will take care of returning it to the pool.

Pool

A generic object and connection pool.

Traits

Manager

This trait is used to create new objects or recycle existing ones.