#async-pool #object-pool #resources #lazy-evaluation #fly #generate #future

lazy_async_pool

An asyncronous object pool that generates objects on the fly

11 releases

0.3.3 Sep 14, 2020
0.3.2 Sep 14, 2020
0.3.1 Aug 5, 2020
0.3.0 Mar 4, 2020
0.1.4 May 23, 2019

#646 in Asynchronous

Download history 64/week @ 2024-03-11 64/week @ 2024-03-18 103/week @ 2024-03-25 134/week @ 2024-04-01 66/week @ 2024-04-08 92/week @ 2024-04-15 96/week @ 2024-04-22 33/week @ 2024-04-29 50/week @ 2024-05-06 98/week @ 2024-05-13 26/week @ 2024-05-20 68/week @ 2024-05-27 110/week @ 2024-06-03 109/week @ 2024-06-10 135/week @ 2024-06-17 93/week @ 2024-06-24

449 downloads per month
Used in c-lightning-http-plugin

MIT license

14KB
278 lines

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()
}

Dependencies

~1MB
~21K SLoC