-
Notifications
You must be signed in to change notification settings - Fork 21
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
Parsing non-standard rate limit reset header value #27
Comments
Hi @zavan, thank you for raising this issue. I have a couple of potential solutions in mind:
Solution 1This is based on the assumption that a unix timestamp is a really long amount of time, so if we were to find a value above let's say 1,600,000,000 that would be a good indicator that the value is a timestamp, rather than the service asking us to wait 50 years 😅.
Solution 2This would me a much more elegant solution, IMO. Faraday.new do |conn|
conn.use RetryMiddleware,
retry_statuses: [429],
rate_limit_reset_header: 'X-RateLimit-Reset',
header_parser_block: ->(value) { Time.zone.at(value.to_i) - Time.zone.now }
# ...
end This way, when you have to deal with an API that doesn't follow the standard, you can pass a custom block to parse the value correctly. This solution has multiple advantages:
ConclusionAlthough the SendGrid API is ultimately responsible for the issue, we could implement "Solution 2" to allow users to deal with such use-cases in a maintainable way. |
Thanks @iMacTia! Yeah, I don't understand why SG went that way, maybe they implemented it before the RFC was accepted, which seems to be a recent thing. Solution 2 sounds great and it's what I had in mind, I didn't implement it because it requires adding the option, checking if was passed etc, so it was simpler to just extend and override the method in my case (if other APIs need the original middleware they can just use that, I'm not monkey-patching the middleware, just extending into a new class). I'll write a PR for solution 2 next week. |
Actually, found a minute and implemented in #28. Tests pass. Please let me know if you want any changes. |
The SendGrid API responses have a
X-RateLimit-Reset
header on 429 rate limit errors, but the value of that header is a integer unix timestamp (e.g.1685544756
), not arfc2822
date or a number of seconds like the RFC and this gem expects.I went around that by extending the middleware and overriding the method:
Is there a better way of doing this? If not, could we implement it?
The text was updated successfully, but these errors were encountered: