From dd8f427bc76431f1b51426289e29cc9d195a3f34 Mon Sep 17 00:00:00 2001 From: Lev Kokotov Date: Thu, 18 May 2023 15:06:54 -0700 Subject: [PATCH] Allow configuring dashboard connection pool settings --- pgml-dashboard/src/lib.rs | 20 ++++++++++++++++---- pgml-dashboard/src/main.rs | 16 +++++++++++++++- 2 files changed, 31 insertions(+), 5 deletions(-) diff --git a/pgml-dashboard/src/lib.rs b/pgml-dashboard/src/lib.rs index 27ed0fb5b..f8e747d4e 100644 --- a/pgml-dashboard/src/lib.rs +++ b/pgml-dashboard/src/lib.rs @@ -24,6 +24,13 @@ use guards::Cluster; use responses::{BadRequest, ResponseOk}; use sqlx::Executor; +#[derive(Debug, Default, Clone)] +pub struct ClustersSettings { + pub max_connections: u32, + pub idle_timeout: u64, + pub min_connections: u32, +} + /// This struct contains information specific to the cluster being displayed in the dashboard. /// /// The dashboard is built to manage multiple clusters, but the server itself by design is stateless. @@ -44,13 +51,18 @@ pub struct Clusters { } impl Clusters { - pub fn add(&self, cluster_id: i64, database_url: &str) -> anyhow::Result { + pub fn add( + &self, + cluster_id: i64, + database_url: &str, + settings: ClustersSettings, + ) -> anyhow::Result { let mut pools = self.pools.lock(); let pool = PgPoolOptions::new() - .max_connections(5) - .idle_timeout(std::time::Duration::from_millis(15_000)) - .min_connections(0) + .max_connections(settings.max_connections) + .idle_timeout(std::time::Duration::from_millis(settings.idle_timeout)) + .min_connections(settings.min_connections) .after_connect(|conn, _meta| { Box::pin(async move { conn.execute("SET application_name = 'pgml_dashboard';") diff --git a/pgml-dashboard/src/main.rs b/pgml-dashboard/src/main.rs index ac466ddfb..4356a3e9a 100644 --- a/pgml-dashboard/src/main.rs +++ b/pgml-dashboard/src/main.rs @@ -11,8 +11,18 @@ async fn main() { dotenv::dotenv().ok(); let clusters = pgml_dashboard::Clusters::new(); + let settings = pgml_dashboard::ClustersSettings { + min_connections: 0, + max_connections: 5, + idle_timeout: 15_000, + }; + clusters - .add(-1, &pgml_dashboard::guards::default_database_url()) + .add( + -1, + &pgml_dashboard::guards::default_database_url(), + settings, + ) .unwrap(); pgml_dashboard::migrate(&clusters.get(-1).unwrap()) @@ -43,6 +53,10 @@ mod test { use std::vec::Vec; async fn rocket() -> Rocket { + let max_connections = 5; + let min_connections = 1; + let idle_timeout = 15_000; + let clusters = Clusters::new(); clusters .add(-1, &pgml_dashboard::guards::default_database_url()) pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy