Skip to content

Commit d0ff2c3

Browse files
committed
Add retry mechanism to connect to PostgreSQL before starting maplibre#1539
1 parent 6f201fc commit d0ff2c3

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

Cargo.lock

+12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

martin/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ tilejson.workspace = true
113113
tokio = { workspace = true, features = ["io-std"] }
114114
tokio-postgres-rustls = { workspace = true, optional = true }
115115
url.workspace = true
116+
tokio-retry = "0.3.0"
116117

117118
[build-dependencies]
118119
static-files = { workspace = true, optional = true }

martin/src/pg/config.rs

+15-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ use crate::pg::utils::on_slow;
1515
use crate::pg::PgResult;
1616
use crate::source::TileInfoSources;
1717
use crate::utils::{IdResolver, OptBoolObj, OptOneMany};
18-
use crate::MartinResult;
18+
use crate::{MartinError, MartinResult};
19+
use tokio_retry::strategy::{jitter, FixedInterval};
20+
use tokio_retry::Retry;
1921

2022
pub trait PgInfo {
2123
fn format_id(&self) -> String;
@@ -114,7 +116,18 @@ impl PgConfig {
114116
}
115117

116118
pub async fn resolve(&mut self, id_resolver: IdResolver) -> MartinResult<TileInfoSources> {
117-
let pg = PgBuilder::new(self, id_resolver).await?;
119+
// Retry strategy: Fixed 5 seconds interval backoff with jitter (random variation)
120+
let retry_strategy = FixedInterval::from_millis(5000)
121+
.map(jitter) // Add random jitter to avoid "thundering herd" problem
122+
.take(3); // Retry up to 3 times
123+
124+
// Create PgBuilder using retry_strategy
125+
let pg = Retry::spawn(retry_strategy, || async {
126+
PgBuilder::new(self, id_resolver.clone()).await
127+
})
128+
.await
129+
.map_err(|e| MartinError::PostgresError(e))?;
130+
118131
let inst_tables = on_slow(
119132
pg.instantiate_tables(),
120133
// warn only if default bounds timeout has already passed

0 commit comments

Comments
 (0)