From 54d47b2d6383bef2b7a4c3710c47fa78e016433c Mon Sep 17 00:00:00 2001 From: Bulat Kurbanov Date: Sat, 7 Feb 2026 11:56:59 +0100 Subject: [PATCH] Add Postgres pool settings and application name Introduce CONFIG fields for POSTGRES_POOL_MAX_CONNECTIONS, POSTGRES_POOL_ACQUIRE_TIMEOUT_SEC and APPLICATION_NAME. Defaults are 10, 300s and 'users_settings_server' respectively. Include application_name in the DB URL and apply the config values to the PgPool options; add a log message on creation. --- src/config.rs | 19 +++++++++++++++++++ src/db.rs | 18 ++++++++++++++---- 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/src/config.rs b/src/config.rs index 872cfe7..538f6ef 100644 --- a/src/config.rs +++ b/src/config.rs @@ -9,6 +9,11 @@ pub struct Config { pub postgres_port: u32, pub postgres_db: String, + pub postgres_pool_max_connections: u32, + pub postgres_pool_acquire_timeout_sec: u64, + + pub application_name: String, + pub sentry_dsn: String, } @@ -16,6 +21,10 @@ fn get_env(env: &'static str) -> String { std::env::var(env).unwrap_or_else(|_| panic!("Cannot get the {} env variable", env)) } +fn get_env_or(env: &'static str, default: &'static str) -> String { + std::env::var(env).unwrap_or_else(|_| default.to_string()) +} + impl Config { pub fn load() -> Config { Config { @@ -27,6 +36,16 @@ impl Config { postgres_port: get_env("POSTGRES_PORT").parse().unwrap(), postgres_db: get_env("POSTGRES_DB"), + postgres_pool_max_connections: std::env::var("POSTGRES_POOL_MAX_CONNECTIONS") + .ok() + .and_then(|s| s.parse().ok()) + .unwrap_or(10), + postgres_pool_acquire_timeout_sec: std::env::var("POSTGRES_POOL_ACQUIRE_TIMEOUT_SEC") + .ok() + .and_then(|s| s.parse().ok()) + .unwrap_or(300), + application_name: get_env_or("APPLICATION_NAME", "users_settings_server"), + sentry_dsn: get_env("SENTRY_DSN"), } } diff --git a/src/db.rs b/src/db.rs index 157d0c0..77d1775 100644 --- a/src/db.rs +++ b/src/db.rs @@ -5,17 +5,27 @@ use tracing::info; pub async fn get_postgres_pool() -> PgPool { let database_url: String = format!( - "postgresql://{}:{}@{}:{}/{}", + "postgresql://{}:{}@{}:{}/{}?application_name={}", CONFIG.postgres_user, CONFIG.postgres_password, CONFIG.postgres_host, CONFIG.postgres_port, - CONFIG.postgres_db + CONFIG.postgres_db, + CONFIG.application_name + ); + + info!( + "Creating Postgres pool (app_name={}, max_connections={}, acquire_timeout_sec={})", + CONFIG.application_name, + CONFIG.postgres_pool_max_connections, + CONFIG.postgres_pool_acquire_timeout_sec ); let pool = PgPoolOptions::new() - .max_connections(10) - .acquire_timeout(std::time::Duration::from_secs(300)) + .max_connections(CONFIG.postgres_pool_max_connections) + .acquire_timeout(std::time::Duration::from_secs( + CONFIG.postgres_pool_acquire_timeout_sec, + )) .connect(&database_url) .await .unwrap();