You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The doc (https://github.com/rack/rack-attack#throttling) seems to indicate that the throttling period is a rolling period over which one cannot do more than x requests/period, but I noticed it was more a fixed reset period for the cache.
Did I miss something? Do you know if this behaviour was intentional?
In any case, a good first step could be to clarify the doc (happy to make a suggestion).
Steps to reproduce
setup a throttling protection: for instance, a limit of 5 requests per hour for a given ip
Rack::Attack.throttle("requests by ip",limit: 5,period: 1.hour)do |request|
request.ipend
the following spec passes: 10 requests over 2 minutes, then the 11th fails
RSpec.describe'Throttling requests',type: :requestdoaround(:each)do |example|
cache_store=ActionController::Base.cache_storeActionController::Base.cache_store=ActiveSupport::Cache::MemoryStore.newActionController::Base.perform_caching=trueRack::Attack.enabled=trueRack::Attack.reset!example.runRack::Attack.enabled=falseActionController::Base.perform_caching=falseActionController::Base.cache_store=cache_storeenddescribe'requests by ip'doit'accepts 10 requests from the same ip within a 2 minutes period, then rejects the 11th'dotravel_toTime.zone.local(2022,12,12,16,59,0o0)5.timesdoget'/'expect(response).tohave_http_status(:found)endtravel_toTime.zone.local(2022,12,12,17,0o0,0o0)5.timesdoget'/'expect(response).tohave_http_status(:found)endget'/'expect(response).tohave_http_status(:too_many_requests)endendend
So I believe you're right and this is essentially a ticker, not a rolling window.
E.g. if I choose period = 60. The intervals are fixed windows of 60 seconds, regardless of when I make my first request.
This isn't necessarily an issue for our use cases (we use small windows to prevent abuse) but agree that the documentation could be more explicit.
Activity
pcothenet commentedon Apr 4, 2023
This also took me out for a spin, as I couldn't understand why our request specs were being flaky.
According the source correctly, the logic to set the cache expiry is:
So I believe you're right and this is essentially a ticker, not a rolling window.
E.g. if I choose
period= 60. The intervals are fixed windows of 60 seconds, regardless of when I make my first request.This isn't necessarily an issue for our use cases (we use small windows to prevent abuse) but agree that the documentation could be more explicit.