Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend database pool options #1026

Merged
merged 5 commits into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ Recommendation: for ease of reading, use the following order:

## [Unreleased]
### Changed
- Extended database config options with next fields: `maxConnections`, `maxLifeTimeSecs` and `acquireTimeoutSecs`

## [0.217.0] - 2025-01-08
### Changed
- GraphQL: flows are listed ordered by status and last event time
- Merged two methods(`saveEnvVariable` and `modifyEnvVariable`) from `DatasetEnvVarsMut` info one `upsertEnvVariable`
### Fixed
Expand Down
10 changes: 9 additions & 1 deletion src/app/cli/src/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,15 @@ pub fn try_build_db_connection_settings(
raw_db_config: &DatabaseConfig,
) -> Option<DatabaseConnectionSettings> {
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 {
Expand Down
6 changes: 6 additions & 0 deletions src/app/cli/src/services/config/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
})
}

Expand All @@ -732,6 +735,9 @@ pub struct RemoteDatabaseConfig {
pub database_name: String,
pub host: String,
pub port: Option<u16>,
pub max_connections: Option<u32>,
pub max_lifetime_secs: Option<u64>,
pub acquire_timeout_secs: Option<u64>,
}

#[skip_serializing_none]
Expand Down
12 changes: 12 additions & 0 deletions src/utils/database-common/src/db_connection_settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ pub struct DatabaseConnectionSettings {
pub database_name: String,
pub host: String,
pub port: Option<u16>,
pub max_connections: Option<u32>,
pub max_lifetime_secs: Option<u64>,
pub acquire_timeout_secs: Option<u64>,
}

impl DatabaseConnectionSettings {
Expand All @@ -27,12 +30,18 @@ impl DatabaseConnectionSettings {
database_name: String,
host: String,
port: Option<u16>,
max_connections: Option<u32>,
max_lifetime_secs: Option<u64>,
acquire_timeout_secs: Option<u64>,
) -> Self {
Self {
provider,
database_name,
host,
port,
max_connections,
max_lifetime_secs,
acquire_timeout_secs,
}
}

Expand All @@ -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,
}
}
}
Expand Down
20 changes: 18 additions & 2 deletions src/utils/database-common/src/plugins/mysql_plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::mysql::MySqlConnectOptions;
use sqlx::MySqlPool;
use sqlx::pool::PoolOptions;
use sqlx::{MySql, MySqlPool};

use crate::*;

Expand Down Expand Up @@ -58,7 +61,20 @@ impl MySqlPlugin {
.password(db_credentials.password.expose_secret());
}

MySqlPool::connect_lazy_with(mysql_options)
let mysql_pool: PoolOptions<MySql> = 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),
);

mysql_pool.connect_lazy_with(mysql_options)
}
}

Expand Down
20 changes: 18 additions & 2 deletions src/utils/database-common/src/plugins/postgres_plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;

Expand Down Expand Up @@ -58,7 +61,20 @@ impl PostgresPlugin {
.password(db_credentials.password.expose_secret());
}

PgPool::connect_lazy_with(pg_options)
let pg_pool: PoolOptions<Postgres> = 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)
}
}

Expand Down
10 changes: 10 additions & 0 deletions src/utils/database-common/src/plugins/sqlite_plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,16 @@ impl SqlitePlugin {
.filename(&db_connection_settings.host)
.create_if_missing(true);

if db_connection_settings.max_lifetime_secs.is_some() {
tracing::warn!("max_lifetime_secs is not supported for SQLite");
}
if db_connection_settings.max_connections.is_some() {
tracing::warn!("max_connections is not supported for SQLite");
}
if db_connection_settings.acquire_timeout_secs.is_some() {
tracing::warn!("acquire_timeout_secs is not supported for SQLite");
}

SqlitePoolOptions::new()
.max_connections(1)
.connect_lazy_with(sqlite_options)
Expand Down