Clarify algorithm

This commit is contained in:
Aaron Suggs 2013-01-17 12:06:10 -05:00
parent 0ca7b8cfac
commit 27a13f6971
2 changed files with 23 additions and 8 deletions

View file

@ -34,10 +34,27 @@ Note that `Rack::Attack.cache` is only used for throttling; not blacklisting & w
The Rack::Attack middleware compares each request against *whitelists*, *blacklists*, *throttles*, and *tracks* that you define. There are none by default.
* If the request matches any **whitelist**, it is allowed. Blacklists and throttles are not checked.
* If the request matches any **blacklist**, it is blocked. Throttles are not checked.
* If the request matches any **throttle**, a counter is incremented in the Rack::Attack.cache. If the throttle limit is exceeded, the request is blocked and further throttles are not checked.
* If the request was not whitelisted, blacklisted, or throttled; all **tracks** are checked.
* If the request matches any **whitelist**, it is allowed.
* Otherwise, if the request matches any **blacklist**, it is blocked.
* Otherwise, if the request matches any **throttle**, a counter is incremented in the Rack::Attack.cache. If the throttle limit is exceeded, the request is blocked.
* Otherwise, all **tracks** are checked, and the request is allowed.
The algorithm is actually more concise in code: See [Rack::Attack.call](https://github.com/kickstarter/rack-attack/blob/master/lib/rack/attack.rb):
def call(env)
req = Rack::Request.new(env)
if whitelisted?(req)
@app.call(env)
elsif blacklisted?(req)
blacklisted_response[env]
elsif throttled?(req)
throttled_response[env]
else
tracked?(req)
@app.call(env)
end
end
## About Tracks

View file

@ -50,10 +50,8 @@ module Rack::Attack
req = Rack::Request.new(env)
if whitelisted?(req)
return @app.call(env)
end
if blacklisted?(req)
@app.call(env)
elsif blacklisted?(req)
blacklisted_response[env]
elsif throttled?(req)
throttled_response[env]