aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRob Watson <[email protected]>2020-10-06 15:23:55 +0200
committerRob Watson <[email protected]>2020-10-09 10:29:02 +0200
commitb9daa59e5d05f1050e6ba4199352953493a57dfb (patch)
tree75de90038993be019f1c17de6bdb1985d9b8cecf
parent1842a796fb90ce401e9bde8e52974638121ac113 (diff)
downloadvaultwarden-b9daa59e5d05f1050e6ba4199352953493a57dfb.tar.gz
vaultwarden-b9daa59e5d05f1050e6ba4199352953493a57dfb.zip
Add DATABASE_MAX_CONNS config setting
-rw-r--r--.env.template4
-rw-r--r--src/config.rs10
-rw-r--r--src/db/mod.rs5
3 files changed, 18 insertions, 1 deletions
diff --git a/.env.template b/.env.template
index 8f134a98..4d987c4e 100644
--- a/.env.template
+++ b/.env.template
@@ -20,6 +20,10 @@
## - https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNSTRING
# DATABASE_URL=postgresql://user:password@host[:port]/database_name
+## Database max connections
+## Define the size of the connection pool used for connecting to the database.
+# DATABASE_MAX_CONNS=10
+
## Individual folders, these override %DATA_FOLDER%
# RSA_KEY_FILENAME=data/rsa_key
# ICON_CACHE_FOLDER=data/icon_cache
diff --git a/src/config.rs b/src/config.rs
index 23f37462..09f4ae2e 100644
--- a/src/config.rs
+++ b/src/config.rs
@@ -222,6 +222,8 @@ make_config! {
data_folder: String, false, def, "data".to_string();
/// Database URL
database_url: String, false, auto, |c| format!("{}/{}", c.data_folder, "db.sqlite3");
+ /// Database connection pool size
+ database_max_conns: u32, false, def, 10;
/// Icon cache folder
icon_cache_folder: String, false, auto, |c| format!("{}/{}", c.data_folder, "icon_cache");
/// Attachments folder
@@ -429,6 +431,14 @@ fn validate_config(cfg: &ConfigItems) -> Result<(), Error> {
// Validate connection URL is valid and DB feature is enabled
DbConnType::from_url(&cfg.database_url)?;
+ let limit = 256;
+ if cfg.database_max_conns < 1 || cfg.database_max_conns > limit {
+ err!(format!(
+ "`DATABASE_MAX_CONNS` contains an invalid value. Ensure it is between 1 and {}.",
+ limit,
+ ));
+ }
+
let dom = cfg.domain.to_lowercase();
if !dom.starts_with("http://") && !dom.starts_with("https://") {
err!("DOMAIN variable needs to contain the protocol (http, https). Use 'http[s]://bw.example.com' instead of 'bw.example.com'");
diff --git a/src/db/mod.rs b/src/db/mod.rs
index 8c95b39b..f9dc2885 100644
--- a/src/db/mod.rs
+++ b/src/db/mod.rs
@@ -51,7 +51,10 @@ macro_rules! generate_connections {
{
paste::paste!{ [< $name _migrations >]::run_migrations()?; }
let manager = ConnectionManager::new(&url);
- let pool = Pool::builder().build(manager).map_res("Failed to create pool")?;
+ let pool = Pool::builder()
+ .max_size(CONFIG.database_max_conns())
+ .build(manager)
+ .map_res("Failed to create pool")?;
return Ok(Self::$name(pool));
}
#[cfg(not($name))]