Crates.io | mobc-forked |
lib.rs | mobc-forked |
version | 0.7.4-alpha.4 |
source | src |
created_at | 2023-03-10 13:13:46.208346 |
updated_at | 2023-07-22 03:12:29.012772 |
description | A generic connection pool with async/await support |
homepage | |
repository | https://github.com/importcjj/mobc |
max_upload_size | |
id | 806433 |
size | 175,172 |
This is a fork of the original Mobc with fixes for stablility. This changes the design of Mobc to use a Semaphore to manage the pool rather than the original design that used channels.
A generic connection pool with async/await support.
Inspired by Deadpool, Sqlx, r2d2 and Golang SQL package.
Note: mobc requires at least Rust 1.39.
[dependencies]
mobc = { git = "https://github.com/prisma/mobc", branch = "main"}
# For async-std runtime
# mobc = { git = "https://github.com/prisma/mobc", features = ["async-std"] }
# For actix-rt 1.0
# mobc = { git = "https://github.com/prisma/mobc", features = ["actix-rt"] }
tokio
and async-std
Backend | Adaptor Crate |
---|---|
tokio-postgres | mobc-postgres |
redis | mobc-redis |
arangodb | mobc-arangors |
lapin | mobc-lapin |
reql | mobc-reql |
redis-cluster | mobc-redis-cluster |
More DB adaptors are welcome.
More examples
Using an imaginary "foodb" database.
use mobc::{async_trait, Manager};
#[derive(Debug)]
pub struct FooError;
pub struct FooConnection;
impl FooConnection {
pub async fn query(&self) -> String {
"PONG".to_string()
}
}
pub 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)
}
}
Sets the maximum number of connections managed by the pool.
0 means unlimited, defaults to 10.
Sets the maximum idle connection count maintained by the pool. The pool will maintain at most this many idle connections at all times, while respecting the value of max_open.
Sets the maximum lifetime of connections in the pool. Expired connections may be closed lazily before reuse.
None meas reuse forever, defaults to None.
Sets the get timeout used by the pool. Calls to Pool::get will wait this long for a connection to become available before returning an error.
None meas never timeout, defaults to 30 seconds.
Some of the connection pool configurations can be adjusted dynamically. Each connection pool instance has the following methods:
Because tokio is not compatible with other runtimes, such as async-std. So a database driver written with tokio cannot run in the async-std runtime. For example, you can't use redis-rs in tide because it uses tokio, so the connection pool which bases on redis-res can't be used in tide either.