Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce pg pool options #64

Merged
merged 3 commits into from
Nov 7, 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
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