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

retry: handle unexpected float delays #464

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

ssaunderss
Copy link

When retry delays were introduced in #226 it was implied that the retry delay would be greater than or equal to 1s. In situations where retry delays are either passed back as floats and/or less than 1s, it would be nice to use full precision since we're converting to milliseconds anyway.

This specifically fixes a problem in my case where an API is consistently handing back retries that are less than 1s, i.e. 0.314s.

@wojtekmach
Copy link
Owner

Thank you for the PR. Could you add a unit test?

@wojtekmach
Copy link
Owner

Could you double check if fractions are allowed by RFC 9110? If not we shouldn’t change this parsing, perhaps the issue is somewhere else.

@ssaunderss
Copy link
Author

I think it's a bit of a grey area - RFC 9110 dictates that it should be a non-negative integer, but APIs / rate limiters often pass back fractional values because they're just doing a raw date diff. Maybe a better way to handle this is to continue parsing as an integer, but handle the case where a fractional part is present by just adding an extra second?

It would be nice to handle this at the library level instead of having to add boilerplate for handling errors downstream.

@ssaunderss
Copy link
Author

I made some updates based on the approach above and added some test coverage for get_retry_after

@ssaunderss ssaunderss changed the title retry_delay_in_ms: support float delays retry_delay_in_ms: handle unexpected float delays Mar 6, 2025
@ssaunderss
Copy link
Author

@wojtekmach I've made the suggested changes and added tests. Would you have time to review this approach of handling fractional delays by rounding up? Let me know if I should close this PR or if you'd like additional changes.

@wojtekmach
Copy link
Owner

Apologies for delay. My bad. I think your solution is pragmatic however to be completely honest I'm not keen on accommodating broken servers that do not follow the spec. Looks like curl and Python requests ignore such invalid value for what it's worth.

We have some escape hatches in https://hexdocs.pm/req/Req.Steps.html#retry/1. Would any of these work for you? Using retry: &fun/2 you could see if the header is there and if parsing with Req.Response.get_retry_after/1 is unsuccessful, try parsing it as a float and return {delay, ms}.

If the current retry: fun interface would not work for you I'm much more keen on making it more useful to solve this and other edge cases. We have some open issues around this so if you'd be interested in going in that direction check them out first:

@wojtekmach wojtekmach changed the title retry_delay_in_ms: handle unexpected float delays retry: handle unexpected float delays Mar 19, 2025
@ssaunderss
Copy link
Author

Thanks for the response. I believe I should be able to handle it using a custom retry function. In the meantime, I'll also take a look into those related issues!

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