Connect lazily

This commit is contained in:
LukeMathWalker 2021-09-29 09:20:50 +01:00
parent dbbb98c2de
commit 3bd80edfba
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()
@ -55,11 +52,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())
}
pub struct ApplicationBaseUrl(pub String);

View file

@ -108,9 +108,7 @@ pub async fn spawn_app() -> TestApp {
TestApp {
address: format!("http://localhost:{}", application_port),
port: application_port,
db_pool: get_connection_pool(&configuration.database)
.await
.expect("Failed to connect to the database"),
db_pool: get_connection_pool(&configuration.database),
email_server,
}
}