From 932554b09aa78c6edadc293d1c831e842ffa85f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tuomas=20M=C3=A4kinen?= Date: Fri, 12 Jan 2024 09:58:54 +0200 Subject: [PATCH] Set limits to PgPool - Maximum number of connections (64). - Connection timeout (750ms). --- crates/node/src/storage/database/postgres.rs | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/crates/node/src/storage/database/postgres.rs b/crates/node/src/storage/database/postgres.rs index ec480b6b..e9c0b571 100644 --- a/crates/node/src/storage/database/postgres.rs +++ b/crates/node/src/storage/database/postgres.rs @@ -1,10 +1,15 @@ +use std::time::Duration; + use eyre::Result; -use sqlx::{self, Row}; +use sqlx::{self, postgres::PgPoolOptions, Row}; use uuid::Uuid; use super::entity::{self}; use crate::types::{self, transaction::ProgramData, File, Hash, Program, Task}; +const MAX_DB_CONNS: u32 = 64; +const DB_CONNECT_TIMEOUT: Duration = Duration::from_millis(750); + #[derive(Clone)] pub struct Database { pool: sqlx::PgPool, @@ -13,7 +18,11 @@ pub struct Database { // TODO: Split this into domain specific components. impl Database { pub async fn new(db_url: &str) -> Result { - let pool = sqlx::PgPool::connect(db_url).await?; + let pool = PgPoolOptions::new() + .max_connections(MAX_DB_CONNS) + .acquire_timeout(DB_CONNECT_TIMEOUT) + .connect(db_url) + .await?; Ok(Database { pool }) }