sfackler / r2d2

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

Test connection before initializing pool #104

Open Pzixel opened 4 years ago

Pzixel commented 4 years ago

Consider following code

let postgres_manager = ConnectionManager::<PgConnection>::new("postgre://bac");
let pool = Pool::new(postgres_manager)?;

Currently it produces following output:

2020-05-13T16:30:07.777244900+03:00 ERROR r2d2 - missing "=" after "postgre://bac" in connection info string
2020-05-13T16:30:07.778244700+03:00 ERROR r2d2 - missing "=" after "postgre://bac" in connection info string
2020-05-13T16:30:07.778244700+03:00 ERROR r2d2 - missing "=" after "postgre://bac" in connection info string
2020-05-13T16:30:07.778244700+03:00 ERROR r2d2 - missing "=" after "postgre://bac" in connection info string
2020-05-13T16:30:07.778244700+03:00 ERROR r2d2 - missing "=" after "postgre://bac" in connection info string
2020-05-13T16:30:07.778244700+03:00 ERROR r2d2 - missing "=" after "postgre://bac" in connection info string
2020-05-13T16:30:07.778244700+03:00 ERROR r2d2 - missing "=" after "postgre://bac" in connection info string
2020-05-13T16:30:07.778244700+03:00 ERROR r2d2 - missing "=" after "postgre://bac" in connection info string
2020-05-13T16:30:07.778244700+03:00 ERROR r2d2 - missing "=" after "postgre://bac" in connection info string
2020-05-13T16:30:07.778244700+03:00 ERROR r2d2 - missing "=" after "postgre://bac" in connection info string
2020-05-13T16:30:08.179240800+03:00 ERROR r2d2 - missing "=" after "postgre://bac" in connection info string
2020-05-13T16:30:08.179240800+03:00 ERROR r2d2 - missing "=" after "postgre://bac" in connection info string
2020-05-13T16:30:08.179240800+03:00 ERROR r2d2 - missing "=" after "postgre://bac" in connection info string
2020-05-13T16:30:08.179240800+03:00 ERROR r2d2 - missing "=" after "postgre://bac" in connection info string
2020-05-13T16:30:08.179240800+03:00 ERROR r2d2 - missing "=" after "postgre://bac" in connection info string
2020-05-13T16:30:08.179240800+03:00 ERROR r2d2 - missing "=" after "postgre://bac" in connection info string
2020-05-13T16:30:08.179240800+03:00 ERROR r2d2 - missing "=" after "postgre://bac" in connection info string
2020-05-13T16:30:08.179240800+03:00 ERROR r2d2 - missing "=" after "postgre://bac" in connection info string
2020-05-13T16:30:08.179240800+03:00 ERROR r2d2 - missing "=" after "postgre://bac" in connection info string
...

It returns Err in the end, but it takes time and unnessessary requests to db.

I think probably it should check if connection is ok before initializing pool. Something like:

    pub fn new(manager: M) -> Result<Pool<M>, Error> {
        let _test_connecction = manager.connect()?;
        Pool::builder().build(manager)
    }