Skip to content

Open source, composable payments platform | PCI compliant | SaaS and Self-host options | Enables connectivity to multiple payment, payout, fraud, vault and tokenization providers | Uplifts authorization with intelligent routing and revenue recovery | Reduce payment processing costs with cost observability | Reduces payment ops with reconciliation

fix(router): Add config to close idle DB connections (#13194)

Anurag Thakur0439e7619dae
diff --git a/config/config.example.toml b/config/config.example.toml
index 6c76b7a3af..8ec83deac5 100644
--- a/config/config.example.toml
+++ b/config/config.example.toml
@@ -52,9 +52,12 @@ password = "db_pass" # DB Password. Use base-64 encoded kms encrypted value
host = "localhost" # DB Host
port = 5432 # DB Port
dbname = "hyperswitch_db" # Name of Database
-pool_size = 5 # Number of connections to keep open
+max_pool_size = 5 # Maximum number of connections in the pool
+min_idle_pool_size = 2 # Minimum number of idle connections to maintain in the pool
connection_timeout = 10 # Timeout for database connection in seconds
queue_strategy = "Fifo" # Add the queue strategy used by the database bb8 client
+idle_timeout = 300 # Maximum idle duration in seconds before a connection is closed
+max_lifetime = 1800 # Maximum lifetime in seconds before a connection is reaped
# Replica SQL data store credentials
[replica_database]
@@ -63,9 +66,12 @@ password = "db_pass" # DB Password. Use base-64 encoded kms encrypted value
host = "localhost" # DB Host
port = 5432 # DB Port
dbname = "hyperswitch_db" # Name of Database
-pool_size = 5 # Number of connections to keep open
+max_pool_size = 5 # Maximum number of connections in the pool
+min_idle_pool_size = 2 # Minimum number of idle connections to maintain in the pool
connection_timeout = 10 # Timeout for database connection in seconds
queue_strategy = "Fifo" # Add the queue strategy used by the database bb8 client
+idle_timeout = 300 # Maximum idle duration in seconds before a connection is closed
+max_lifetime = 1800 # Maximum lifetime in seconds before a connection is reaped
# Redis credentials
[redis]
@@ -1173,9 +1179,12 @@ password = "db_pass" # Analytics DB Password
host = "localhost" # Analytics DB Host
port = 5432 # Analytics DB Port
dbname = "hyperswitch_db" # Name of Database
-pool_size = 5 # Number of connections to keep open
+max_pool_size = 5 # Maximum number of connections in the pool
+min_idle_pool_size = 2 # Minimum number of idle connections to maintain in the pool
connection_timeout = 10 # Timeout for database connection in seconds
queue_strategy = "Fifo" # Add the queue strategy used by the database bb8 client
+idle_timeout = 300 # Maximum idle duration in seconds before a connection is closed
+max_lifetime = 1800 # Maximum lifetime in seconds before a connection is reaped
# Config for KV setup
[kv_config]
diff --git a/config/deployments/env_specific.toml b/config/deployments/env_specific.toml
index 5c9d237757..4901b701de 100644
--- a/config/deployments/env_specific.toml
+++ b/config/deployments/env_specific.toml
@@ -22,9 +22,12 @@ password = "db_pass" # Analytics DB Password
host = "localhost" # Analytics DB Host
port = 5432 # Analytics DB Port
dbname = "hyperswitch_db" # Name of Database
-pool_size = 5 # Number of connections to keep open
+max_pool_size = 5 # Maximum number of connections in the pool
+min_idle_pool_size = 2 # Minimum number of idle connections to maintain in the pool
connection_timeout = 10 # Timeout for database connection in seconds
queue_strategy = "Fifo" # Add the queue strategy used by the database bb8 client
+idle_timeout = 300 # Maximum idle duration in seconds before a connection is closed
+max_lifetime = 1800 # Maximum lifetime in seconds before a connection is reaped
[api_keys]
hash_key = "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef" # API key hashing key.
@@ -177,9 +180,12 @@ password = "db_pass" # DB Password. Use base-64 encoded kms encrypted value
host = "localhost" # DB Host
port = 5432 # DB Port
dbname = "hyperswitch_db" # Name of Database
-pool_size = 5 # Number of connections to keep open
+max_pool_size = 5 # Maximum number of connections in the pool
+min_idle_pool_size = 2 # Minimum number of idle connections to maintain in the pool
connection_timeout = 10 # Timeout for database connection in seconds
queue_strategy = "Fifo" # Add the queue strategy used by the database bb8 client
+idle_timeout = 300 # Maximum idle duration in seconds before a connection is closed
+max_lifetime = 1800 # Maximum lifetime in seconds before a connection is reaped
[generic_link]
[generic_link.payment_method_collect]
@@ -248,9 +254,12 @@ password = "db_pass" # DB Password. Use base-64 encoded kms encrypted value
host = "localhost" # DB Host
port = 5432 # DB Port
dbname = "hyperswitch_db" # Name of Database
-pool_size = 5 # Number of connections to keep open
+max_pool_size = 5 # Maximum number of connections in the pool
+min_idle_pool_size = 2 # Minimum number of idle connections to maintain in the pool
connection_timeout = 10 # Timeout for database connection in seconds
queue_strategy = "Fifo" # Add the queue strategy used by the database bb8 client
+idle_timeout = 300 # Maximum idle duration in seconds before a connection is closed
+max_lifetime = 1800 # Maximum lifetime in seconds before a connection is reaped
[report_download_config]
dispute_function = "report_download_config_dispute_function" # Config to download dispute report
diff --git a/config/development.toml b/config/development.toml
index 73f8e21339..c64e24a487 100644
--- a/config/development.toml
+++ b/config/development.toml
@@ -25,9 +25,11 @@ password = "db_pass"
host = "localhost"
port = 5432
dbname = "hyperswitch_db"
-pool_size = 5
+max_pool_size = 5
+min_idle_pool_size = 2
connection_timeout = 10
-min_idle = 2
+idle_timeout = 300
+max_lifetime = 1800
[replica_database]
username = "db_user"
@@ -35,8 +37,11 @@ password = "db_pass"
host = "localhost"
port = 5432
dbname = "hyperswitch_db"
-pool_size = 5
+max_pool_size = 5
+min_idle_pool_size = 2
connection_timeout = 10
+idle_timeout = 300
+max_lifetime = 1800
[redis]
host = "127.0.0.1"
@@ -1377,9 +1382,12 @@ password = "db_pass"
host = "localhost"
port = 5432
dbname = "hyperswitch_db"
-pool_size = 5
+max_pool_size = 5
+min_idle_pool_size = 2
connection_timeout = 10
queue_strategy = "Fifo"
+idle_timeout = 300
+max_lifetime = 1800
[connector_onboarding.paypal]
client_id = ""
diff --git a/config/docker_compose.toml b/config/docker_compose.toml
index e21e8f42e9..203d7a718d 100644
--- a/config/docker_compose.toml
+++ b/config/docker_compose.toml
@@ -32,7 +32,10 @@ password = "db_pass"
host = "pg"
port = 5432
dbname = "hyperswitch_db"
-pool_size = 5
+max_pool_size = 5
+min_idle_pool_size = 2
+idle_timeout = 300
+max_lifetime = 1800
[forex_api]
api_key = ""
@@ -47,7 +50,10 @@ password = "db_pass"
host = "pg"
port = 5432
dbname = "hyperswitch_db"
-pool_size = 5
+max_pool_size = 5
+min_idle_pool_size = 2
+idle_timeout = 300
+max_lifetime = 1800
[secrets]
admin_api_key = "test_admin"
@@ -1200,9 +1206,12 @@ password = "db_pass"
host = "pg"
port = 5432
dbname = "hyperswitch_db"
-pool_size = 5
+max_pool_size = 5
+min_idle_pool_size = 2
connection_timeout = 10
queue_strategy = "Fifo"
+idle_timeout = 300
+max_lifetime = 1800
[kv_config]
ttl = 900 # 15 * 60 seconds
diff --git a/crates/analytics/src/sqlx.rs b/crates/analytics/src/sqlx.rs
index 61d236b88b..22865739e1 100644
--- a/crates/analytics/src/sqlx.rs
+++ b/crates/analytics/src/sqlx.rs
@@ -59,8 +59,11 @@ impl SqlxClient {
let database_url = conf.get_database_url(schema);
#[allow(clippy::expect_used)]
let pool = PgPoolOptions::new()
- .max_connections(conf.pool_size)
+ .max_connections(conf.max_pool_size)
+ .min_connections(conf.min_idle_pool_size)
.acquire_timeout(std::time::Duration::from_secs(conf.connection_timeout))
+ .max_lifetime(std::time::Duration::from_secs(conf.max_lifetime))
+ .idle_timeout(std::time::Duration::from_secs(conf.idle_timeout))
.connect_lazy(&database_url)
.expect("SQLX Pool Creation failed");
Self { pool }
diff --git a/crates/router/src/configs/defaults.rs b/crates/router/src/configs/defaults.rs
index 14c4298e76..3a3d5cc4e4 100644
--- a/crates/router/src/configs/defaults.rs
+++ b/crates/router/src/configs/defaults.rs
@@ -42,11 +42,12 @@ impl Default for super::settings::Database {
host: "localhost".into(),
port: 5432,
dbname: String::new(),
- pool_size: 5,
+ max_pool_size: 5,
connection_timeout: 10,
queue_strategy: Default::default(),
- min_idle: None,
- max_lifetime: None,
+ min_idle_pool_size: 2,
+ max_lifetime: 1800,
+ idle_timeout: 300,
}
}
}
diff --git a/crates/router/src/configs/settings.rs b/crates/router/src/configs/settings.rs
index 682173115e..40d99bd9ef 100644
--- a/crates/router/src/configs/settings.rs
+++ b/crates/router/src/configs/settings.rs
@@ -865,11 +865,14 @@ pub struct Database {
pub host: String,
pub port: u16,
pub dbname: String,
- pub pool_size: u32,
+ #[serde(alias = "pool_size")]
+ pub max_pool_size: u32,
pub connection_timeout: u64,
pub queue_strategy: QueueStrategy,
- pub min_idle: Option<u32>,
- pub max_lifetime: Option<u64>,
+ #[serde(alias = "min_idle")]
+ pub min_idle_pool_size: u32,
+ pub max_lifetime: u64,
+ pub idle_timeout: u64,
}
impl From<Database> for storage_impl::config::Database {
@@ -880,11 +883,12 @@ impl From<Database> for storage_impl::config::Database {
host: val.host,
port: val.port,
dbname: val.dbname,
- pool_size: val.pool_size,
+ max_pool_size: val.max_pool_size,
connection_timeout: val.connection_timeout,
queue_strategy: val.queue_strategy,
- min_idle: val.min_idle,
+ min_idle_pool_size: val.min_idle_pool_size,
max_lifetime: val.max_lifetime,
+ idle_timeout: val.idle_timeout,
}
}
}
diff --git a/crates/storage_impl/src/config.rs b/crates/storage_impl/src/config.rs
index 170fe8fcb1..8ad3880eb7 100644
--- a/crates/storage_impl/src/config.rs
+++ b/crates/storage_impl/src/config.rs
@@ -11,11 +11,28 @@ pub struct Database {
pub host: String,
pub port: u16,
pub dbname: String,
- pub pool_size: u32,
+ #[serde(alias = "pool_size")]
+ pub max_pool_size: u32,
pub connection_timeout: u64,
pub queue_strategy: QueueStrategy,
- pub min_idle: Option<u32>,
- pub max_lifetime: Option<u64>,
+ #[serde(alias = "min_idle", default = "default_min_idle_pool_size")]
+ pub min_idle_pool_size: u32,
+ #[serde(default = "default_max_lifetime")]
+ pub max_lifetime: u64,
+ #[serde(default = "default_idle_timeout")]
+ pub idle_timeout: u64,
+}
+
+const fn default_min_idle_pool_size() -> u32 {
+ 2
+}
+
+const fn default_max_lifetime() -> u64 {
+ 1800
+}
+
+const fn default_idle_timeout() -> u64 {
+ 300
}
impl DbConnectionParams for Database {
@@ -61,11 +78,12 @@ impl Default for Database {
host: "localhost".into(),
port: 5432,
dbname: String::new(),
- pool_size: 5,
+ max_pool_size: 5,
connection_timeout: 10,
queue_strategy: QueueStrategy::default(),
- min_idle: None,
- max_lifetime: None,
+ min_idle_pool_size: default_min_idle_pool_size(),
+ max_lifetime: default_max_lifetime(),
+ idle_timeout: default_idle_timeout(),
}
}
}
diff --git a/crates/storage_impl/src/database/store.rs b/crates/storage_impl/src/database/store.rs
index ce8ddebddc..14a3d7318b 100644
--- a/crates/storage_impl/src/database/store.rs
+++ b/crates/storage_impl/src/database/store.rs
@@ -151,11 +151,12 @@ pub async fn diesel_make_pg_pool(
let database_url = database.get_database_url(schema);
let manager = async_bb8_diesel::ConnectionManager::<PgConnection>::new(database_url);
let mut pool = bb8::Pool::builder()
- .max_size(database.pool_size)
- .min_idle(database.min_idle)
+ .max_size(database.max_pool_size)
+ .min_idle(Some(database.min_idle_pool_size))
.queue_strategy(database.queue_strategy.into())
.connection_timeout(std::time::Duration::from_secs(database.connection_timeout))
- .max_lifetime(database.max_lifetime.map(std::time::Duration::from_secs));
+ .max_lifetime(std::time::Duration::from_secs(database.max_lifetime))
+ .idle_timeout(std::time::Duration::from_secs(database.idle_timeout));
if test_transaction {
pool = pool.connection_customizer(Box::new(TestTransaction));
diff --git a/loadtest/config/development.toml b/loadtest/config/development.toml
index 106033b5b1..d685b9e5dc 100644
--- a/loadtest/config/development.toml
+++ b/loadtest/config/development.toml
@@ -19,8 +19,11 @@ password = "postgres"
host = "db"
port = 5432
dbname = "loadtest_router"
-pool_size = 20
+max_pool_size = 20
connection_timeout = 10
+min_idle_pool_size = 2
+idle_timeout = 300
+max_lifetime = 1800
[server]
host = "0.0.0.0"
@@ -868,8 +871,11 @@ password = "db_pass"
host = "localhost"
port = 5432
dbname = "hyperswitch_db"
-pool_size = 5
+max_pool_size = 5
connection_timeout = 10
+min_idle_pool_size = 2
+idle_timeout = 300
+max_lifetime = 1800
[kv_config]
ttl = 300 # 5 * 60 seconds