diff --git a/src/app/cli/src/database.rs b/src/app/cli/src/database.rs index d4ccb6d92..b90deb8a9 100644 --- a/src/app/cli/src/database.rs +++ b/src/app/cli/src/database.rs @@ -206,7 +206,15 @@ pub fn try_build_db_connection_settings( raw_db_config: &DatabaseConfig, ) -> Option { fn convert(c: &RemoteDatabaseConfig, provider: DatabaseProvider) -> DatabaseConnectionSettings { - DatabaseConnectionSettings::new(provider, c.database_name.clone(), c.host.clone(), c.port) + DatabaseConnectionSettings::new( + provider, + c.database_name.clone(), + c.host.clone(), + c.port, + c.max_connections, + c.max_lifetime_secs, + c.acquire_timeout_secs, + ) } match raw_db_config { diff --git a/src/app/cli/src/services/config/models.rs b/src/app/cli/src/services/config/models.rs index ae38e6005..78bbfcb4c 100644 --- a/src/app/cli/src/services/config/models.rs +++ b/src/app/cli/src/services/config/models.rs @@ -706,6 +706,9 @@ impl DatabaseConfig { database_name: String::from("kamu"), host: String::from("localhost"), port: Some(DatabaseProvider::Postgres.default_port()), + acquire_timeout_secs: None, + max_connections: None, + max_lifetime_secs: None, }) } @@ -732,6 +735,9 @@ pub struct RemoteDatabaseConfig { pub database_name: String, pub host: String, pub port: Option, + pub max_connections: Option, + pub max_lifetime_secs: Option, + pub acquire_timeout_secs: Option, } #[skip_serializing_none] diff --git a/src/utils/database-common/src/db_connection_settings.rs b/src/utils/database-common/src/db_connection_settings.rs index 7a844d354..8a0384f7c 100644 --- a/src/utils/database-common/src/db_connection_settings.rs +++ b/src/utils/database-common/src/db_connection_settings.rs @@ -19,6 +19,9 @@ pub struct DatabaseConnectionSettings { pub database_name: String, pub host: String, pub port: Option, + pub max_connections: Option, + pub max_lifetime_secs: Option, + pub acquire_timeout_secs: Option, } impl DatabaseConnectionSettings { @@ -27,12 +30,18 @@ impl DatabaseConnectionSettings { database_name: String, host: String, port: Option, + max_connections: Option, + max_lifetime_secs: Option, + acquire_timeout_secs: Option, ) -> Self { Self { provider, database_name, host, port, + max_connections, + max_lifetime_secs, + acquire_timeout_secs, } } @@ -46,6 +55,9 @@ impl DatabaseConnectionSettings { database_name: String::new(), host: String::from(path.to_str().unwrap()), port: None, + max_connections: None, + max_lifetime_secs: None, + acquire_timeout_secs: None, } } } diff --git a/src/utils/database-common/src/plugins/postgres_plugin.rs b/src/utils/database-common/src/plugins/postgres_plugin.rs index d40c00dba..57d15e2d8 100644 --- a/src/utils/database-common/src/plugins/postgres_plugin.rs +++ b/src/utils/database-common/src/plugins/postgres_plugin.rs @@ -7,10 +7,13 @@ // the Business Source License, use of this software will be governed // by the Apache License, Version 2.0. +use std::time::Duration; + use dill::*; use secrecy::ExposeSecret; +use sqlx::pool::PoolOptions; use sqlx::postgres::PgConnectOptions; -use sqlx::PgPool; +use sqlx::{PgPool, Postgres}; use crate::*; @@ -58,7 +61,20 @@ impl PostgresPlugin { .password(db_credentials.password.expose_secret()); } - PgPool::connect_lazy_with(pg_options) + let pg_pool: PoolOptions = PoolOptions::new() + .max_lifetime( + db_connection_settings + .max_lifetime_secs + .map(Duration::from_secs), + ) + .max_connections(db_connection_settings.max_connections.unwrap_or(10)) + .acquire_timeout( + db_connection_settings + .acquire_timeout_secs + .map_or(Duration::from_secs(30), Duration::from_secs), + ); + + pg_pool.connect_lazy_with(pg_options) } }