• Stars
    star
    1,345
  • Rank 34,927 (Top 0.7 %)
  • Language
    Rust
  • License
    Apache License 2.0
  • Created over 10 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A generic connection pool for Rust

r2d2

CircleCI

A generic connection pool for Rust.

Documentation

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.

r2d2 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 adaptors for different backends:

Backend Adaptor Crate
rust-postgres r2d2-postgres
redis-rs use r2d2 feature of redis-rs
rust-memcache r2d2-memcache
rust-mysql-simple r2d2-mysql
rusqlite r2d2-sqlite
rsfbclient r2d2-firebird
rusted-cypher r2d2-cypher
diesel diesel::r2d2 (docs)
couchdb r2d2-couchdb
mongodb (archived)
use official mongodb driver instead
r2d2-mongodb
(deprecated: official driver handles pooling internally)
odbc r2d2-odbc
jfs r2d2-jfs
oracle r2d2-oracle
ldap3 r2d2-ldap
duckdb-rs use r2d2 feature of duckdb-rs

Example

Using an imaginary "foodb" database.

use std::thread;

extern crate r2d2;
extern crate r2d2_foodb;

fn main() {
    let manager = r2d2_foodb::FooConnectionManager::new("localhost:1234");
    let pool = r2d2::Pool::builder()
        .max_size(15)
        .build(manager)
        .unwrap();

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

License

Licensed under either of

at your option.

Contribution

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

More Repositories

1

rust-postgres

Native PostgreSQL driver for the Rust programming language
Rust
3,037
star
2

rust-openssl

OpenSSL bindings for Rust
Rust
1,223
star
3

cargo-tree

Rust
531
star
4

rust-native-tls

Rust
474
star
5

rust-postgres-macros

Support macros for Rust-Postgres
Rust
154
star
6

serde-transcode

Rust
99
star
7

rust-socks

Rust
80
star
8

streaming-iterator

Rust
76
star
9

rust-fallible-iterator

Rust
54
star
10

rust-log-panics

Rust
54
star
11

foreign-types

Rust
50
star
12

scheduled-thread-pool

Rust
36
star
13

hyper-native-tls

Rust
36
star
14

rust-postgres-derive

Rust
35
star
15

rstack

Rust
32
star
16

tokio-io-timeout

Rust
32
star
17

hyper-openssl

Rust
25
star
18

shell-escape

Rust
20
star
19

rust-postgres-array

Rust
20
star
20

typed-headers

Rust
17
star
21

staged-builder

Rust
14
star
22

exponential-decay-histogram

Rust
13
star
23

rust-antidote

Rust
11
star
24

rust-postgres-large-object

Rust
9
star
25

jemalloc-ctl

Rust
9
star
26

rust-log-mdc

Rust
9
star
27

rust-openssl-verify

Rust
8
star
28

rust-postgres-range

Rust
7
star
29

rust-stringprep

Rust
7
star
30

stream-vbyte64

Rust
7
star
31

thread-local-object

Rust
6
star
32

log4rs-routing-appender

Rust
6
star
33

fallible-streaming-iterator

Rust
6
star
34

serde-smile

A Smile implementation for Serde
Rust
6
star
35

rust-postgres-protocol

5
star
36

syntax-ext-talk

JavaScript
5
star
37

rust-posix-ipc

Rust
5
star
38

log4rs-rolling-file

Rust
4
star
39

futures-state-stream

Rust
4
star
40

rust-hyper-socks

Rust
4
star
41

serde-humantime

Rust
4
star
42

rust-pg_query

Rust
3
star
43

hyper-timeout-connector

Rust
3
star
44

rust-time2

Rust
2
star
45

rust-docker-compose

Rust
1
star
46

serde-file-value

Rust
1
star
47

conduit-hyper

Rust
1
star
48

busted-crate

Rust
1
star
49

advent-of-code-2016

Rust
1
star
50

perf-hacks

Rust
1
star
51

advent-of-code

Rust
1
star
52

cargo-sls-distribution

Rust
1
star
53

rust-debug-builders

Rust
1
star