hammer icon indicating copy to clipboard operation
hammer copied to clipboard

Using Hammer for ratelimiting external API calls

Open jchristgit opened this issue 5 years ago • 3 comments

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.

jchristgit avatar Feb 24 '19 23:02 jchristgit

(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 avatar Apr 11 '19 10:04 JuneKelly

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.

JuneKelly avatar Apr 11 '19 10:04 JuneKelly

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?

jchristgit avatar Apr 11 '19 18:04 jchristgit

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

epinault avatar Aug 31 '23 22:08 epinault

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

epinault avatar Jan 09 '24 16:01 epinault