-
Notifications
You must be signed in to change notification settings - Fork 42
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
Inconsistent Bucket Duration in Hammer for Periodic Rate Limits #103
Comments
I ll see if I can look into it but PR welcome too if you have a solution |
👋
UPDATE: I was updating the guides and noticed that Hammer says it uses token bucket rate limiting, but in reality it uses a fixed winder counter. So maybe it makes sense to actually implement token bucket rate limiting? Otherwise it's probably a bit confusing. For now I updated the tutorial in #104 to describe the current algorithm as a fixed winder counter. |
yea as I was looking back this is because it is a fixed window counter. I think this is fine to have it be the default. Sliding Window https://www.rdiachenko.com/posts/arch/rate-limiting/sliding-window-algorithm/ as well as Leaky Bucket support? |
Yeah, sliding window is easy to implement, here's a clean example, but it's slow. For users with expensive resources, on which they rate limit to like one per hour (#99) it might be a reasonable trade off. Maybe we can have a periodic benchmark of all official backends / algorithms running in CI to make it easier for people to choose.
defmodule MyApp.RateLimit do
use Hammer, backend: :ets, algorithm: :fixed_window # default
end
defmodule MyApp.RateLimit do
use Hammer, backend: :ets, algorithm: :sliding_window
end
# etc.
defmodule MyApp.RateLimit do
use Hammer, backend: :fixed_window_ets
end
defmodule MyApp.RateLimit do
use Hammer, backend: :sliding_window_ets
end
# etc. UPDATE: Ah, no. The above wouldn't work since the current public API relies on |
I think we need to have a backend (ETS, redis) separted to how they use the window. I think we need to extract that later into something configurable but defaulting on the current way. So your first approach with algorithm is the wya I would approach it |
Describe the bug
In the current implementation of Hammer, the bucket duration can be less than the specified period depending on the precise moment a request is received.
For example, with a 10-second period, requests made at 5 seconds past the epoch will result in a bucket duration of only 5 seconds instead of 10.
** Provide the following details
Expected behavior
When configuring a rate limit with a specified period (e.g., 10 seconds), each bucket should consistently cover the full time period. For example, a 10-second period should always represent a 10-second window, regardless of when the request is made within that period.
Actual behavior
The bucket duration can be less than the specified period. For example, with a 10-second period, requests made at 5 seconds past the epoch result in a bucket duration of only 5 seconds instead of 10. This leads to premature allowance of requests within the same time window.
The text was updated successfully, but these errors were encountered: