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 在 异步
每月 449 次下载
用于 c-lightning-http-plugin
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