#connection-pool #redis #bb8-connection #tokio #r2d2 #adapter

bb8-redis

Full-featured async (tokio-based) redis connection pool (like r2d2)

17 releases (breaking)

0.15.0 Mar 11, 2024
0.14.0 Dec 6, 2023
0.13.1 May 31, 2023
0.12.0 Oct 17, 2022
0.3.0 Mar 18, 2019

#486 in Database interfaces

Download history 2688/week @ 2023-12-23 5853/week @ 2023-12-30 8899/week @ 2024-01-06 8625/week @ 2024-01-13 9712/week @ 2024-01-20 9529/week @ 2024-01-27 9935/week @ 2024-02-03 10336/week @ 2024-02-10 10829/week @ 2024-02-17 10478/week @ 2024-02-24 10163/week @ 2024-03-02 11576/week @ 2024-03-09 11411/week @ 2024-03-16 8930/week @ 2024-03-23 7901/week @ 2024-03-30 6603/week @ 2024-04-06

36,574 downloads per month
Used in 18 crates (17 directly)

MIT license

42KB
827 lines

bb8

Documentation Crates.io Build status codecov License: MIT

A full-featured connection pool, designed for asynchronous connections (using tokio). Originally based on r2d2.

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.

bb8 is agnostic to the connection type it is managing. Implementors of the ManageConnection trait provide the database-specific logic to create and check the health of connections.

A (possibly not exhaustive) list of adapters for different backends:

Backend Adapter Crate
tokio-postgres bb8-postgres (in-tree)
redis bb8-redis (in-tree)
redis_cluster_async bb8-redis-cluster
rsmq rsmq_async
bolt-client bb8-bolt
diesel bb8-diesel
tiberius bb8-tiberius
nebula-client bb8-nebula
memcache-async bb8-memcached
lapin bb8-lapin
arangors bb8-arangodb

Example

Using an imaginary "foodb" database.

#[tokio::main]
async fn main() {
    let manager = bb8_foodb::FooConnectionManager::new("localhost:1234");
    let pool = bb8::Pool::builder()
        .max_size(15)
        .build(manager)
        .await
        .unwrap();

    for _ in 0..20 {
        let pool = pool.clone();
        tokio::spawn(async move {
            let conn = pool.get().await.unwrap();
            // use the connection
            // it will be returned to the pool when it falls out of scope.
        });
    }
}

License

Licensed under the MIT license (LICENSE).

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you shall be licensed as above, without any additional terms or conditions.


lib.rs:

Redis support for the bb8 connection pool.

Example

use futures_util::future::join_all;
use bb8_redis::{
    bb8,
    redis::{cmd, AsyncCommands},
    RedisConnectionManager
};

#[tokio::main]
async fn main() {
    let manager = RedisConnectionManager::new("redis://localhost").unwrap();
    let pool = bb8::Pool::builder().build(manager).await.unwrap();

    let mut handles = vec![];

    for _i in 0..10 {
        let pool = pool.clone();

        handles.push(tokio::spawn(async move {
            let mut conn = pool.get().await.unwrap();

            let reply: String = cmd("PING").query_async(&mut *conn).await.unwrap();

            assert_eq!("PONG", reply);
        }));
    }

    join_all(handles).await;
}

Dependencies

~6–18MB
~237K SLoC