Skip to content

Commit

Permalink
Add retry mechanism to connect to PostgreSQL before starting maplibre (
Browse files Browse the repository at this point in the history
  • Loading branch information
teyotan committed Oct 21, 2024
1 parent 6f201fc commit 953f23f
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
12 changes: 12 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions martin/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ tilejson.workspace = true
tokio = { workspace = true, features = ["io-std"] }
tokio-postgres-rustls = { workspace = true, optional = true }
url.workspace = true
tokio-retry = "0.3.0"

[build-dependencies]
static-files = { workspace = true, optional = true }
Expand Down
17 changes: 15 additions & 2 deletions martin/src/pg/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ use crate::pg::utils::on_slow;
use crate::pg::PgResult;
use crate::source::TileInfoSources;
use crate::utils::{IdResolver, OptBoolObj, OptOneMany};
use crate::MartinResult;
use crate::{MartinError, MartinResult};
use tokio_retry::strategy::{jitter, FixedInterval};
use tokio_retry::Retry;

pub trait PgInfo {
fn format_id(&self) -> String;
Expand Down Expand Up @@ -114,7 +116,18 @@ impl PgConfig {
}

pub async fn resolve(&mut self, id_resolver: IdResolver) -> MartinResult<TileInfoSources> {
let pg = PgBuilder::new(self, id_resolver).await?;
// Retry strategy: Fixed 5 seconds interval backoff with jitter (random variation)
let retry_strategy = FixedInterval::from_millis(5000)
.map(jitter) // Add random jitter to avoid "thundering herd" problem
.take(3); // Retry up to 3 times

// Create PgBuilder using retry_strategy
let pg = Retry::spawn(retry_strategy, || async {
PgBuilder::new(self, id_resolver.clone()).await
})
.await
.map_err(MartinError::PostgresError)?;

let inst_tables = on_slow(
pg.instantiate_tables(),
// warn only if default bounds timeout has already passed
Expand Down

0 comments on commit 953f23f

Please sign in to comment.