From 75b0dd98aa30874b3b7d39c122f49f7b53fc828c Mon Sep 17 00:00:00 2001 From: ngutech21 Date: Tue, 5 Mar 2024 07:57:58 +0100 Subject: [PATCH] feat: add env-variable for db max connections --- .env.example | 2 ++ integrationtests/tests/tests.rs | 1 + moksha-mint/src/config.rs | 14 +++++++++++++- moksha-mint/src/database/postgres.rs | 2 +- moksha-mint/src/mint.rs | 1 + moksha-mint/src/server.rs | 1 + 6 files changed, 19 insertions(+), 2 deletions(-) diff --git a/.env.example b/.env.example index e1b56bfc..236070f6 100644 --- a/.env.example +++ b/.env.example @@ -5,6 +5,8 @@ RUST_LOG=debug MINT_APP_ENV=dev # connection string for the postgres database MINT_DB_URL=postgres://postgres:postgres@127.0.0.1/moksha-mint +# Set the maximum number of connections that the pool should maintain (default 5) (optional) +MINT_DB_MAX_CONNECTIONS=5 # the private key of the mint MINT_PRIVATE_KEY=superprivatesecretkey diff --git a/integrationtests/tests/tests.rs b/integrationtests/tests/tests.rs index 1e74d5ba..b0217e7a 100644 --- a/integrationtests/tests/tests.rs +++ b/integrationtests/tests/tests.rs @@ -45,6 +45,7 @@ pub fn test_integration() -> anyhow::Result<()> { "postgres://postgres:postgres@localhost:{}/postgres", host_port ), + ..Default::default() }; let mint = MintBuilder::new() diff --git a/moksha-mint/src/config.rs b/moksha-mint/src/config.rs index 03fdd4cb..684780bd 100644 --- a/moksha-mint/src/config.rs +++ b/moksha-mint/src/config.rs @@ -235,10 +235,22 @@ impl From for Nut15 { } } -#[derive(Debug, Clone, Default, Parser)] +#[derive(Debug, Clone, Parser)] pub struct DatabaseConfig { #[clap(long, env = "MINT_DB_URL")] pub db_url: String, + + #[clap(long, default_value_t = 5, env = "MINT_DB_MAX_CONNECTIONS")] + pub max_connections: u32, +} + +impl Default for DatabaseConfig { + fn default() -> Self { + Self { + db_url: "".to_owned(), + max_connections: 5, + } + } } #[derive(Debug, Clone, Parser)] diff --git a/moksha-mint/src/database/postgres.rs b/moksha-mint/src/database/postgres.rs index 66eae87b..defb6d9f 100644 --- a/moksha-mint/src/database/postgres.rs +++ b/moksha-mint/src/database/postgres.rs @@ -25,7 +25,7 @@ impl PostgresDB { pub async fn new(config: &DatabaseConfig) -> Result { Ok(Self { pool: PgPoolOptions::new() - .max_connections(5) // FIXME make max connections configurable + .max_connections(config.max_connections) .connect(config.db_url.as_str()) .await?, }) diff --git a/moksha-mint/src/mint.rs b/moksha-mint/src/mint.rs index 862afe66..5d034cdf 100644 --- a/moksha-mint/src/mint.rs +++ b/moksha-mint/src/mint.rs @@ -744,6 +744,7 @@ mod tests { &format!("postgres://postgres:postgres@127.0.0.1:{}/postgres", port); let db = PostgresDB::new(&DatabaseConfig { db_url: connection_string.to_owned(), + ..Default::default() }) .await?; db.migrate().await; diff --git a/moksha-mint/src/server.rs b/moksha-mint/src/server.rs index 76ea51c8..c5fde75b 100644 --- a/moksha-mint/src/server.rs +++ b/moksha-mint/src/server.rs @@ -443,6 +443,7 @@ mod tests { &format!("postgres://postgres:postgres@127.0.0.1:{}/postgres", port); let db = PostgresDB::new(&DatabaseConfig { db_url: connection_string.to_owned(), + ..Default::default() }) .await?; db.migrate().await;