[−][src]Crate mobc
A generic connection pool with async/await support.
Opening a new database connection every time one is needed is both inefficient and can lead to resource exhaustion under high traffic conditions. A connection pool maintains a set of open connections to a database, handing them out for repeated use.
mobc is agnostic to the connection type it is managing. Implementors of the
Manager
trait provide the database-specific logic to create and
check the health of connections.
Example
Using an imaginary "foodb" database.
use mobc::{Manager, Pool, async_trait}; #[derive(Debug)] struct FooError; struct FooConnection; impl FooConnection { async fn query(&self) -> String { "nori".to_string() } } struct FooManager; #[async_trait] impl Manager for FooManager { type Connection = FooConnection; type Error = FooError; async fn connect(&self) -> Result<Self::Connection, Self::Error> { Ok(FooConnection) } async fn check(&self, conn: Self::Connection) -> Result<Self::Connection, Self::Error> { 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; assert_eq!(name, "nori".to_string()); tx.send(()).await.unwrap(); }); } for _ in 0..num { rx.recv().await.unwrap(); } }
Modules
runtime | A batteries included runtime for applications using mobc. Mobc does not implement runtime, it simply exports runtime. |
Structs
Builder | A builder for a connection pool. |
Connection | A smart pointer wrapping a connection. |
Pool | A generic connection pool. |
State | Information about the state of a |
Enums
Error | The error type returned by methods in this crate. |
Traits
Manager | A trait which provides connection-specific functionality. |
Functions
spawn | Spawns a new asynchronous task. |
Attribute Macros
async_trait |