-
Notifications
You must be signed in to change notification settings - Fork 166
Use new HTTP GET retry logic in SearchClient. #8732
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
5 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 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 |
|---|---|---|
|
|
@@ -47,23 +47,41 @@ Future<K> httpGetWithRetry<K>( | |
| Uri uri, { | ||
| required FutureOr<K> Function(http.Response response) responseFn, | ||
| int maxAttempts = 3, | ||
|
|
||
| /// The HTTP client to use. | ||
| /// | ||
| /// Note: when the client is specified, the inner loop will not create a new client object on retries. | ||
| http.Client? client, | ||
| Map<String, String>? headers, | ||
|
|
||
| /// Per-request time amount that will be applied on the overall HTTP request. | ||
| Duration? timeout, | ||
isoos marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| /// Additional retry conditions (on top of the default ones). | ||
| bool Function(Exception e)? retryIf, | ||
| }) async { | ||
| return await retry( | ||
| () async { | ||
| final client = http.Client(); | ||
| final closeClient = client == null; | ||
| final effectiveClient = client ?? http.Client(); | ||
| try { | ||
| final rs = await client.get(uri); | ||
| var f = effectiveClient.get(uri, headers: headers); | ||
| if (timeout != null) { | ||
| f = f.timeout(timeout); | ||
| } | ||
| final rs = await f; | ||
| if (rs.statusCode == 200) { | ||
| return responseFn(rs); | ||
| } | ||
| throw http.ClientException( | ||
| 'Unexpected status code for $uri: ${rs.statusCode}.'); | ||
| throw UnexpectedStatusException(rs.statusCode, uri); | ||
|
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. Perhaps document that the |
||
| } finally { | ||
| client.close(); | ||
| if (closeClient) { | ||
| effectiveClient.close(); | ||
| } | ||
| } | ||
| }, | ||
| maxAttempts: maxAttempts, | ||
| retryIf: _retryIf, | ||
| retryIf: (e) => _retryIf(e) || (retryIf != null && retryIf(e)), | ||
| ); | ||
| } | ||
|
|
||
|
|
@@ -77,5 +95,20 @@ bool _retryIf(Exception e) { | |
| if (e is http.ClientException) { | ||
| return true; // HTTP issues are worth retrying | ||
| } | ||
| if (e is UnexpectedStatusException) { | ||
| return _transientStatusCodes.contains(e.statusCode); | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| /// Thrown when status code is not 200. | ||
| class UnexpectedStatusException implements Exception { | ||
| final int statusCode; | ||
| final String message; | ||
|
|
||
| UnexpectedStatusException(this.statusCode, Uri uri) | ||
| : message = 'Unexpected status code for $uri: $statusCode.'; | ||
|
|
||
| @override | ||
| String toString() => 'UnexpectedStatusException: $message'; | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.