Skip to content

Commit

Permalink
Adapt to retry-policies changes
Browse files Browse the repository at this point in the history
  • Loading branch information
bbaldino committed Mar 4, 2024
1 parent 1c31f4b commit 3a91398
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
2 changes: 1 addition & 1 deletion reqwest-retry/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ chrono = { version = "0.4.19", features = ["clock"], default-features = false }
futures = "0.3.0"
http = "0.2.0"
reqwest = { version = "0.11.0", default-features = false }
retry-policies = "0.2.0"
retry-policies = "0.3.0"
task-local-extensions = "0.1.4"
tracing = "0.1.26"

Expand Down
20 changes: 11 additions & 9 deletions reqwest-retry/src/middleware.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,19 @@ use task_local_extensions::Extensions;
/// runtime that supports them.
///
///```rust
/// use std::time::Duration;
/// use reqwest_middleware::ClientBuilder;
/// use reqwest_retry::{RetryTransientMiddleware, policies::ExponentialBackoff};
/// use retry_policies::{RetryDecision, RetryPolicy, Jitter};
/// use retry_policies::policies::ExponentialBackoff;
/// use reqwest_retry::RetryTransientMiddleware;
/// use reqwest::Client;
///
/// // We create a ExponentialBackoff retry policy which implements `RetryPolicy`.
/// let retry_policy = ExponentialBackoff {
/// /// How many times the policy will tell the middleware to retry the request.
/// max_n_retries: 3,
/// max_retry_interval: std::time::Duration::from_millis(30),
/// min_retry_interval: std::time::Duration::from_millis(100),
/// backoff_exponent: 2,
/// };
/// let retry_policy = ExponentialBackoff::builder()
/// .retry_bounds(Duration::from_secs(1), Duration::from_secs(60))
/// .jitter(Jitter::Bounded)
/// .base(2)
/// .build_with_total_retry_duration(Duration::from_secs(24 * 60 * 60));
///
/// let retry_transient_middleware = RetryTransientMiddleware::new_with_policy(retry_policy);
/// let client = ClientBuilder::new(Client::new()).with(retry_transient_middleware).build();
Expand Down Expand Up @@ -111,6 +112,7 @@ where
ext: &'a mut Extensions,
) -> Result<Response> {
let mut n_past_retries = 0;
let start_time = Utc::now();
loop {
// Cloning the request object before-the-fact is not ideal..
// However, if the body of the request is not static, e.g of type `Bytes`,
Expand All @@ -130,7 +132,7 @@ where
Some(Retryable::Transient) => {
// If the response failed and the error type was transient
// we can safely try to retry the request.
let retry_decision = self.retry_policy.should_retry(n_past_retries);
let retry_decision = self.retry_policy.should_retry(start_time, n_past_retries);
if let retry_policies::RetryDecision::Retry { execute_after } = retry_decision {
let duration = (execute_after - Utc::now())
.to_std()
Expand Down

0 comments on commit 3a91398

Please sign in to comment.