3

I want to use hyper with bb8 and tokio-postgres. In every request I want to acquire a new connection from the pool. Can anybody provide me some example for this scenario? Currently I do it like this:

fn main() {
    let addr = "127.0.0.1:3000".parse().unwrap();

    let pg_mgr =
        PostgresConnectionManager::new("postgresql://auth:auth@localhost:5433/auth", NoTls);

    rt::run(future::lazy(move || {
        Pool::builder()
            .build(pg_mgr)
            .map_err(|e| eprintln!("Database error: {}", e))
            .and_then(move |pool| {
                let service = || service_fn(|req| router(req, pool.clone()));

                let server = Server::bind(&addr)
                    .serve(service)
                    .map_err(|e| eprintln!("Server error: {}", e));

                println!("Listening on http://{}", addr);
                server
            })
    }))
}

fn router(
    _req: Request<Body>,
    _pool: Pool<PostgresConnectionManager<NoTls>>,
) -> Result<Response<Body>, hyper::Error> {
    // do some staff with pool
}

But it won't compile:

error[E0597]: `pool` does not live long enough
  --> src/main.rs:22:63
   |
22 |                 let service = || service_fn(|req| router(req, pool.clone()));
   |                               -- -----------------------------^^^^----------
   |                               |  |                            |
   |                               |  |                            borrowed value does not live long enough
   |                               |  returning this value requires that `pool` is borrowed for `'static`
   |                               value captured here
...
30 |             })
   |             - `pool` dropped here while still borrowed

What am I doing wrong? How to make my case work correctly?

sivakov512
  • 415
  • 5
  • 14

2 Answers2

2

The solution is pretty simple but to understand the problem I want to provide some additional info...

  1. When you call and_then on a future to get the result, it passes the value of the variable to the closure passed to and_then which gives you ownership of that data.

  2. The method serve on hypers builder (returned by Server::bind), expects for the closure to have a static lifetime.

Now to address the problem:

  • Good: pass the value of the closure into serve, this moves it, transferring the ownership.
  • Good: service_fn is defined outside of the and_then closure so that function lives long enough
  • Bad: The closure uses the local variable pool to pass it to the service_fn.

To resolve the problem, just move the local data into your closure like so:

let service = move || service_fn(|req| router(req, pool));

kyle
  • 2,563
  • 6
  • 22
  • 37
  • It would be cool if you show code examples for every solution. With my low Rust experience it's easier to understand the solutions by reading code. – sivakov512 Jul 19 '19 at 05:05
  • I could provide some more code in a playground example -- but it doesn't have r2d2 so I would have the mock it out. [Here is some extra code](https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=af120bda3f2354498f08f1d44d0a5925) but the server and pool aren't actually hyper and r2d2. – kyle Jul 21 '19 at 23:17
1

Solution found here

The simplest solution looks like:

fn main() {
    let addr = "127.0.0.1:3000".parse().unwrap();

    let pg_mgr =
        PostgresConnectionManager::new("postgresql://auth:auth@localhost:5433/auth", NoTls);

    rt::run(future::lazy(move || {
        Pool::builder()
            .build(pg_mgr)
            .map_err(|_| eprintln!("kek"))
            .and_then(move |pool| {
                let service = move || {
                    let pool = pool.clone();
                    service_fn(move |req| router(req, &pool))
                };

                let server = Server::bind(&addr)
                    .serve(service)
                    .map_err(|e| eprintln!("Server error: {}", e));

                println!("Listening on http://{}", addr);
                server
            })
    }))
}

fn router(
    _req: Request<Body>,
    _pool: &Pool<PostgresConnectionManager<NoTls>>,
) -> impl Future<Item = Response<Body>, Error = hyper::Error> {
    // some staff
}

It is also possible to construct service outside of rt::run with Arc and Mutex:

fn main() {
    let addr = "127.0.0.1:3000".parse().unwrap();

    let pg_mgr =
        PostgresConnectionManager::new("postgresql://auth:auth@localhost:5433/auth", NoTls);
    let pool: Arc<Mutex<Option<Pool<PostgresConnectionManager<NoTls>>>>> =
        Arc::new(Mutex::new(None));
    let pool2 = pool.clone();

    let service = move || {
        let pool = pool.clone();
        service_fn(move |req| {
            let locked = pool.lock().unwrap();
            let pool = locked
                .as_ref()
                .expect("bb8 should be initialized before hyper");
            router(req, pool)
        })
    };

    rt::run(future::lazy(move || {
        Pool::builder()
            .build(pg_mgr)
            .map_err(|_| eprintln!("kek"))
            .and_then(move |pool| {
                *pool2.lock().unwrap() = Some(pool);

                let server = Server::bind(&addr)
                    .serve(service)
                    .map_err(|e| eprintln!("Server error: {}", e));

                println!("Listening on http://{}", addr);
                server
            })
    }))
}

fn router(
    _req: Request<Body>,
    _pool: &Pool<PostgresConnectionManager<NoTls>>,
) -> impl Future<Item = Response<Body>, Error = hyper::Error> {
    // some staff
}
sivakov512
  • 415
  • 5
  • 14