Repository navigation
fix(http): improve transient transport retries #293
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
Changes from all commits
e38c58c
d355ea7
1e888e0
46171fb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -241,12 +241,10 @@ impl RetryStrategy { | |
|
|
||
| /// Whether the given network/client error is retryable. | ||
| pub fn is_retryable_error(&self, error: &reqwest::Error) -> bool { | ||
| // A DNS *resolution* failure (the host name does not resolve) is | ||
| // permanent — retrying it just burns the whole retry budget on a host | ||
| // that will never come back. reqwest reports it as a "connect" error, | ||
| // which the branches below would otherwise treat as retryable, so | ||
| // short-circuit it here regardless of idempotency. | ||
| if is_dns_resolution_failure(error) { | ||
| // A permanent DNS lookup failure (for example NXDOMAIN) cannot recover | ||
| // through retry. Temporary resolver failures remain ordinary connect | ||
| // errors and follow the request's idempotency policy below. | ||
| if is_permanent_dns_resolution_failure(error) { | ||
| return false; | ||
| } | ||
| match self.idempotency { | ||
|
|
@@ -260,35 +258,31 @@ impl RetryStrategy { | |
| } | ||
| } | ||
|
|
||
| /// Heuristic: is this network error a DNS *resolution* failure (the host name | ||
| /// does not resolve), as opposed to a transient connect/timeout error? | ||
| /// Heuristic: is this network error a permanent DNS lookup failure (the host | ||
| /// name does not exist), as opposed to a temporary resolver/connect error? | ||
| /// | ||
| /// A non-existent host (NXDOMAIN) will never resolve, so it must not be | ||
| /// retried. reqwest does not expose a typed DNS-error variant, so we walk the | ||
| /// error's source chain and match the resolver's message. Matching is broad | ||
| /// and lowercased to cover the common phrasings across platforms/resolvers: | ||
| /// libc/getaddrinfo (`Name or service not known`), macOS | ||
| /// (`nodename nor servname provided`), and hyper/hickory | ||
| /// (`dns error`, `failed to lookup address information`). | ||
| fn is_dns_resolution_failure(error: &reqwest::Error) -> bool { | ||
| error_chain_indicates_dns_failure(error) | ||
| /// error's source chain and match only messages that identify an unknown host. | ||
| /// Generic markers such as `dns error` and `name resolution` are deliberately | ||
| /// excluded because they also describe temporary resolver failures. | ||
| fn is_permanent_dns_resolution_failure(error: &reqwest::Error) -> bool { | ||
| error_chain_indicates_permanent_dns_failure(error) | ||
| } | ||
|
|
||
| /// Walk an error's `source()` chain looking for a DNS-resolution marker. | ||
| /// Walk an error's `source()` chain looking for a permanent DNS marker. | ||
| /// | ||
| /// Split out from [`is_dns_resolution_failure`] so it can be unit-tested with a | ||
| /// synthetic error chain — `reqwest::Error` has no public constructor, so the | ||
| /// matching logic can't be exercised through the typed wrapper directly. | ||
| fn error_chain_indicates_dns_failure(error: &(dyn std::error::Error + 'static)) -> bool { | ||
| /// Split out from [`is_permanent_dns_resolution_failure`] so it can be tested | ||
| /// with a synthetic error chain; `reqwest::Error` has no public constructor. | ||
| fn error_chain_indicates_permanent_dns_failure(error: &(dyn std::error::Error + 'static)) -> bool { | ||
| let mut source: Option<&(dyn std::error::Error + 'static)> = Some(error); | ||
| while let Some(err) = source { | ||
| let msg = err.to_string().to_ascii_lowercase(); | ||
| if msg.contains("dns error") | ||
| || msg.contains("failed to lookup address") | ||
| || msg.contains("name or service not known") | ||
| if msg.contains("name or service not known") | ||
|
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. 🔵 Low — The narrowed marker set is tied to specific resolver phrasings — glibc This is fine on glibc/macOS/Windows with the default |
||
| || msg.contains("nodename nor servname") | ||
| || msg.contains("name resolution") | ||
| || msg.contains("no such host") | ||
| || msg.contains("non-existent domain") | ||
| || msg.contains("nxdomain") | ||
| { | ||
| return true; | ||
| } | ||
|
|
@@ -370,18 +364,36 @@ mod tests { | |
| // --- DNS-resolution detection tests --- | ||
|
|
||
| #[test] | ||
| fn dns_marker_detected_for_common_resolver_phrasings() { | ||
| fn permanent_dns_marker_detected_for_unknown_host_phrasings() { | ||
| use std::io::Error as IoError; | ||
| for msg in [ | ||
| "failed to lookup address information: Name or service not known", | ||
| "dns error: failed to lookup address information", | ||
| "nodename nor servname provided, or not known", | ||
| "No such host is known. (os error 11001)", | ||
| "dns error: NXDOMAIN", | ||
| "non-existent domain", | ||
| ] { | ||
| let err = IoError::other(msg); | ||
| assert!( | ||
| error_chain_indicates_dns_failure(&err), | ||
| "expected DNS marker in: {msg}" | ||
| error_chain_indicates_permanent_dns_failure(&err), | ||
| "expected permanent DNS marker in: {msg}" | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| #[test] | ||
| fn temporary_dns_failures_are_not_classified_as_permanent() { | ||
| use std::io::Error as IoError; | ||
| for msg in [ | ||
| "dns error: temporary failure in name resolution", | ||
| "failed to lookup address information: Try again", | ||
| "temporary failure in name resolution", | ||
| "server failure", | ||
| ] { | ||
| let err = IoError::other(msg); | ||
| assert!( | ||
| !error_chain_indicates_permanent_dns_failure(&err), | ||
| "temporary DNS failure must not be classified as permanent: {msg}" | ||
| ); | ||
| } | ||
| } | ||
|
|
@@ -393,9 +405,9 @@ mod tests { | |
| ErrorKind::ConnectionRefused, | ||
| "Connection refused (os error 111)", | ||
| ); | ||
| assert!(!error_chain_indicates_dns_failure(&refused)); | ||
| assert!(!error_chain_indicates_permanent_dns_failure(&refused)); | ||
| let reset = IoError::new(ErrorKind::ConnectionReset, "connection reset by peer"); | ||
| assert!(!error_chain_indicates_dns_failure(&reset)); | ||
| assert!(!error_chain_indicates_permanent_dns_failure(&reset)); | ||
| } | ||
|
|
||
| #[test] | ||
|
|
@@ -414,8 +426,8 @@ mod tests { | |
| Some(&self.0) | ||
| } | ||
| } | ||
| let inner = std::io::Error::other("dns error"); | ||
| assert!(error_chain_indicates_dns_failure(&Wrapper(inner))); | ||
| let inner = std::io::Error::other("dns error: NXDOMAIN"); | ||
| assert!(error_chain_indicates_permanent_dns_failure(&Wrapper(inner))); | ||
| } | ||
|
|
||
| // --- RequestType mapping tests --- | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.