Docs.rs
  • mobc-0.8.5
    • mobc 0.8.5
    • Permalink
    • Docs.rs crate page
    • MIT/Apache-2.0
    • Links
    • Repository
    • crates.io
    • Source
    • Owners
    • garrensmith
    • aqrln
    • importcjj
    • Dependencies
      • actix-rt ^1 normal optional
      • async-std ^1.0 normal optional
      • async-trait ^0.1 normal
      • futures-channel ^0.3 normal
      • futures-core ^0.3 normal
      • futures-timer ^3.0.2 normal
      • futures-util ^0.3 normal
      • log ^0.4 normal
      • metrics ^0.23.0 normal
      • thiserror ^1.0 normal
      • tokio ^1 normal optional
      • tracing ^0.1 normal
      • tracing-subscriber ^0.3.11 normal
      • actix-rt ^2 dev
      • actix-web ^4.2.1 dev
      • async-std ^1.0 dev
      • env_logger ^0.10.0 dev
      • tide ^0.16 dev
      • tokio ^1 dev
    • Versions
    • 100% of the crate is documented
  • Platform
    • i686-pc-windows-msvc
    • i686-unknown-linux-gnu
    • x86_64-apple-darwin
    • x86_64-pc-windows-msvc
    • x86_64-unknown-linux-gnu
  • Feature flags
  • docs.rs
    • About docs.rs
    • Privacy policy
  • Rust
    • Rust website
    • The Book
    • Standard Library API Reference
    • Rust by Example
    • The Cargo Guide
    • Clippy Documentation

Crate mobc

mobc0.8.5

  • All Items

Sections

  • Example
  • Metrics

Crate Items

  • Modules
  • Structs
  • Enums
  • Traits
  • Functions
  • Attribute Macros

Crates

  • mobc

Crate mobc

Source
Expand description

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

§Metrics

Mobc uses the metrics crate to expose the following metrics

  1. Active Connections - The number of connections in use.
  2. Idle Connections - The number of connections that are not being used
  3. Wait Count - the number of processes waiting for a connection
  4. Wait Duration - A cumulative histogram of the wait time for a connection

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 Pool.

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

Results

Settings
Help
    extern crate
    mobc
    A generic connection pool with async/await support.
No results :(
Try on DuckDuckGo?

Or try looking in one of these:
  • The Rust Reference for technical details about the language.
  • Rust By Example for expository code examples.
  • The Rust Book for introductions to language features and the language itself.
  • Docs.rs for documentation of crates released on crates.io.
No results :(
Try on DuckDuckGo?

Or try looking in one of these:
  • The Rust Reference for technical details about the language.
  • Rust By Example for expository code examples.
  • The Rust Book for introductions to language features and the language itself.
  • Docs.rs for documentation of crates released on crates.io.