diff --git a/README.md b/README.md index a242117c80..5d929d4f2c 100644 --- a/README.md +++ b/README.md @@ -299,6 +299,7 @@ upcoming CLI tool. | PIP_TRUSTED_HOST | None | The trusted host to pass to pip. | Worker | | PATH | None | The path environment variable, usually inherited | Worker | | HOME | None | The home directory to use for Go and Bash , usually inherited | Worker | +| DATABASE_CONNECTIONS | 50 (Server)/3 (Worker) | The max number of connections in the database connection pool | All | ## Run a local dev setup diff --git a/backend/src/main.rs b/backend/src/main.rs index 4ce24718b3..1ce54999c1 100644 --- a/backend/src/main.rs +++ b/backend/src/main.rs @@ -21,8 +21,6 @@ async fn main() -> anyhow::Result<()> { windmill_common::tracing_init::initialize_tracing(); - let db = windmill_common::connect_db().await?; - let num_workers = std::env::var("NUM_WORKERS") .ok() .and_then(|x| x.parse::().ok()) @@ -43,6 +41,8 @@ async fn main() -> anyhow::Result<()> { .and_then(|x| x.parse::().ok()) .unwrap_or(false); + let db = windmill_common::connect_db(server_mode).await?; + if server_mode { windmill_api::migrate_db(&db).await?; } diff --git a/backend/windmill-api/src/main.rs b/backend/windmill-api/src/main.rs index 46fa33a6b4..e81e12861c 100644 --- a/backend/windmill-api/src/main.rs +++ b/backend/windmill-api/src/main.rs @@ -14,7 +14,7 @@ use anyhow::Ok; async fn main() -> anyhow::Result<()> { windmill_common::tracing_init::initialize_tracing(); - let db = windmill_common::connect_db().await?; + let db = windmill_common::connect_db(true).await?; let num_workers = std::env::var("NUM_WORKERS") .ok() diff --git a/backend/windmill-common/src/lib.rs b/backend/windmill-common/src/lib.rs index d94efa4801..046efb998c 100644 --- a/backend/windmill-common/src/lib.rs +++ b/backend/windmill-common/src/lib.rs @@ -29,7 +29,8 @@ pub mod tracing_init; pub const DEFAULT_NUM_WORKERS: usize = 3; pub const DEFAULT_TIMEOUT: i32 = 300; pub const DEFAULT_SLEEP_QUEUE: u64 = 50; -pub const DEFAULT_MAX_CONNECTIONS: u32 = 100; +pub const DEFAULT_MAX_CONNECTIONS_SERVER: u32 = 50; +pub const DEFAULT_MAX_CONNECTIONS_WORKER: u32 = 3; #[cfg(feature = "tokio")] pub async fn shutdown_signal(tx: tokio::sync::broadcast::Sender<()>) -> anyhow::Result<()> { @@ -79,7 +80,7 @@ async fn metrics() -> Result { } #[cfg(feature = "sqlx")] -pub async fn connect_db() -> anyhow::Result> { +pub async fn connect_db(server_mode: bool) -> anyhow::Result> { use anyhow::Context; let database_url = std::env::var("DATABASE_URL") @@ -87,7 +88,13 @@ pub async fn connect_db() -> anyhow::Result> { let max_connections = match std::env::var("DATABASE_CONNECTIONS") { Ok(n) => n.parse::().context("invalid DATABASE_CONNECTIONS")?, - Err(_) => DEFAULT_MAX_CONNECTIONS, + Err(_) => { + if server_mode { + DEFAULT_MAX_CONNECTIONS_SERVER + } else { + DEFAULT_MAX_CONNECTIONS_WORKER + } + } }; Ok(connect(&database_url, max_connections).await?) @@ -101,6 +108,7 @@ pub async fn connect( use std::time::Duration; sqlx::postgres::PgPoolOptions::new() + .min_connections(3) .max_connections(max_connections) .max_lifetime(Duration::from_secs(30 * 60)) // 30 mins .connect(database_url)