-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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 retry on specified httpx network errors #7593
Conversation
✅ Deploy Preview for prefect-orion ready!
To edit notification comments on pull requests, go to your Netlify site settings. |
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.
nice work! left a couple questions
src/prefect/client/base.py
Outdated
in {status.HTTP_429_TOO_MANY_REQUESTS, status.HTTP_503_SERVICE_UNAVAILABLE} | ||
and retry_count < self.RETRY_MAX | ||
not response | ||
or (response.status_code in retry_codes) |
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.
I am not certain but could this be:
while (not response or retry):
It looks like retry is being set on the same statements that the while loop is conditional on? I might be missing a case where the reverse doesn't make sense. Or maybe the retry
variable is unneeded? I can't quite put my finger on it, but there could be a way to simplify this or make it a little more easily readable😅
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.
Is the new arrangement more readable?
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.
I'm so sorry, I think my original comment was a little vague and unhelpful! I think a combination of the conditionals underneath the try/except/else/finally
structure makes it harder to follow the code paths. Maybe something like this could work? (Just to qualify a bit I don't know if this works and the solution as written is great, just seeing if it's possible to consolidate a bit)
try_count = 0
response = None
while True:
try_count += 1
retry_seconds = None
retry = try_count <= self.RETRY_MAX
try:
response = await request()
except retry_exceptions:
if not retry:
raise
retry_seconds = 2**try_count
else:
if response.status_code in retry_codes:
retry_seconds = float(response.headers.get("Retry-After", 2**try_count))
if retry and retry_seconds is not None:
await anyio.sleep(retry_seconds)
else:
break
return response
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.
@jakekaplan want to give it a quick look again?
Also adds debug level logs for retries, including tracebacks
3da9d7a
to
3719fbe
Compare
@madkinsz thanks a ton for jumping in on this! |
Co-authored-by: Michael Adkins <[email protected]>
Co-authored-by: Michael Adkins <[email protected]>
Begins to address #7512 and #7442
This pull request moves adds retry logic for exceptions encountered when sending requests with
prefect/client/base.py::PrefectHttpxClient
. It also moves the logic for the retry from thesend
method to_send_with_retry
, which takes a partial of the send function with the associated args and kwargs, a list of exceptions to retry under, and a list of status codes to retry under.Example
Checklist
<link to issue>
"fix
,feature
,enhancement