Made database connection pool size configurable. Fixes #11

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-12 15:47:36 +02:00
co-authored by Claude Opus 5.5
parent e097d8ba51
commit 53c043a6b0
4 changed files with 42 additions and 2 deletions
+34 -1
View File
@@ -43,11 +43,18 @@ pub struct Database {
password: String,
db_name: String,
host: String,
port: Option<u16>
port: Option<u16>,
max_connections: Option<u32>
}
#[cfg(feature = "ssr")]
impl Database {
/// Upper limit of open database connections, 10 by default. With many instances on one
/// PostgreSQL server the sum of the limits must fit into its `max_connections`.
pub fn max_connections(&self) -> u32 {
self.max_connections.unwrap_or(10).max(1)
}
pub fn con_string(&self) -> String {
format!("postgres://{}:{}@{}:{}/{}", self.user, self.password, self.host, self.port.unwrap_or(5432), self.db_name)
}
@@ -358,6 +365,32 @@ mod tests {
assert_eq!(database.con_string(), "postgres://u:p@db.example.com:6543/db");
}
#[test]
fn max_connections_defaults_to_ten() {
let database: Database = toml::from_str(
"user = 'u'\npassword = 'p'\ndb_name = 'db'\nhost = 'localhost'").unwrap();
assert_eq!(database.max_connections(), 10);
// The sample config has the key commented out
assert_eq!(sample().database().max_connections(), 10);
}
#[test]
fn max_connections_can_be_configured() {
let database: Database = toml::from_str(
"user = 'u'\npassword = 'p'\ndb_name = 'db'\nhost = 'localhost'\nmax_connections = 3").unwrap();
assert_eq!(database.max_connections(), 3);
}
#[test]
fn max_connections_is_at_least_one() {
let database: Database = toml::from_str(
"user = 'u'\npassword = 'p'\ndb_name = 'db'\nhost = 'localhost'\nmax_connections = 0").unwrap();
assert_eq!(database.max_connections(), 1);
}
#[test]
fn smtp_mailing_config() {
let mailing: Mailing = toml::from_str(