Adapt to retry-policies changes (#113)

pull/122/head^2
bbaldino 2024-03-05 04:05:19 -08:00 committed by GitHub
parent 8d40b60776
commit 4bdf56f2e0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 12 additions and 10 deletions

View File

@ -18,7 +18,7 @@ chrono = { version = "0.4.19", features = ["clock"], default-features = false }
futures = "0.3.0" futures = "0.3.0"
http = "0.2.0" http = "0.2.0"
reqwest = { version = "0.11.0", default-features = false } reqwest = { version = "0.11.0", default-features = false }
retry-policies = "0.2.0" retry-policies = "0.3.0"
task-local-extensions = "0.1.4" task-local-extensions = "0.1.4"
tracing = "0.1.26" tracing = "0.1.26"

View File

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