-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Add 'on_error' argument to 'retry.retry_target' and 'Retry'. #3891
Add 'on_error' argument to 'retry.retry_target' and 'Retry'. #3891
Conversation
Permit application code to reset / fix-up state before retrying. See: #3889
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.
LGTM though I think you should let @jonparrott have a look before merging
core/google/api/core/retry.py
Outdated
@@ -150,6 +150,9 @@ def retry_target(target, predicate, sleep_generator, deadline): | |||
sleep_generator (Iterator[float]): An infinite iterator that determines | |||
how long to sleep between retries. | |||
deadline (float): How long to keep retrying the target. | |||
on_error(Callable): A function to call while processing a retryable |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
core/google/api/core/retry.py
Outdated
"""Wrap a callable with retry behavior. | ||
|
||
Args: | ||
func (Callable): The callable to add retry behavior to. | ||
on_error(Callable): A function to call while processing a |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
|
||
assert result == 42 | ||
assert call_count['target'] == 3 | ||
assert call_count['on_error'] == 2 |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
assert call_count['target'] == 3 | ||
assert call_count['on_error'] == 2 | ||
|
||
sleep.assert_has_calls([mock.call(0), mock.call(1)]) |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
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.
looks mostly good, just a few questions.
core/google/api/core/retry.py
Outdated
@@ -177,6 +180,8 @@ def retry_target(target, predicate, sleep_generator, deadline): | |||
if not predicate(exc): | |||
raise | |||
last_exc = exc | |||
if on_error is not None: | |||
on_error() |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
core/google/api/core/retry.py
Outdated
@@ -150,6 +150,9 @@ def retry_target(target, predicate, sleep_generator, deadline): | |||
sleep_generator (Iterator[float]): An infinite iterator that determines | |||
how long to sleep between retries. | |||
deadline (float): How long to keep retrying the target. | |||
on_error(Callable): A function to call while processing a retryable |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
@@ -226,11 +231,14 @@ def __init__( | |||
self._maximum = maximum | |||
self._deadline = deadline | |||
|
|||
def __call__(self, func): | |||
def __call__(self, func, on_error=None): |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
Permit application code to reset / fix-up state before retrying.
See: #3889