bb8/
lib.rs

1//! A full-featured connection pool, designed for asynchronous connections
2//! (using tokio). Originally based on [r2d2](https://github.com/sfackler/r2d2).
3//!
4//! Opening a new database connection every time one is needed is both
5//! inefficient and can lead to resource exhaustion under high traffic
6//! conditions. A connection pool maintains a set of open connections to a
7//! database, handing them out for repeated use.
8//!
9//! bb8 is agnostic to the connection type it is managing. Implementors of the
10//! `ManageConnection` trait provide the database-specific logic to create and
11//! check the health of connections.
12//!
13//! # Example
14//!
15//! Using an imaginary "foodb" database.
16//!
17//! ```ignore
18//! #[tokio::main]
19//! async fn main() {
20//!     let manager = bb8_foodb::FooConnectionManager::new("localhost:1234");
21//!     let pool = bb8::Pool::builder().build(manager).await.unwrap();
22//!
23//!     for _ in 0..20 {
24//!         let pool = pool.clone();
25//!         tokio::spawn(async move {
26//!             let conn = pool.get().await.unwrap();
27//!             // use the connection
28//!             // it will be returned to the pool when it falls out of scope.
29//!         });
30//!     }
31//! }
32//! ```
33#![allow(clippy::needless_doctest_main)]
34#![deny(missing_docs, missing_debug_implementations)]
35
36mod api;
37pub use api::{
38    AddError, Builder, CustomizeConnection, ErrorSink, ManageConnection, NopErrorSink, Pool,
39    PooledConnection, QueueStrategy, RunError, State, Statistics,
40};
41
42mod inner;
43mod internals;
44mod lock {
45    #[cfg(feature = "parking_lot")]
46    use parking_lot::Mutex as MutexImpl;
47    #[cfg(feature = "parking_lot")]
48    use parking_lot::MutexGuard;
49
50    #[cfg(not(feature = "parking_lot"))]
51    use std::sync::Mutex as MutexImpl;
52    #[cfg(not(feature = "parking_lot"))]
53    use std::sync::MutexGuard;
54
55    pub(crate) struct Mutex<T>(MutexImpl<T>);
56
57    impl<T> Mutex<T> {
58        pub(crate) fn new(val: T) -> Self {
59            Self(MutexImpl::new(val))
60        }
61
62        pub(crate) fn lock(&self) -> MutexGuard<'_, T> {
63            #[cfg(feature = "parking_lot")]
64            {
65                self.0.lock()
66            }
67            #[cfg(not(feature = "parking_lot"))]
68            {
69                self.0.lock().unwrap()
70            }
71        }
72    }
73}