Skip to content

Commit c449804

Browse files
committed
feat: retry timed out broker requests
1 parent acd8da0 commit c449804

File tree

3 files changed

+24
-9
lines changed

3 files changed

+24
-9
lines changed

broker/src/crypto.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,11 @@ impl GetCertsFromPki {
4040
health: Arc<RwLock<Health>>,
4141
config: &Config,
4242
) -> Result<Self, SamplyBeamError> {
43-
let hyper_client = http_client::build(
43+
let hyper_client = http_client::builder(
4444
&config.tls_ca_certificates,
4545
Some(Duration::from_secs(30)),
4646
Some(Duration::from_secs(20)),
47-
)?;
47+
).build()?;
4848

4949
Ok(Self {
5050
pki_realm: config.pki_realm.clone(),

proxy/src/main.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,26 @@ pub async fn main() -> anyhow::Result<()> {
3636
banner::print_banner();
3737

3838
let config = Config::load()?;
39-
let client = http_client::build(
39+
let retry_policy = reqwest::retry::for_host(config.broker_uri.host_str().unwrap().to_string())
40+
.classify_fn(|res| {
41+
if res.method() != reqwest::Method::GET {
42+
return res.success();
43+
}
44+
if let Some(StatusCode::BAD_GATEWAY | StatusCode::SERVICE_UNAVAILABLE | StatusCode::GATEWAY_TIMEOUT) = res.status() {
45+
res.retryable()
46+
} else if res.error().and_then(|e| e.downcast_ref::<reqwest::Error>()).is_some_and(reqwest::Error::is_timeout) {
47+
res.retryable()
48+
} else {
49+
res.success()
50+
}
51+
})
52+
.max_retries_per_request(3);
53+
54+
let client = http_client::builder(
4055
&config.tls_ca_certificates,
4156
Some(Duration::from_secs(PROXY_TIMEOUT)),
4257
Some(Duration::from_secs(20)),
43-
)?;
58+
).retry(retry_policy).build()?;
4459

4560
if let Err(err) = retry_notify(|| get_broker_health(&config, &client), |err, dur| {
4661
warn!("Still trying to reach Broker: {err}. Retrying in {}s", dur.as_secs());

shared/src/http_client.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ use crate::{errors::SamplyBeamError};
1111

1212
pub type SamplyHttpClient = reqwest::Client;
1313

14-
pub fn build(
14+
pub fn builder(
1515
ca_certificates: &Vec<Certificate>,
1616
timeout: Option<Duration>,
1717
keepalive: Option<Duration>,
18-
) -> Result<SamplyHttpClient, SamplyBeamError> {
18+
) -> ClientBuilder {
1919
let mut builder = Client::builder().tcp_keepalive(keepalive);
2020
if let Some(to) = timeout {
2121
builder = builder.connect_timeout(to);
@@ -43,7 +43,7 @@ pub fn build(
4343
};
4444
info!("Using {proxies} and {certs} for TLS termination.");
4545

46-
builder.build().map_err(|e| SamplyBeamError::ConfigurationFailed(e.to_string()))
46+
builder
4747
}
4848

4949
#[cfg(test)]
@@ -60,13 +60,13 @@ mod test {
6060

6161
#[tokio::test]
6262
async fn https() {
63-
let client = http_client::build(&vec![], None, None).unwrap();
63+
let client = http_client::builder(&vec![], None, None).build().unwrap();
6464
run(HTTPS.parse().unwrap(), client).await;
6565
}
6666

6767
#[tokio::test]
6868
async fn http() {
69-
let client = http_client::build(&vec![], None, None).unwrap();
69+
let client = http_client::builder(&vec![], None, None).build().unwrap();
7070
run(HTTP.parse().unwrap(), client).await;
7171
}
7272

0 commit comments

Comments
 (0)