sfackler / r2d2

A generic connection pool for Rust
Apache License 2.0
1.51k stars 82 forks source link

Better Tutorial #72

Closed jparr721 closed 5 years ago

jparr721 commented 5 years ago

Hi, I noticed the tutorial for this piece of Diesel rs is a bit lackluster. I am attempting to use r2d2 for its connection pool capabilities, but I have noticed it's kind of hard to get started with the example given.

use std::thread;

extern crate r2d2;
extern crate r2d2_foodb; // Where does this come from?

fn main() {
    let manager = r2d2_foodb::FooConnectionManager::new("localhost:1234"); // How does this get created?
    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.
        })
    }
}

A cursory glance of this code still leaves me confused about how I may go about implementing this in my own system. For example, you import r2d2, but you use a connection manager for a separate r2d2 module, r2d2::foodb. After reading the source code for an example of how I can emulate this desired behavior, I have not been able to find what I am looking for. What is "foodb" and how does it get created as an external crate in this case?

Would you be willing to provide a more complete example of how this system works? It is still quite confusing in its current state.

sfackler commented 5 years ago

r2d2_foodb is a separate crate, not a separate module. It's a stand-in for the interface for whatever database you're using. There's a list of them in the readme: https://github.com/sfackler/r2d2/blob/master/README.md

jparr721 commented 5 years ago

Forgive me for the confusion here. I see exactly what you mean now.