Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fail to get the connection with the features ["postgres", "bb8"] #99

Closed
3 tasks done
peng-huang-ch opened this issue Aug 6, 2023 · 4 comments
Closed
3 tasks done
Labels
bug Something isn't working

Comments

@peng-huang-ch
Copy link

Setup

Versions

  • Rust: rustc 1.72.0
  • Diesel: 2.1.0
  • Diesel_async: 0.3.1
  • Database: postgres
  • Operating System macos 13.4.1

Feature Flags

  • diesel: ["postgres", "r2d2"]
  • diesel_async: ["postgres", "bb8"]

Problem Description

Fail to get the connection, TimedOut

What are you trying to accomplish?

Manager the connection with the diesel-async

What is the expected output?

What is the actual output?

thread 'pg_async::tests::test_init_db' panicked at 'could not get connection: TimedOut', crates/srv-storage/src/pg_async.rs:39:41
stack backtrace:
   0: rust_begin_unwind
             at /rustc/065a1f5df9c2f1d93269e4d25a2acabbddb0db8d/library/std/src/panicking.rs:593:5
   1: core::panicking::panic_fmt
             at /rustc/065a1f5df9c2f1d93269e4d25a2acabbddb0db8d/library/core/src/panicking.rs:67:14
   2: core::result::unwrap_failed
             at /rustc/065a1f5df9c2f1d93269e4d25a2acabbddb0db8d/library/core/src/result.rs:1651:5
   3: core::result::Result<T,E>::expect
             at /rustc/065a1f5df9c2f1d93269e4d25a2acabbddb0db8d/library/core/src/result.rs:1033:23
   4: srv_storage::pg_async::tests::test_init_db::{{closure}}
             at ./src/pg_async.rs:39:24

Are you seeing any additional errors?

No additional error is shown.

Steps to reproduce

use diesel_async::{
    pooled_connection::{bb8::Pool, bb8::PooledConnection, AsyncDieselConnectionManager},
    AsyncPgConnection,
};

pub type DbConnectionManger = AsyncDieselConnectionManager<AsyncPgConnection>;

pub type DbPool = Pool<AsyncPgConnection>;

pub type DbConnection<'a> = PooledConnection<'a, AsyncDieselConnectionManager<AsyncPgConnection>>;

#[tracing::instrument()]
pub async fn init_db(database_url: &str) -> DbPool {
    let mgr = AsyncDieselConnectionManager::<AsyncPgConnection>::new(database_url);

    Pool::builder()
        .build(mgr)
        .await
        .expect("could not build connection pool")
}

#[cfg(test)]
mod tests {
    use super::init_db;
    use diesel::{prelude::*, sql_query, sql_types::Text};
    use diesel_async::RunQueryDsl;
    #[derive(QueryableByName)]
    struct SqlVersion {
        #[diesel(sql_type = Text)]
        pub version: String,
    }

    #[tokio::main]
    #[test]
    async fn test_init_db() {
        dotenvy::dotenv().ok();
        let database_url = std::env::var("DATABASE_URL").expect("Expected DATABASE_URL to be set");
        let pool = init_db(database_url.as_str()).await;
        let mut conn = pool.get().await.expect("could not get connection");
        let version = sql_query("SELECT version()")
            .get_result::<SqlVersion>(&mut conn)
            .await;

        assert!(version.is_ok());
        let version = version.unwrap();
        println!("database version {}", version.version);
    }
}
diesel = { version = "2.1.0", features = ["postgres", "r2d2"] }
diesel-async = { version = "0.3.1", features = ["postgres", "bb8"] }

Checklist

  • I have already looked over the issue tracker for similar possible closed issues.
  • This issue can be reproduced on Rust's stable channel. (Your issue will be
    closed if this is not the case)
  • This issue can be reproduced without requiring a third party crate
@peng-huang-ch peng-huang-ch added the bug Something isn't working label Aug 6, 2023
@weiznich
Copy link
Owner

weiznich commented Aug 6, 2023

I've tried your minimal example locally and the tests passes for my setup. Additionally this issue indicates that this error also might be returned if your connection URL is somehow invalid. I agree that this error message is not optimal, but that's an issue in bb8 then.

Closed that I'm not able to reproduce the issue locally with a valid database URL.

@weiznich weiznich closed this as not planned Won't fix, can't repro, duplicate, stale Aug 6, 2023
@peng-huang-ch
Copy link
Author

Thank you for your reply. I don't think it's a URL issue. I have provided a repo to reproduce the issue.

image

@weiznich
Copy link
Owner

weiznich commented Aug 7, 2023

I've checked your repo and I cannot reproduce the issue with that either. Both tests complete successful for me. Again I believe that this is just an issue with your database URL. Please note that diesel::PgConnection and diesel_async::AsyncPgConnection do not necessarily support the same database URL params/format. They are completely different implementations that support a different set of parameters. Just because a database URL works with one of the implementations it does not necessarily mean that it will work with the other implementation as well. Checkout the documentation of both types to learn about the required format.

@peng-huang-ch
Copy link
Author

peng-huang-ch commented Aug 7, 2023

Once I change my url format to the postgres://${whoami}@localhost/signatures, the test passed.
The connection URLs should be in the form postgres://[user[:password]@]host/database_name
Thanks again.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants