From 71814376250ccc1d5f1ec1518138d5edfa24dd7d Mon Sep 17 00:00:00 2001 From: Anders Olsson Date: Thu, 4 Jun 2026 21:46:41 +0200 Subject: [PATCH] feat(server): configurable DB pool size via --db-max-connections/DB_MAX_CONNECTIONS (#2) Co-Authored-By: Claude Sonnet 4.6 --- crates/db/src/lib.rs | 7 ++++--- crates/server/src/config.rs | 8 ++++++++ crates/server/src/lib.rs | 5 +++-- crates/server/tests/serve.rs | 2 +- 4 files changed, 16 insertions(+), 6 deletions(-) diff --git a/crates/db/src/lib.rs b/crates/db/src/lib.rs index 17267b8..0f1c2fa 100644 --- a/crates/db/src/lib.rs +++ b/crates/db/src/lib.rs @@ -17,10 +17,11 @@ pub struct Db { } impl Db { - /// Connect to the database at `database_url`, opening a connection pool. - pub async fn connect(database_url: &str) -> Result { + /// Connect to the database at `database_url`, opening a connection pool with at most + /// `max_connections` connections. + pub async fn connect(database_url: &str, max_connections: u32) -> Result { let pool = PgPoolOptions::new() - .max_connections(5) + .max_connections(max_connections) .connect(database_url) .await?; diff --git a/crates/server/src/config.rs b/crates/server/src/config.rs index 0182938..08e809e 100644 --- a/crates/server/src/config.rs +++ b/crates/server/src/config.rs @@ -42,4 +42,12 @@ pub struct Config { /// Meilisearch index name for catalogue objects. #[arg(long = "meili-index", env = "MEILI_INDEX", default_value = "objects")] pub meili_index: String, + + /// Maximum size of the PostgreSQL connection pool. + #[arg( + long = "db-max-connections", + env = "DB_MAX_CONNECTIONS", + default_value_t = 5 + )] + pub db_max_connections: u32, } diff --git a/crates/server/src/lib.rs b/crates/server/src/lib.rs index ff2d3bc..53951b2 100644 --- a/crates/server/src/lib.rs +++ b/crates/server/src/lib.rs @@ -15,7 +15,7 @@ use tokio::net::TcpListener; /// Connect dependencies from `config` and serve until shutdown. pub async fn run(config: Config) -> anyhow::Result<()> { - let db = Db::connect(&config.database_url) + let db = Db::connect(&config.database_url, config.db_max_connections) .await .context("connecting to the database")?; @@ -136,7 +136,8 @@ pub async fn create_user(database_url: &str, email: &str, role: Role) -> anyhow: auth::hash_password(&password).map_err(|err| anyhow::anyhow!("hashing password: {err}"))? }; - let db = Db::connect(database_url) + // CLI one-shot: a tiny pool is plenty. + let db = Db::connect(database_url, 2) .await .context("connecting to the database")?; diff --git a/crates/server/tests/serve.rs b/crates/server/tests/serve.rs index b3dd99d..edf402a 100644 --- a/crates/server/tests/serve.rs +++ b/crates/server/tests/serve.rs @@ -9,7 +9,7 @@ use tokio::net::TcpListener; async fn serves_health_live_over_tcp() { let database_url = std::env::var("DATABASE_URL").expect("DATABASE_URL must be set for this test"); - let db = Db::connect(&database_url) + let db = Db::connect(&database_url, 2) .await .expect("connect to database"); let state = AppState {