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 retries functionality #178

Merged
merged 25 commits into from
Apr 29, 2024
Merged

Add retries functionality #178

merged 25 commits into from
Apr 29, 2024

Conversation

MatthewFlamm
Copy link
Owner

@MatthewFlamm MatthewFlamm commented Apr 25, 2024

NWS servers are flaky. This PR adds in ability to use tenacity to do retries. This can be done by downstream applications already, but some downstream applications, such as homeassistant prefer to have this logic done inside the upstream libraries.

This implements a simple retry strategy to try every X unit of time until Y unit of time elapses.

@lymanepp
Copy link
Collaborator

Hi @MatthewFlamm, you may want to consider only retrying for server errors (5xx).

@MatthewFlamm
Copy link
Owner Author

Thanks I should have made this draft as there are a few things I need to change. I didn't yet think of your point, which is a good one.

I'm also unsure why the tests fail on python 3.8 and 3.9 right now.

@MatthewFlamm MatthewFlamm marked this pull request as draft April 25, 2024 15:44
@lymanepp
Copy link
Collaborator

I'm also unsure why the tests fail on python 3.8 and 3.9 right now.

This might be a good time to drop support for those old versions.

@MatthewFlamm
Copy link
Owner Author

MatthewFlamm commented Apr 25, 2024

Hi @MatthewFlamm, you may want to consider only retrying for server errors (5xx).

I need to look into how to best detect catch 500 errors on the client-side in tenacity. The server side errors are nicely grouped in aiohttp.web, but these aren't appropriate for the client-side. Might need to provide a custom retry predicate for tenacity.retry.

At least all the python versions are failing now that I have the right test. I expect the 3.8 and 3.9 tests to continue to fail when this is fixed. I have an outside hope that we can figure out this issue, as it could be a testing problem or a testing environment problem. Just removing support is also an option as these are very old versions of python and this package is widely used for one downstream app that is always on the bleeding edge.

pynws/simple_nws.py Outdated Show resolved Hide resolved
@MatthewFlamm MatthewFlamm marked this pull request as ready for review April 26, 2024 10:34
@MatthewFlamm
Copy link
Owner Author

This is ready for full review. It still fails in python 3.8 and python 3.9. I will leave for now in case we can figure that out. Otherwise we can drop support for those versions. The retry behavior should be generally useful given NWS flakiness.

@MatthewFlamm
Copy link
Owner Author

I was able to get a MRE for this and submitted an issue jd/tenacity#456

@MatthewFlamm
Copy link
Owner Author

I suspect that there is some inspection logic in tenacity that prevents detecting AsyncMock as async only in previous Python versions. The solution is to wrap the mock object in a regular async function, but have the mock object defined outside the scope of the function. It looks ugly, but it works. If you have time to review @lymanepp, I would appreciate it

@lymanepp
Copy link
Collaborator

I question adding the retry logic to the existing classes as they're not used internally. Can they be moved to a separate class (or function)?

@lymanepp
Copy link
Collaborator

It looks ugly, but it works. If you have time to review @lymanepp, I would appreciate it

It looks fine to me--I added some suggestions inline.

@MatthewFlamm
Copy link
Owner Author

Your comment makes sense to me, nws.call_with_retry(NWS.update_observation) is awkward. I made it a function. Now it is call_with_retry(NWS.update_observation).

@lymanepp
Copy link
Collaborator

I forgot to remove .pylintrc, can you include that in this PR?

@lymanepp
Copy link
Collaborator

LGTM

@MatthewFlamm MatthewFlamm merged commit e259485 into master Apr 29, 2024
8 checks passed
@MatthewFlamm MatthewFlamm deleted the retries branch April 29, 2024 18:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants