mobc

A generic connection pool with async/await support.

Build Status crates.io

Documentation

Changelog

Note: mobc requires at least Rust 1.39.

Features

Usage

toml [dependencies] mobc = "0.4"

Example

Using an imaginary "foodb" database.

```rust use mobc::{Manager, Pool, ResultFuture};

[derive(Debug)]

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 { Box::pin(futures::future::ok(FooConnection)) }

fn check(&self, conn: Self::Connection) -> ResultFuture { Box::pin(futures::future::ok(conn)) } }

[tokio::main]

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