mobc
r2d2 with async/await
Documentation
implementation
-
mobc_redis
-
mobc_postgres
Note: mobc requires at least Rust 1.39.
Usage
use tokio;
extern crate mobc;
extern crate mobc_foodb;
#[tokio::main]
async fn main() {
let manager = mobc_foodb::FooConnectionManager::new("localhost:1234");
let pool = mobc::Pool::builder()
.max_size(15)
.build(manager)
.await
.unwrap();
for _ in 0..20 {
let pool = pool.clone();
tokio::spawn(async {
let conn = pool.get().await.unwrap();
});
}
}
DIY
You can easily customize your own connection pool with the following trait ConnectionManager
.
pub type AnyFuture<T, E> = Pin<Box<dyn Future<Output = Result<T, E>> + Send>>;
pub trait ConnectionManager: Send + Sync + 'static {
type Connection: Send + 'static;
type Error: error::Error + Send + Sync + 'static;
type Executor: Executor + Send + Sync + 'static + Clone;
fn get_executor(&self) -> Self::Executor;
fn connect(&self) -> AnyFuture<Self::Connection, Self::Error>;
fn is_valid(&self, conn: Self::Connection) -> AnyFuture<Self::Connection, Self::Error>;
fn has_broken(&self, conn: &mut Option<Self::Connection>) -> bool;
}
benchmark
The benchmark is not ready