-
Notifications
You must be signed in to change notification settings - Fork 323
fix: updates timeout/retry code to respect hanging server #2408
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
0ca3d87
f58d712
f62c42b
291cfe9
6f0bad0
2544afb
00eb8b5
e5973a0
90ef70f
8ee106c
60ae827
f263a8d
7e9b9f9
b7ea81a
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 |
|---|---|---|
|
|
@@ -12,12 +12,15 @@ | |
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| import logging | ||
|
|
||
| from google.api_core import exceptions | ||
| from google.api_core import retry | ||
| import google.api_core.future.polling | ||
| from google.auth import exceptions as auth_exceptions # type: ignore | ||
| import requests.exceptions | ||
|
|
||
| _LOGGER = logging.getLogger(__name__) | ||
|
|
||
| _RETRYABLE_REASONS = frozenset( | ||
| ["rateLimitExceeded", "backendError", "internalError", "badGateway"] | ||
|
|
@@ -61,14 +64,17 @@ | |
| def _should_retry(exc): | ||
| """Predicate for determining when to retry. | ||
|
|
||
| We retry if and only if the 'reason' is 'backendError' | ||
| or 'rateLimitExceeded'. | ||
| We retry if and only if the 'reason' is in _RETRYABLE_REASONS or is | ||
| in _UNSTRUCTURED_RETRYABLE_TYPES. | ||
| """ | ||
| if not hasattr(exc, "errors") or len(exc.errors) == 0: | ||
| # Check for unstructured error returns, e.g. from GFE | ||
| try: | ||
| reason = exc.errors[0]["reason"] | ||
| except (AttributeError, IndexError, TypeError, KeyError): | ||
| # Fallback for when errors attribute is missing, empty, or not a dict | ||
| # or doesn't contain "reason" (e.g. gRPC exceptions). | ||
| _LOGGER.debug("Inspecting unstructured error for retry: %r", exc) | ||
|
Collaborator
Author
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. NOTE to reviewer: Why did we use In the Python Because this code is not in a super-tight loop, the difference is negligible, but none-the-less it is good practice. |
||
| return isinstance(exc, _UNSTRUCTURED_RETRYABLE_TYPES) | ||
|
|
||
| reason = exc.errors[0]["reason"] | ||
| return reason in _RETRYABLE_REASONS | ||
|
|
||
|
|
||
|
|
||
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.
NOTE to reviewer:
Why a monotonic clock? A monotonic clock is guaranteed to move forward or stay still, but never go backward, making it ideal for measuring elapsed time and durations, unlike the system's wall clock (time.time()), which can be adjusted manually or by network time protocols (NTP) (i.e. fall back in the fall). Not likely to be a huge issue here, but good practice for this use case.