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

Perform exponential backoff with jitter when retrying requests #632

Merged
merged 1 commit into from
Feb 1, 2025
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
88 changes: 85 additions & 3 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 Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ http = "1.1.0"
log = "0.4.22"
model = { path = "crates/model" }
oauth2 = "4.4.2"
rand = "0.9.0"
rayon = "1.10.0"
reqwest = { version = "0.11.23", default-features = false, features = [ "blocking", "json", "rustls-tls", ] }
rusoto_core = { version = "0.48.0", default-features = false, features = [ "rustls", ] }
Expand Down
38 changes: 32 additions & 6 deletions src/retry.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,54 @@
use super::*;

pub(crate) trait Retry {
fn retry(self, retries: usize) -> Result<reqwest::blocking::Response>;
fn retry(self, max_retries: usize) -> Result<reqwest::blocking::Response>;
}

impl Retry for RequestBuilder {
fn retry(self, retries: usize) -> Result<reqwest::blocking::Response> {
fn retry(self, max_retries: usize) -> Result<reqwest::blocking::Response> {

Check warning on line 8 in src/retry.rs

View check run for this annotation

Codecov / codecov/patch

src/retry.rs#L8

Added line #L8 was not covered by tests
let mut attempts = 0;

while attempts <= retries {
let (base_delay, max_delay) =
(Duration::from_millis(100), Duration::from_secs(30));

Check warning on line 12 in src/retry.rs

View check run for this annotation

Codecov / codecov/patch

src/retry.rs#L11-L12

Added lines #L11 - L12 were not covered by tests

while attempts <= max_retries {

Check warning on line 14 in src/retry.rs

View check run for this annotation

Codecov / codecov/patch

src/retry.rs#L14

Added line #L14 was not covered by tests
match self
.try_clone()
.ok_or_else(|| anyhow!("Failed to clone request builder"))?
.send()
{
Ok(response) => return Ok(response),
Err(error) => {
error!("Request failed: {error}");
error!(
"Request failed (attempt {}/{}): {}",
attempts + 1,
max_retries + 1,

Check warning on line 25 in src/retry.rs

View check run for this annotation

Codecov / codecov/patch

src/retry.rs#L22-L25

Added lines #L22 - L25 were not covered by tests
error
);

if attempts == max_retries {
break;
}

let (exp_delay, jitter) = (
base_delay * (2_u32.pow(attempts as u32)),
Duration::from_millis(rand::random::<u64>() % 100),
);

let delay = std::cmp::min(exp_delay + jitter, max_delay);

warn!("Retrying in {:?}...", delay);

Check warning on line 40 in src/retry.rs

View check run for this annotation

Codecov / codecov/patch

src/retry.rs#L29-L40

Added lines #L29 - L40 were not covered by tests

thread::sleep(delay);

Check warning on line 43 in src/retry.rs

View check run for this annotation

Codecov / codecov/patch

src/retry.rs#L42-L43

Added lines #L42 - L43 were not covered by tests
attempts += 1;
thread::sleep(Duration::from_secs(1));
}
}
}

Err(Error(anyhow!("Request timed out")))
Err(Error(anyhow!(
"Request failed after {} attempts",
max_retries + 1
)))

Check warning on line 52 in src/retry.rs

View check run for this annotation

Codecov / codecov/patch

src/retry.rs#L49-L52

Added lines #L49 - L52 were not covered by tests
}
}