A generic connection pool with async/await support.
Note: mobc requires at least Rust 1.39.
tokio
and async-std
runtimes.toml
[dependencies]
mobc = "0.4"
Using an imaginary "foodb" database.
```rust use mobc::{Manager, Pool, ResultFuture};
struct FooError;
struct FooConnection;
impl FooConnection { async fn query(&self) -> String { "nori".to_string() } }
struct FooManager;
impl Manager for FooManager { type Connection = FooConnection; type Error = FooError;
fn connect(&self) -> ResultFuture
fn check(&self, conn: Self::Connection) -> ResultFuture
async fn main() { let pool = Pool::builder().max_open(15).build(FooManager); let num: usize = 10000; let (tx, mut rx) = tokio::sync::mpsc::channel::<()>(16);
for _ in 0..num { let pool = pool.clone(); let mut tx = tx.clone(); tokio::spawn(async move { let conn = pool.get().await.unwrap(); let name = conn.query().await; asserteq!(name, "nori".tostring()); tx.send(()).await.unwrap(); }); }
for _ in 0..num { rx.recv().await.unwrap(); } } ```