-
Notifications
You must be signed in to change notification settings - Fork 207
STF-322: Bounded transport-failure retry in WebServiceClient #693
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
Open
oschwald
wants to merge
5
commits into
main
Choose a base branch
from
greg/stf-322
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
185cde8
STF-322: Add bounded transport-failure retry to WebServiceClient
oschwald a13a4af
STF-322: Restore interrupt flag in InterruptedException rewrap path
oschwald 28c937b
STF-322: Add tests for transport-failure retry
oschwald a517511
STF-322: Document transport-failure retry in README and CHANGELOG
oschwald 9888efc
Add mise config
oschwald 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| # @generated - this file is auto-generated by `mise lock` https://mise.jdx.dev/dev-tools/mise-lock.html | ||
|
|
||
| [[tools.java]] | ||
| version = "26.0.0" | ||
| backend = "core:java" | ||
|
|
||
| [[tools.maven]] | ||
| version = "3.9.15" | ||
| backend = "aqua:apache/maven" | ||
|
|
||
| [tools.maven."platforms.linux-arm64"] | ||
| url = "https://archive.apache.org/dist/maven/maven-3/3.9.15/binaries/apache-maven-3.9.15-bin.tar.gz" | ||
|
|
||
| [tools.maven."platforms.linux-arm64-musl"] | ||
| url = "https://archive.apache.org/dist/maven/maven-3/3.9.15/binaries/apache-maven-3.9.15-bin.tar.gz" | ||
|
|
||
| [tools.maven."platforms.linux-x64"] | ||
| url = "https://archive.apache.org/dist/maven/maven-3/3.9.15/binaries/apache-maven-3.9.15-bin.tar.gz" | ||
|
|
||
| [tools.maven."platforms.linux-x64-musl"] | ||
| url = "https://archive.apache.org/dist/maven/maven-3/3.9.15/binaries/apache-maven-3.9.15-bin.tar.gz" | ||
|
|
||
| [tools.maven."platforms.macos-arm64"] | ||
| url = "https://archive.apache.org/dist/maven/maven-3/3.9.15/binaries/apache-maven-3.9.15-bin.tar.gz" | ||
|
|
||
| [tools.maven."platforms.macos-x64"] | ||
| url = "https://archive.apache.org/dist/maven/maven-3/3.9.15/binaries/apache-maven-3.9.15-bin.tar.gz" | ||
|
|
||
| [tools.maven."platforms.windows-x64"] | ||
| url = "https://archive.apache.org/dist/maven/maven-3/3.9.15/binaries/apache-maven-3.9.15-bin.tar.gz" |
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| [settings] | ||
| experimental = true | ||
| lockfile = true | ||
| disable_backends = [ | ||
| "asdf", | ||
| "vfox", | ||
| ] | ||
|
|
||
| [tools] | ||
| java = "latest" | ||
| maven = "latest" | ||
|
|
||
| [hooks] | ||
| enter = "mise install --quiet --locked" | ||
|
|
||
| [[watch_files]] | ||
| patterns = ["mise.toml", "mise.lock"] | ||
| run = "mise install --quiet --locked" |
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -20,20 +20,26 @@ | |
| import com.maxmind.geoip2.model.InsightsResponse; | ||
| import java.io.IOException; | ||
| import java.io.InputStream; | ||
| import java.io.InterruptedIOException; | ||
| import java.net.ConnectException; | ||
| import java.net.InetAddress; | ||
| import java.net.InetSocketAddress; | ||
| import java.net.ProxySelector; | ||
| import java.net.URI; | ||
| import java.net.URISyntaxException; | ||
| import java.net.UnknownHostException; | ||
| import java.net.http.HttpClient; | ||
| import java.net.http.HttpRequest; | ||
| import java.net.http.HttpResponse; | ||
| import java.net.http.HttpTimeoutException; | ||
| import java.nio.charset.StandardCharsets; | ||
| import java.time.Duration; | ||
| import java.util.Base64; | ||
| import java.util.HashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import javax.net.ssl.SSLHandshakeException; | ||
| import javax.net.ssl.SSLPeerUnverifiedException; | ||
|
|
||
| /** | ||
| * <p> | ||
|
|
@@ -112,6 +118,7 @@ public class WebServiceClient implements WebServiceProvider { | |
| private final boolean useHttps; | ||
| private final int port; | ||
| private final Duration requestTimeout; | ||
| private final int maxRetries; | ||
| private final String userAgent = "GeoIP2/" | ||
| + getClass().getPackage().getImplementationVersion() | ||
| + " (Java/" + System.getProperty("java.version") + ")"; | ||
|
|
@@ -125,6 +132,7 @@ private WebServiceClient(Builder builder) { | |
| this.port = builder.port; | ||
| this.useHttps = builder.useHttps; | ||
| this.locales = builder.locales; | ||
| this.maxRetries = builder.maxRetries; | ||
|
|
||
| // HttpClient supports basic auth, but it will only send it after the | ||
| // server responds with an unauthorized. As such, we just make the | ||
|
|
@@ -182,6 +190,7 @@ public static final class Builder { | |
| List<String> locales = List.of("en"); | ||
| private ProxySelector proxy = null; | ||
| private HttpClient httpClient = null; | ||
| private int maxRetries = 1; | ||
|
|
||
| /** | ||
| * @param accountId Your MaxMind account ID. | ||
|
|
@@ -197,6 +206,7 @@ public Builder(int accountId, String licenseKey) { | |
| * @param val Timeout duration to establish a connection to the | ||
| * web service. The default is 3 seconds. | ||
| * @return Builder object | ||
| * @apiNote See {@link #maxRetries(int)} for how this timeout interacts with retries. | ||
| */ | ||
| public Builder connectTimeout(Duration val) { | ||
| this.connectTimeout = val; | ||
|
|
@@ -251,6 +261,7 @@ public Builder locales(List<String> val) { | |
| /** | ||
| * @param val Request timeout duration. The default is 20 seconds. | ||
| * @return Builder object | ||
| * @apiNote See {@link #maxRetries(int)} for how this timeout interacts with retries. | ||
| */ | ||
| public Builder requestTimeout(Duration val) { | ||
| this.requestTimeout = val; | ||
|
|
@@ -271,13 +282,38 @@ public Builder proxy(ProxySelector val) { | |
| * @param val the custom HttpClient to use for requests. When providing a | ||
| * custom HttpClient, you cannot also set connectTimeout or proxy | ||
| * parameters as these should be configured on the provided client. | ||
| * <p> | ||
| * The SDK applies its own transport-failure retry on top of any | ||
| * supplied client; customers can disable it via | ||
|
Contributor
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. "customers" seems somewhat out of place here. |
||
| * {@link #maxRetries(int)} with {@code .maxRetries(0)}. | ||
| * @return Builder object | ||
| */ | ||
| public Builder httpClient(HttpClient val) { | ||
| this.httpClient = val; | ||
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * @param val Maximum number of retries on transport-level failures | ||
| * (connection reset, broken pipe, EOF, ...). | ||
| * Applies uniformly to all endpoints. Defaults to 1. | ||
| * Set to 0 to disable. | ||
| * @return Builder. | ||
| * @throws IllegalArgumentException if {@code val} is negative. | ||
| * @apiNote Retries fire only on transient transport failures. | ||
| * Timeouts and other non-transient errors are not retried — see | ||
| * the README for the complete list. When all attempts fail, | ||
| * the prior {@code IOException}s are attached via | ||
| * {@link Throwable#getSuppressed()} for debugging. | ||
| */ | ||
| public Builder maxRetries(int val) { | ||
| if (val < 0) { | ||
| throw new IllegalArgumentException("maxRetries must not be negative"); | ||
| } | ||
| maxRetries = val; | ||
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * @return an instance of {@code WebServiceClient} created from the | ||
| * fields set on this builder. | ||
|
|
@@ -371,18 +407,80 @@ private <T> T responseFor(String path, InetAddress ipAddress, Class<T> cls) | |
| .GET() | ||
| .build(); | ||
| try { | ||
| var response = this.httpClient | ||
| .send(request, HttpResponse.BodyHandlers.ofInputStream()); | ||
| var response = sendWithRetry(request); | ||
| try { | ||
| return handleResponse(response, cls); | ||
| } finally { | ||
| response.body().close(); | ||
| } | ||
| } catch (InterruptedException e) { | ||
| Thread.currentThread().interrupt(); | ||
| throw new GeoIp2Exception("Interrupted sending request", e); | ||
| } | ||
| } | ||
|
|
||
| private HttpResponse<InputStream> sendWithRetry(HttpRequest request) | ||
| throws IOException, InterruptedException { | ||
| int attempts = 0; | ||
| IOException prior = null; | ||
| while (true) { | ||
| try { | ||
| return httpClient.send(request, HttpResponse.BodyHandlers.ofInputStream()); | ||
| } catch (IOException e) { | ||
| // Attach the immediate predecessor so the suppressed chain | ||
| // carries the full retry history (each link is the previous | ||
| // attempt's failure; walk via Throwable#getSuppressed). | ||
| if (prior != null) { | ||
| e.addSuppressed(prior); | ||
| } | ||
| if (!isRetriableTransportFailure(e) || attempts >= maxRetries) { | ||
| throw e; | ||
| } | ||
| prior = e; | ||
| attempts++; | ||
| } | ||
| } | ||
| } | ||
|
oschwald marked this conversation as resolved.
|
||
|
|
||
| private static boolean isRetriableTransportFailure(IOException e) { | ||
| if (Thread.currentThread().isInterrupted()) { | ||
| return false; | ||
| } | ||
| // Both connect-phase and request-phase timeouts are customer-set | ||
| // budgets that retrying would silently extend. | ||
| // HttpConnectTimeoutException extends HttpTimeoutException, so this | ||
| // single check covers both. | ||
| if (e instanceof HttpTimeoutException) { | ||
| return false; | ||
| } | ||
| // The thread was interrupted during I/O; honor the cancellation. | ||
| if (e instanceof InterruptedIOException) { | ||
| return false; | ||
| } | ||
| // The four exclusions below are *occasionally* transient (DNS hiccup, | ||
| // TCP RST race during cert rotation, brief LB outage), but treating | ||
| // them as deterministic is a deliberate product decision: retrying | ||
| // would mask config bugs behind 2x latency, and the customer-visible | ||
| // cost of one extra failed call on a true transient is small. | ||
| if (e instanceof UnknownHostException) { | ||
| return false; | ||
| } | ||
| if (e instanceof ConnectException) { | ||
| return false; | ||
| } | ||
| if (e instanceof SSLHandshakeException) { | ||
| return false; | ||
| } | ||
| if (e instanceof SSLPeerUnverifiedException) { | ||
| return false; | ||
| } | ||
| // Everything else from httpClient.send() is a transport failure | ||
| // (connection reset, broken pipe, EOF, closed channel, ...). | ||
| // HTTP 4xx and 5xx responses do not reach this predicate -- they come | ||
| // back as HttpResponse objects rather than IOExceptions. | ||
| return true; | ||
| } | ||
|
|
||
| private <T> T handleResponse(HttpResponse<InputStream> response, Class<T> cls) | ||
| throws GeoIp2Exception, IOException { | ||
| var status = response.statusCode(); | ||
|
|
||
Oops, something went wrong.
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.
Maybe 5.1.0?