-
Notifications
You must be signed in to change notification settings - Fork 850
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
Retry Safe/Read-Only Requests on Timeout #5278
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -119,11 +119,19 @@ impl From<Error> for std::io::Error { | |
|
||
pub type Result<T, E = Error> = std::result::Result<T, E>; | ||
|
||
/// Contains the configuration for how to respond to server errors | ||
/// The configuration for how to respond to request errors | ||
/// | ||
/// By default they will be retried up to some limit, using exponential | ||
/// The following categories of error will be retried: | ||
/// | ||
/// * 5xx server errors | ||
/// * Connection errors | ||
/// * Dropped connections | ||
/// * Timeouts for [safe] / read-only requests | ||
/// | ||
/// Requests will be retried up to some limit, using exponential | ||
/// backoff with jitter. See [`BackoffConfig`] for more information | ||
/// | ||
/// [safe]: https://datatracker.ietf.org/doc/html/rfc7231#section-4.2.1 | ||
#[derive(Debug, Clone)] | ||
pub struct RetryConfig { | ||
/// The backoff configuration | ||
|
@@ -173,13 +181,16 @@ impl RetryExt for reqwest::RequestBuilder { | |
let max_retries = config.max_retries; | ||
let retry_timeout = config.retry_timeout; | ||
|
||
let (client, req) = self.build_split(); | ||
let req = req.expect("request must be valid"); | ||
|
||
async move { | ||
let mut retries = 0; | ||
let now = Instant::now(); | ||
|
||
loop { | ||
let s = self.try_clone().expect("request body must be cloneable"); | ||
match s.send().await { | ||
let s = req.try_clone().expect("request body must be cloneable"); | ||
match client.execute(s).await { | ||
Ok(r) => match r.error_for_status_ref() { | ||
Ok(_) if r.status().is_success() => return Ok(r), | ||
Ok(r) if r.status() == StatusCode::NOT_MODIFIED => { | ||
|
@@ -242,7 +253,9 @@ impl RetryExt for reqwest::RequestBuilder { | |
Err(e) => | ||
{ | ||
let mut do_retry = false; | ||
if let Some(source) = e.source() { | ||
if req.method().is_safe() && e.is_timeout() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Technically we could retry PUT requests as they are idempotent, that is even if repeated they yield the same server-side state, however, in the presence of request preconditions this behaviour I think might be surprising to users even if it is technically correct. |
||
do_retry = true | ||
} else if let Some(source) = e.source() { | ||
if let Some(e) = source.downcast_ref::<hyper::Error>() { | ||
if e.is_connect() || e.is_closed() || e.is_incomplete_message() { | ||
do_retry = true; | ||
|
@@ -294,7 +307,11 @@ mod tests { | |
retry_timeout: Duration::from_secs(1000), | ||
}; | ||
|
||
let client = Client::new(); | ||
let client = Client::builder() | ||
.timeout(Duration::from_millis(100)) | ||
.build() | ||
.unwrap(); | ||
|
||
let do_request = || client.request(Method::GET, mock.url()).send_retry(&retry); | ||
|
||
// Simple request should work | ||
|
@@ -419,7 +436,7 @@ mod tests { | |
|
||
let e = do_request().await.unwrap_err().to_string(); | ||
assert!( | ||
e.contains("Error after 2 retries in") && | ||
e.contains("Error after 2 retries in") && | ||
e.contains("max_retries:2, retry_timeout:1000s, source:HTTP status server error (502 Bad Gateway) for url"), | ||
"{e}" | ||
); | ||
|
@@ -442,6 +459,25 @@ mod tests { | |
"{e}" | ||
); | ||
|
||
// Retries on client timeout | ||
mock.push_async_fn(|_| async move { | ||
tokio::time::sleep(Duration::from_secs(10)).await; | ||
panic!() | ||
}); | ||
do_request().await.unwrap(); | ||
|
||
// Does not retry PUT request | ||
mock.push_async_fn(|_| async move { | ||
tokio::time::sleep(Duration::from_secs(10)).await; | ||
panic!() | ||
}); | ||
let res = client.request(Method::PUT, mock.url()).send_retry(&retry); | ||
let e = res.await.unwrap_err().to_string(); | ||
assert!( | ||
e.contains("Error after 0 retries in") && e.contains("operation timed out"), | ||
"{e}" | ||
); | ||
|
||
// Shutdown | ||
mock.shutdown().await | ||
} | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Previously this would have panicked try_clone below, so this doesn't change the behaviour. The clients shouldn't be constructing requests that fail validation and so panicking here is the "correct" thing to do.