Made database connection pool size configurable. Fixes #11
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
+34
-1
@@ -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(
|
||||
|
||||
+1
-1
@@ -77,7 +77,7 @@ async fn main() -> std::io::Result<()> {
|
||||
let key = Key::from(srv_conf.session().key());
|
||||
|
||||
let pool = PgPoolOptions::new()
|
||||
.max_connections(10)
|
||||
.max_connections(srv_conf.database().max_connections())
|
||||
.connect(&srv_conf.database().con_string()).await.unwrap();
|
||||
migrate!().run(&pool).await.expect("could not run SQLx migrations");
|
||||
|
||||
|
||||
Reference in New Issue
Block a user