11 个版本

0.3.3 2020年9月14日
0.3.2 2020年9月14日
0.3.1 2020年8月5日
0.3.0 2020年3月4日
0.1.4 2019年5月23日

#646异步

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 次下载
用于 c-lightning-http-plugin

MIT 许可证

14KB
278

Lazy Async Pool

此 crate 定义了一个在 futures 上操作的对象池,并能够动态生成对象。

现已更新支持 async/await

使用方法

将以下内容添加到您的 Cargo.toml

[dependencies]
lazy_async_pool = "0.3.3"

功能

timeout:设置超时时间,在此之后即使池已满,也会生成新的对象。

示例:数据库连接池

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

依赖项

~1MB
~21K SLoC