Skip to content
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

Merged
merged 15 commits into from
Dec 1, 2022
Merged

Conversation

peytonrunyan
Copy link
Contributor

@peytonrunyan peytonrunyan commented Nov 18, 2022

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 the send 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

  • This pull request references any related issue by including "closes <link to issue>"
    • If no issue exists and your change is not a small fix, please create an issue first.
  • This pull request includes tests or only affects documentation.
  • This pull request includes a label categorizing the change e.g. fix, feature, enhancement

@netlify
Copy link

netlify bot commented Nov 18, 2022

Deploy Preview for prefect-orion ready!

Name Link
🔨 Latest commit 3719fbe
🔍 Latest deploy log https://app.netlify.com/sites/prefect-orion/deploys/6388505d5377a1000866f70a
😎 Deploy Preview https://deploy-preview-7593--prefect-orion.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

src/prefect/client/base.py Outdated Show resolved Hide resolved
src/prefect/client/base.py Outdated Show resolved Hide resolved
@peytonrunyan peytonrunyan added v2 fix A fix for a bug in an existing feature labels Nov 21, 2022
@peytonrunyan peytonrunyan marked this pull request as ready for review November 21, 2022 13:30
@peytonrunyan peytonrunyan changed the title Add retry to send Add retry on specified httpx network errors Nov 21, 2022
Copy link
Contributor

@jakekaplan jakekaplan left a 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 Show resolved Hide resolved
src/prefect/client/base.py Show resolved Hide resolved
src/prefect/client/base.py Outdated Show resolved Hide resolved
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)
Copy link
Contributor

@jakekaplan jakekaplan Nov 21, 2022

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😅

Copy link
Contributor Author

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?

Copy link
Contributor

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

Copy link
Contributor

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?

src/prefect/client/base.py Outdated Show resolved Hide resolved
@peytonrunyan
Copy link
Contributor Author

@madkinsz thanks a ton for jumping in on this!

@zanieb zanieb merged commit ccf3040 into main Dec 1, 2022
@zanieb zanieb deleted the catch-network-failures branch December 1, 2022 15:19
github-actions bot pushed a commit that referenced this pull request Dec 1, 2022
github-actions bot pushed a commit to ddelange/prefect that referenced this pull request Dec 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
fix A fix for a bug in an existing feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants