DATABASE_CONNECTIONS sets Pool max_connections (#376)

If unspecified, defaults to the current value of 100
This commit is contained in:
sqwishy
2022-08-09 00:47:48 -07:00
committed by GitHub
parent 2247b133c2
commit 6b2ff5a87f
2 changed files with 11 additions and 3 deletions

View File

@@ -12,9 +12,9 @@ use std::time::Duration;
pub type DB = Pool<Postgres>;
pub async fn connect(database_url: &str) -> Result<DB, Error> {
pub async fn connect(database_url: &str, max_connections: u32) -> Result<DB, Error> {
PgPoolOptions::new()
.max_connections(100)
.max_connections(max_connections)
.max_lifetime(Duration::from_secs(30 * 60)) // 30 mins
.connect(database_url)
.await

View File

@@ -6,6 +6,7 @@
* LICENSE-AGPL for a copy of the license.
*/
use anyhow::Context;
use argon2::Argon2;
use axum::{handler::Handler, middleware::from_extractor, routing::get, Extension, Router};
use db::DB;
@@ -62,6 +63,7 @@ const GIT_VERSION: &str = git_version!(args = ["--tag", "--always"], fallback =
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 async fn migrate_db(db: &DB) -> anyhow::Result<()> {
let app_password = std::env::var("APP_USER_PASSWORD").unwrap_or_else(|_| "changeme".to_owned());
@@ -74,7 +76,13 @@ pub async fn migrate_db(db: &DB) -> anyhow::Result<()> {
pub async fn connect_db() -> anyhow::Result<DB> {
let database_url = std::env::var("DATABASE_URL")
.map_err(|_| Error::BadConfig("DATABASE_URL env var is missing".to_string()))?;
Ok(db::connect(&database_url).await?)
let max_connections = match std::env::var("DATABASE_CONNECTIONS") {
Ok(n) => n.parse::<u32>().context("invalid DATABASE_CONNECTIONS")?,
Err(_) => DEFAULT_MAX_CONNECTIONS,
};
Ok(db::connect(&database_url, max_connections).await?)
}
pub async fn initialize_tracing() -> anyhow::Result<()> {