Skip to content

Commit

Permalink
fix(store): Connection opts for specific databases (#384)
Browse files Browse the repository at this point in the history
  • Loading branch information
pedronauck authored and 0xterminator committed Jan 27, 2025
1 parent 5973927 commit e2560d4
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 10 deletions.
1 change: 1 addition & 0 deletions .env.sample
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# General Configuration
DB_TYPE=Cockroach
DB_POOL_SIZE=100
DB_TIMEOUT=240
STORE_PAGINATION_LIMIT=100
Expand Down
2 changes: 1 addition & 1 deletion cluster/charts/fuel-streams/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ apiVersion: v2
appVersion: "1.0"
description: A Helm chart for Kubernetes
name: fuel-streams
version: 0.9.3
version: 0.9.4
dependencies:
- name: nats
version: 1.2.9
Expand Down
1 change: 1 addition & 0 deletions cluster/charts/fuel-streams/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ commonConfigMap:
NATS_ADMIN_USER: "admin"
NATS_ADMIN_PASS: "admin"
# For local purposes only, for production use fuel-streams-keys secret
DB_TYPE: "Aurora"
DATABASE_URL: "postgresql://root:root@fuel-streams-cockroachdb:26257/fuel_streams?sslmode=disable"

# This is a secret that is used for local development
Expand Down
35 changes: 26 additions & 9 deletions crates/fuel-streams-store/src/db/db_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ impl Default for DbConnectionOpts {
pool_size: Some(*DB_POOL_SIZE as u32),
connection_str: dotenvy::var("DATABASE_URL")
.expect("DATABASE_URL not set"),
statement_timeout: Some(Duration::from_secs(30)),
statement_timeout: Some(Duration::from_secs(60)),
acquire_timeout: Some(Duration::from_secs(10)),
idle_timeout: Some(Duration::from_secs(180)),
idle_timeout: Some(Duration::from_secs(240)),
min_connections: Some((*DB_POOL_SIZE as u32) / 4),
}
}
Expand Down Expand Up @@ -92,19 +92,13 @@ impl Db {
}

async fn create_pool(opts: &DbConnectionOpts) -> DbResult<Pool<Postgres>> {
let statement_timeout =
opts.statement_timeout.unwrap_or_default().as_millis();
let statement_timeout = &format!("{}", statement_timeout);
let connections_opts =
sqlx::postgres::PgConnectOptions::from_str(&opts.connection_str)
.map_err(|e| {
DbError::Open(sqlx::Error::Configuration(Box::new(e)))
})?
.application_name("fuel-streams")
.options([
("retry_connect_backoff", "2"),
("statement_timeout", statement_timeout),
]);
.options(Self::connect_opts(&opts));

sqlx::postgres::PgPoolOptions::new()
.max_connections(opts.pool_size.unwrap_or_default())
Expand All @@ -116,4 +110,27 @@ impl Db {
.await
.map_err(DbError::Open)
}

fn connect_opts(opts: &DbConnectionOpts) -> Vec<(String, String)> {
let db_type =
dotenvy::var("DB_TYPE").expect("Database type need to be defined");
let statement_timeout =
opts.statement_timeout.unwrap_or_default().as_millis();
let statement_timeout = format!("{}", statement_timeout);
let idle_timeout = opts.idle_timeout.unwrap_or_default().as_millis();
let idle_timeout = format!("{}", idle_timeout);
let mut connect_opts = vec![];
if db_type == "Aurora" {
connect_opts.push((
"idle_in_transaction_session_timeout".to_string(),
idle_timeout,
));
}
if db_type == "Cockroach" {
connect_opts
.push(("retry_connect_backoff".to_string(), "2".to_string()));
}
connect_opts.push(("statement_timeout".to_string(), statement_timeout));
connect_opts
}
}

0 comments on commit e2560d4

Please sign in to comment.