Skip to content

Commit

Permalink
Extend pool options
Browse files Browse the repository at this point in the history
  • Loading branch information
rmn-boiko committed Jan 6, 2025
1 parent a574f62 commit e6e8937
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 3 deletions.
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/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

0 comments on commit e6e8937

Please sign in to comment.