Skip to content
This repository has been archived by the owner on Dec 17, 2024. It is now read-only.

Commit

Permalink
Set limits to PgPool
Browse files Browse the repository at this point in the history
- Maximum number of connections (64).
- Connection timeout (750ms).
  • Loading branch information
tuommaki committed Jan 12, 2024
1 parent 75ae3c2 commit 932554b
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions crates/node/src/storage/database/postgres.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -13,7 +18,11 @@ pub struct Database {
// TODO: Split this into domain specific components.
impl Database {
pub async fn new(db_url: &str) -> Result<Database> {
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 })
}

Expand Down

0 comments on commit 932554b

Please sign in to comment.