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

db: retry db connection #26

Merged
merged 2 commits into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
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
14 changes: 14 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
[workspace]
resolver = "2"

members = ["block-index", "chain", "shared", "orm", "webserver"]

[workspace.package]
Expand Down Expand Up @@ -43,3 +42,4 @@ tracing-appender = "0.2.0"
tracing-subscriber = { version = "0.3", features = [ "env-filter" ] }
validator = { version = "0.16.0", features = ["derive"] }
xorf = { version = "0.11.0", features = ["serde"]}
tryhard = { version = "0.5.1" }
1 change: 1 addition & 0 deletions block-index/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ tracing-appender.workspace = true
tracing-subscriber.workspace = true
tracing.workspace = true
xorf.workspace = true
tryhard.workspace = true

[build-dependencies]
vergen = { version = "8.0.0", features = ["build", "git", "gitcl"] }
38 changes: 26 additions & 12 deletions block-index/src/appstate.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::env;
use std::time::Duration;

use anyhow::Context;
use deadpool_diesel::postgres::{Object, Pool as DbPool};
Expand All @@ -9,22 +10,35 @@ pub struct AppState {
}

impl AppState {
pub fn new(db_url: String) -> anyhow::Result<Self> {
pub async fn new(db_url: String) -> anyhow::Result<Self> {
let max_pool_size = env::var("DATABASE_POOL_SIZE")
.unwrap_or_else(|_| 8.to_string())
.parse::<usize>()
.unwrap_or(8_usize);
let pool_manager = deadpool_diesel::Manager::from_config(
db_url,
deadpool_diesel::Runtime::Tokio1,
deadpool_diesel::ManagerConfig {
recycling_method: deadpool_diesel::RecyclingMethod::Verified,
},
);
let pool = DbPool::builder(pool_manager)
.max_size(max_pool_size)
.build()
.context("Failed to build Postgres db pool")?;

let max_conn_retries = env::var("DATABASE_MAX_CONN_RETRIES")
.unwrap_or_else(|_| 5.to_string())
.parse::<u32>()
.unwrap_or(5);

let pool = tryhard::retry_fn(|| async {
let pool_manager = deadpool_diesel::Manager::from_config(
db_url.clone(),
deadpool_diesel::Runtime::Tokio1,
deadpool_diesel::ManagerConfig {
recycling_method:
deadpool_diesel::RecyclingMethod::Verified,
},
);
DbPool::builder(pool_manager)
.max_size(max_pool_size)
.build()
.context("Failed to build Postgres db pool")
})
.retries(max_conn_retries)
.exponential_backoff(Duration::from_millis(100))
.max_delay(Duration::from_secs(5))
.await?;

Ok(Self { db: pool })
}
Expand Down
2 changes: 1 addition & 1 deletion block-index/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ async fn main() -> Result<(), MainError> {
tracing::info!(version = VERSION_STRING, "Started the block index builder");
let mut exit_handle = must_exit();

let app_state = AppState::new(database_url).into_db_error()?;
let app_state = AppState::new(database_url).await.into_db_error()?;

if wait_for_migrations(&mut exit_handle, &app_state)
.await
Expand Down
1 change: 1 addition & 0 deletions chain/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ tokio-retry.workspace = true
tokio.workspace = true
tracing-subscriber.workspace = true
tracing.workspace = true
tryhard.workspace = true

[build-dependencies]
vergen = { version = "8.0.0", features = ["build", "git", "gitcl"] }
35 changes: 26 additions & 9 deletions chain/src/appstate.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::env;
use std::time::Duration;

use anyhow::Context;
use deadpool_diesel::postgres::{Object, Pool as DbPool};
Expand All @@ -9,19 +10,35 @@ pub struct AppState {
}

impl AppState {
pub fn new(db_url: String) -> anyhow::Result<Self> {
pub async fn new(db_url: String) -> anyhow::Result<Self> {
let max_pool_size = env::var("DATABASE_POOL_SIZE")
.unwrap_or_else(|_| 8.to_string())
.parse::<usize>()
.unwrap_or(8_usize);
let pool_manager = deadpool_diesel::Manager::new(
db_url,
deadpool_diesel::Runtime::Tokio1,
);
let pool = DbPool::builder(pool_manager)
.max_size(max_pool_size)
.build()
.context("Failed to build Postgres db pool")?;

let max_conn_retries = env::var("DATABASE_MAX_CONN_RETRIES")
.unwrap_or_else(|_| 5.to_string())
.parse::<u32>()
.unwrap_or(5);

let pool = tryhard::retry_fn(|| async {
let pool_manager = deadpool_diesel::Manager::from_config(
db_url.clone(),
deadpool_diesel::Runtime::Tokio1,
deadpool_diesel::ManagerConfig {
recycling_method:
deadpool_diesel::RecyclingMethod::Verified,
},
);
DbPool::builder(pool_manager)
.max_size(max_pool_size)
.build()
.context("Failed to build Postgres db pool")
})
.retries(max_conn_retries)
.exponential_backoff(Duration::from_millis(100))
.max_delay(Duration::from_secs(5))
.await?;

Ok(Self { db: pool })
}
Expand Down
2 changes: 1 addition & 1 deletion chain/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ async fn main() -> Result<(), MainError> {
tracing::info!(version = VERSION_STRING, "Started the namada-masp-indexer");
let exit_handle = must_exit_handle();

let app_state = AppState::new(database_url).into_db_error()?;
let app_state = AppState::new(database_url).await.into_db_error()?;

run_migrations(&app_state).await?;

Expand Down
1 change: 1 addition & 0 deletions webserver/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ tracing-subscriber.workspace = true
tracing.workspace = true
validator.workspace = true
xorf.workspace = true
tryhard.workspace = true

[build-dependencies]
vergen = { version = "8.0.0", features = ["build", "git", "gitcl"] }
2 changes: 1 addition & 1 deletion webserver/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ impl ApplicationServer {
let rps = config.rps.unwrap_or_else(|| *REQ_PER_SEC);
let db_url = config.database_url.clone();

let app_state = AppState::new(db_url)?;
let app_state = AppState::new(db_url).await?;

let routes = {
let common_state = CommonState::new(app_state.clone());
Expand Down
38 changes: 26 additions & 12 deletions webserver/src/appstate.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::env;
use std::time::Duration;

use anyhow::Context;
use deadpool_diesel::postgres::{Object, Pool as DbPool};
Expand All @@ -9,22 +10,35 @@ pub struct AppState {
}

impl AppState {
pub fn new(db_url: String) -> anyhow::Result<Self> {
pub async fn new(db_url: String) -> anyhow::Result<Self> {
let max_pool_size = env::var("DATABASE_POOL_SIZE")
.unwrap_or_else(|_| 8.to_string())
.parse::<usize>()
.unwrap_or(8_usize);
let pool_manager = deadpool_diesel::Manager::from_config(
db_url,
deadpool_diesel::Runtime::Tokio1,
deadpool_diesel::ManagerConfig {
recycling_method: deadpool_diesel::RecyclingMethod::Verified,
},
);
let pool = DbPool::builder(pool_manager)
.max_size(max_pool_size)
.build()
.context("Failed to build Postgres db pool")?;

let max_conn_retries = env::var("DATABASE_MAX_CONN_RETRIES")
.unwrap_or_else(|_| 5.to_string())
.parse::<u32>()
.unwrap_or(5);

let pool = tryhard::retry_fn(|| async {
let pool_manager = deadpool_diesel::Manager::from_config(
db_url.clone(),
deadpool_diesel::Runtime::Tokio1,
deadpool_diesel::ManagerConfig {
recycling_method:
deadpool_diesel::RecyclingMethod::Verified,
},
);
DbPool::builder(pool_manager)
.max_size(max_pool_size)
.build()
.context("Failed to build Postgres db pool")
})
.retries(max_conn_retries)
.exponential_backoff(Duration::from_millis(100))
.max_delay(Duration::from_secs(5))
.await?;

Ok(Self { db: pool })
}
Expand Down