-
Notifications
You must be signed in to change notification settings - Fork 322
feat: Retry behavior #1113
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
feat: Retry behavior #1113
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
f6ee870
chore: Refrefsh system test creds.
clundin25 676d9e2
feat: Retry behavior
clundin25 92f6c21
Update license for tests/test_exceptions.py
clundin25 56bab8c
Merge branch 'main' into property-retry
sai-sunder-s b282743
Merge branch 'main' into property-retry
clundin25 b2ebcbc
s/should_retry/can_retry/
clundin25 27ff428
Merge branch 'main' into property-retry
clundin25 7863cc6
PR feedback. Add temporarily_unavailable as a retryable status_code.
clundin25 6c4a837
Merge remote-tracking branch 'carl/refresh' into property-retry
clundin25 f04a1bb
PR Feedback
clundin25 78b1bf9
Merge remote-tracking branch 'origin/main' into property-retry
clundin25 00f17ed
chore: Refresh system test creds.
clundin25 db8f1ea
PR feedback.
clundin25 60904a8
PR feedback.
clundin25 2cb8d68
chore: Refresh system test creds.
clundin25 984580f
PR feedback.
clundin25 bcbe828
Merge remote-tracking branch 'origin/main' into property-retry
clundin25 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
# Copyright 2022 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import random | ||
import time | ||
|
||
import six | ||
|
||
# The default amount of retry attempts | ||
_DEFAULT_RETRY_TOTAL_ATTEMPTS = 3 | ||
|
||
# The default initial backoff period (1.0 second). | ||
_DEFAULT_INITIAL_INTERVAL_SECONDS = 1.0 | ||
|
||
# The default randomization factor (0.1 which results in a random period ranging | ||
# between 10% below and 10% above the retry interval). | ||
_DEFAULT_RANDOMIZATION_FACTOR = 0.1 | ||
|
||
# The default multiplier value (2 which is 100% increase per back off). | ||
_DEFAULT_MULTIPLIER = 2.0 | ||
|
||
"""Exponential Backoff Utility | ||
|
||
This is a private module that implements the exponential back off algorithm. | ||
It can be used as a utility for code that needs to retry on failure, for example | ||
an HTTP request. | ||
""" | ||
|
||
|
||
class ExponentialBackoff(six.Iterator): | ||
"""An exponential backoff iterator. This can be used in a for loop to | ||
perform requests with exponential backoff. | ||
|
||
Args: | ||
total_attempts Optional[int]: | ||
The maximum amount of retries that should happen. | ||
The default value is 3 attempts. | ||
initial_wait_seconds Optional[int]: | ||
The amount of time to sleep in the first backoff. This parameter | ||
should be in seconds. | ||
The default value is 1 second. | ||
randomization_factor Optional[float]: | ||
The amount of jitter that should be in each backoff. For example, | ||
a value of 0.1 will introduce a jitter range of 10% to the | ||
current backoff period. | ||
The default value is 0.1. | ||
multiplier Optional[float]: | ||
The backoff multipler. This adjusts how much each backoff will | ||
increase. For example a value of 2.0 leads to a 200% backoff | ||
on each attempt. If the initial_wait is 1.0 it would look like | ||
this sequence [1.0, 2.0, 4.0, 8.0]. | ||
The default value is 2.0. | ||
""" | ||
|
||
def __init__( | ||
self, | ||
total_attempts=_DEFAULT_RETRY_TOTAL_ATTEMPTS, | ||
initial_wait_seconds=_DEFAULT_INITIAL_INTERVAL_SECONDS, | ||
randomization_factor=_DEFAULT_RANDOMIZATION_FACTOR, | ||
multiplier=_DEFAULT_MULTIPLIER, | ||
): | ||
self._total_attempts = total_attempts | ||
self._initial_wait_seconds = initial_wait_seconds | ||
|
||
self._current_wait_in_seconds = self._initial_wait_seconds | ||
|
||
self._randomization_factor = randomization_factor | ||
self._multiplier = multiplier | ||
self._backoff_count = 0 | ||
|
||
def __iter__(self): | ||
self._backoff_count = 0 | ||
self._current_wait_in_seconds = self._initial_wait_seconds | ||
return self | ||
|
||
def __next__(self): | ||
if self._backoff_count >= self._total_attempts: | ||
raise StopIteration | ||
self._backoff_count += 1 | ||
|
||
jitter_variance = self._current_wait_in_seconds * self._randomization_factor | ||
jitter = random.uniform( | ||
self._current_wait_in_seconds - jitter_variance, | ||
self._current_wait_in_seconds + jitter_variance, | ||
) | ||
|
||
time.sleep(jitter) | ||
|
||
self._current_wait_in_seconds *= self._multiplier | ||
return self._backoff_count | ||
|
||
@property | ||
def total_attempts(self): | ||
"""The total amount of backoff attempts that will be made.""" | ||
return self._total_attempts | ||
|
||
@property | ||
def backoff_count(self): | ||
"""The current amount of backoff attempts that have been made.""" | ||
return self._backoff_count |
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
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.
Uh oh!
There was an error while loading. Please reload this page.