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

Set limits to PgPool #33

Merged
merged 1 commit into from
Jan 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
Comment on lines +10 to +11
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a bit undecided whether or not these should be configurable.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You'll decide when you'll need to configure it. It's easy to add some configuration and use these as default value when needed. If you don't have a good reason for now, let the things like that.


#[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