Connect lazily

This commit is contained in:
LukeMathWalker 2021-09-29 09:20:50 +01:00
parent 07350a45bf
commit b9cdb5c5c7
2 changed files with 4 additions and 10 deletions

View file

@ -16,10 +16,7 @@ pub struct Application {
impl Application {
pub async fn build(configuration: Settings) -> Result<Self, std::io::Error> {
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<PgPool, sqlx::Error> {
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(

View file

@ -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),
}
}