lazy_async_pool

Crates.iolazy_async_pool
lib.rslazy_async_pool
version0.3.3
sourcesrc
created_at2019-05-23 21:26:17.507092
updated_at2020-09-14 23:04:46.8063
descriptionAn asyncronous object pool that generates objects on the fly.
homepage
repositoryhttps://github.com/DR-BoneZ/lazy-async-pool-rs
max_upload_size
id136502
size13,085
Aiden McClelland (dr-bonez)

documentation

http://docs.rs/lazy_async_pool

README

Lazy Async Pool

This crate defines an object pool that operates over futures, and generates objects on the fly.

now updated to support async/await

Usage

Add this to your Cargo.toml:

[dependencies]
lazy_async_pool = "0.3.3"

Features

timeout: set a timeout duration after which a new object will be generated even if the pool has no free resources and is at capacity.

Example: DB Connection Pool

use lazy_async_pool::Pool;

async fn test_fut() -> Result<(), failure::Error> {
    use futures::FutureExt;
    use futures::TryFutureExt;

    let pool = Pool::new(20, || {
        tokio_postgres::connect(
            "postgres://amcclelland:pass@localhost:5432/pgdb",
            tokio_postgres::NoTls,
        )
        .map_ok(|(client, connection)| {
            let connection = connection.map_err(|e| eprintln!("connection error: {}", e));
            tokio::spawn(connection);
            client
        })
        .boxed()
    });

    let client = pool.clone().get().await?;
    let stmt = client.prepare("SELECT $1::TEXT").await?;
    let rows = client.query(&stmt, &[&"hello".to_owned()]).await?;
    let hello: String = rows[0].get(0);
    println!("{}", hello);
    assert_eq!("hello", &hello);
    println!("len: {}", pool.len());
    assert_eq!(1, pool.len());
    Ok(())
}

fn main() {
    tokio::runtime::Runtime::new()
        .unwrap()
        .block_on(test_fut())
        .unwrap()
}
Commit count: 16

cargo fmt