ExHammer / hammer

An Elixir rate-limiter with pluggable backends
https://hexdocs.pm/hammer/
MIT License
749 stars 42 forks source link

Using Hammer for ratelimiting external API calls #39

Closed jchristgit closed 10 months ago

jchristgit commented 5 years ago

This is more of a question than an issue with the library - I'm currently attempting to use Hammer to ratelimit access to an external API to avoid hitting ratelimits on the API itself. Other libraries I found require the ratelimits to be declared upfront somehow and the possibility to create a rate limit checker seemed appropriate (since I get the rate limits returned in HTTP headers) so I went with Hammer.

However, since the API starts counting ratelimits after my first request, and Hammer's rate limits move in "windows" due to stamp_key/2...:

https://github.com/ExHammer/hammer/blob/b2cf18dd66bbd2b1f2a6d8e5350b4585b4d51bce/lib/hammer/utils.ex#L17-L24

... this doesn't quite work out, namely because Hammer's buckets are almost guaranteed to expire before an API bucket expires. What would you recommend to do in this case instead? Perhaps it would be possible to allow for using a custom "initial start" for stamp_key?

Thanks for creating this library, by the way. I really like the configurability of backends and being able to extend those if needed.

JuneKelly commented 5 years ago

(Sorry for the late reply, I haven't been keeping up on my Github notifications)

Wow, that's a tricky one. I guess if you wanted to try adding a custom start for stamp_key, and it works, I'd be totally willing to merge the change in.

Or, you could try setting a window size and expiry wider than the remote API has, but then I'm not sure that it would line up correctly, so you'd maybe still trip the remote rate limiter.

JuneKelly commented 5 years ago

Follow up question, what kind of information do you get back from the remote api? do you get a sort of "remaining calls" count and a timestamp of when that will be refreshed? I have an idea that might work depending on what kind of data we have available.

jchristgit commented 5 years ago

Thanks for the reply. Adding a custom start for stamp_key definitely sounds like a possibility but I'm not sure if we might be able to do something better based on the API data.

The API returns the total amount of calls you've done, the maximum of calls, and the timeframe for which they apply. The API doesn't return the starting timestamp, but if you track your current calls somewhere, I imagine you could stamp it yourself. However, in case you hit the limit the API does return the amount of seconds until it expires again. What did you have in mind?

epinault commented 1 year ago

@JuneKelly would you mind sharing your idea here? otherwIse I ll close this as I am not sure what to do with it

epinault commented 10 months ago

closing as I don t have details and don t seem that we wil make progress on this