mirror of
https://github.com/flibusta-apps/users_settings_server.git
synced 2026-03-03 15:10:51 +01:00
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.
This commit is contained in:
@@ -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"),
|
||||
}
|
||||
}
|
||||
|
||||
18
src/db.rs
18
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();
|
||||
|
||||
Reference in New Issue
Block a user