diff --git a/bb8/src/api.rs b/bb8/src/api.rs index 794d58d..c8e0017 100644 --- a/bb8/src/api.rs +++ b/bb8/src/api.rs @@ -272,6 +272,7 @@ impl Builder { /// minimum number of connections, or it times out. pub async fn build(self, manager: M) -> Result, M::Error> { let pool = self.build_inner(manager); + pool.inner.preconnect().await?; pool.inner.start_connections().await.map(|()| pool) } diff --git a/bb8/src/inner.rs b/bb8/src/inner.rs index 71f6ab8..cfe759c 100644 --- a/bb8/src/inner.rs +++ b/bb8/src/inner.rs @@ -151,6 +151,10 @@ where Ok(conn) } + pub(crate) async fn preconnect(&self) -> Result<(), M::Error> { + self.inner.manager.connect().await.map(|_| ()) + } + /// Return connection back in to the pool pub(crate) fn put_back(&self, conn: Option>) { let conn = conn.and_then(|mut conn| { diff --git a/bb8/tests/test.rs b/bb8/tests/test.rs index a8e1d02..cbf0673 100644 --- a/bb8/tests/test.rs +++ b/bb8/tests/test.rs @@ -108,7 +108,7 @@ where #[tokio::test] async fn test_max_size_ok() { - let manager = NthConnectionFailManager::::new(5); + let manager = NthConnectionFailManager::::new(6); let pool = Pool::builder().max_size(5).build(manager).await.unwrap(); let mut channels = Vec::with_capacity(5); let mut ignored = Vec::with_capacity(5); @@ -409,7 +409,7 @@ async fn test_max_lifetime() { } } - let manager = NthConnectionFailManager::::new(5); + let manager = NthConnectionFailManager::::new(6); let pool = Pool::builder() .max_lifetime(Some(Duration::from_secs(1))) .connection_timeout(Duration::from_secs(1)) @@ -442,14 +442,14 @@ async fn test_max_lifetime() { assert!(timeout(Duration::from_secs(2), pending::<()>()) .await .is_err()); - assert_eq!(DROPPED.load(Ordering::SeqCst), 4); + assert_eq!(DROPPED.load(Ordering::SeqCst), 5); tx2.send(()).unwrap(); // And wait some more. assert!(timeout(Duration::from_secs(2), pending::<()>()) .await .is_err()); - assert_eq!(DROPPED.load(Ordering::SeqCst), 5); + assert_eq!(DROPPED.load(Ordering::SeqCst), 6); } #[tokio::test] @@ -550,7 +550,7 @@ async fn test_conns_drop_on_pool_drop() { drop(pool); for _ in 0u8..10 { - if DROPPED.load(Ordering::SeqCst) == 10 { + if DROPPED.load(Ordering::SeqCst) == 11 { return; } @@ -626,7 +626,7 @@ async fn test_conn_fail_once() { async fn connect(&self) -> Result { // only fail once so the retry should work - if FAILED_ONCE.load(Ordering::SeqCst) { + if FAILED_ONCE.load(Ordering::SeqCst) || NB_CALL.load(Ordering::SeqCst) == 0 { Ok(Connection) } else { FAILED_ONCE.store(true, Ordering::SeqCst);