Skip to content

Commit

Permalink
Introduce pg pool options (#64)
Browse files Browse the repository at this point in the history
  • Loading branch information
piotr-iohk authored Nov 7, 2024
1 parent f7960bb commit de18efe
Showing 1 changed file with 27 additions and 2 deletions.
29 changes: 27 additions & 2 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,40 @@
use std::time::Duration;

use anyhow::Result;
use clap::{Args, Parser};
use coinbase_mesh::models::BlockIdentifier;
use sqlx::PgPool;
use sqlx::postgres::PgPoolOptions;

use crate::{graphql::GraphQLClient, util::default_mina_proxy_url, MinaMesh};

#[derive(Debug, Args)]
pub struct MinaMeshConfig {
/// The URL of the Mina GraphQL
#[arg(long, env = "MINAMESH_PROXY_URL", default_value_t = default_mina_proxy_url())]
pub proxy_url: String,

/// The URL of the Archive Database
#[arg(long, env = "MINAMESH_ARCHIVE_DATABASE_URL")]
pub archive_database_url: String,

/// The maximum number of concurrent connections allowed in the Archive
/// Database connection pool.
#[arg(long, env = "MINAMESH_MAX_DB_POOL_SIZE", default_value_t = 128)]
pub max_db_pool_size: u32,

/// The duration (in seconds) that an unused connection can remain idle in the
/// pool before being closed.
#[arg(long, env = "MINAMESH_DB_POOL_IDLE_TIMEOUT", default_value_t = 1)]
pub db_pool_idle_timeout: u64,

#[arg(long, env = "MINAMESH_GENESIS_BLOCK_IDENTIFIER_HEIGHT")]
pub genesis_block_identifier_height: i64,
#[arg(long, env = "MINAMESH_GENESIS_BLOCK_IDENTIFIER_STATE_HASH")]
pub genesis_block_identifier_state_hash: String,

/// Whether to use optimizations for searching transactions. Requires the
/// optimizations to be enabled via the `mina-mesh search-tx-optimizations`
/// command.
#[arg(long, env = "USE_SEARCH_TX_OPTIMIZATIONS", default_value = "false")]
pub use_search_tx_optimizations: bool,
}
Expand All @@ -34,7 +54,12 @@ impl MinaMeshConfig {
pub async fn to_mina_mesh(self) -> Result<MinaMesh> {
Ok(MinaMesh {
graphql_client: GraphQLClient::new(self.proxy_url.to_owned()),
pg_pool: PgPool::connect(self.archive_database_url.as_str()).await?,
pg_pool: PgPoolOptions::new()
.max_connections(self.max_db_pool_size)
.min_connections(0)
.idle_timeout(Duration::from_secs(self.db_pool_idle_timeout))
.connect(self.archive_database_url.as_str())
.await?,
genesis_block_identifier: BlockIdentifier::new(
self.genesis_block_identifier_height,
self.genesis_block_identifier_state_hash.to_owned(),
Expand Down

0 comments on commit de18efe

Please sign in to comment.