From b9cdb5c5c762fda74e73d592150e1e7920f97cd3 Mon Sep 17 00:00:00 2001 From: LukeMathWalker Date: Wed, 29 Sep 2021 09:20:50 +0100 Subject: [PATCH] Connect lazily --- src/startup.rs | 10 +++------- tests/api/helpers.rs | 4 +--- 2 files changed, 4 insertions(+), 10 deletions(-) diff --git a/src/startup.rs b/src/startup.rs index a0edf64..316f2e1 100644 --- a/src/startup.rs +++ b/src/startup.rs @@ -16,10 +16,7 @@ pub struct Application { impl Application { pub async fn build(configuration: Settings) -> Result { - let connection_pool = get_connection_pool(&configuration.database) - .await - .expect("Failed to connect to Postgres."); - + let connection_pool = get_connection_pool(&configuration.database); let sender_email = configuration .email_client .sender() @@ -52,11 +49,10 @@ impl Application { } } -pub async fn get_connection_pool(configuration: &DatabaseSettings) -> Result { +pub fn get_connection_pool(configuration: &DatabaseSettings) -> PgPool { PgPoolOptions::new() .connect_timeout(std::time::Duration::from_secs(2)) - .connect_with(configuration.with_db()) - .await + .connect_lazy_with(configuration.with_db()) } fn run( diff --git a/tests/api/helpers.rs b/tests/api/helpers.rs index 31e43f9..b0c49c1 100644 --- a/tests/api/helpers.rs +++ b/tests/api/helpers.rs @@ -60,9 +60,7 @@ pub async fn spawn_app() -> TestApp { TestApp { address, - db_pool: get_connection_pool(&configuration.database) - .await - .expect("Failed to connect to the database"), + db_pool: get_connection_pool(&configuration.database), } }