Class: Rack::Throttle::Limiter
- Inherits:
-
Object
- Object
- Rack::Throttle::Limiter
- Defined in:
- lib/rack/throttle/limiters/limiter.rb
Overview
This is the base class for rate limiter implementations.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#app ⇒ Object
readonly
Returns the value of attribute app.
-
#matchers ⇒ Object
readonly
Returns the value of attribute matchers.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
-
#allowed?(request) ⇒ Boolean
Returns
falseif the rate limit has been exceeded for the givenrequest, ortrueotherwise. -
#blacklisted?(request) ⇒ Boolean
abstract
Returns
trueif the originator of the givenrequestis blacklisted (not honoring rate limits, and thus permanently forbidden access without the need to maintain further rate limit counters). - #call(env) ⇒ Array(Integer, Hash, #each)
-
#initialize(app, options = {}) ⇒ Limiter
constructor
A new instance of Limiter.
-
#restricted_url?(path) ⇒ Boolean
Returns
trueif no :url_rule regex or if the request path matches the :url regex,falseotherwise. -
#whitelisted?(request) ⇒ Boolean
abstract
Returns
trueif the originator of the givenrequestis whitelisted (not subject to further rate limits).
Constructor Details
#initialize(app, options = {}) ⇒ Limiter
Returns a new instance of Limiter.
23 24 25 26 27 28 29 30 |
# File 'lib/rack/throttle/limiters/limiter.rb', line 23 def initialize(app, = {}) rules = .delete(:rules) || {} @app, @options, @matchers = app, , [] @matchers += Array(rules[:url]).map { |rule| UrlMatcher.new(rule) } if rules[:url] @matchers += Array(rules[:user_agent]).map { |rule| UserAgentMatcher.new(rule) } if rules[:user_agent] @matchers += Array(rules[:method]).map { |rule| MethodMatcher.new(rule) } if rules[:method] @matchers += Array(rules[:basic_auth]).map { |rule| BasicAuthMatcher.new(rule) } if rules[:basic_auth] end |
Instance Attribute Details
#app ⇒ Object (readonly)
Returns the value of attribute app.
13 14 15 |
# File 'lib/rack/throttle/limiters/limiter.rb', line 13 def app @app end |
#matchers ⇒ Object (readonly)
Returns the value of attribute matchers.
13 14 15 |
# File 'lib/rack/throttle/limiters/limiter.rb', line 13 def matchers @matchers end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
13 14 15 |
# File 'lib/rack/throttle/limiters/limiter.rb', line 13 def @options end |
Instance Method Details
#allowed?(request) ⇒ Boolean
Returns false if the rate limit has been exceeded for the given
request, or true otherwise.
Override this method in subclasses that implement custom rate limiter strategies.
68 69 70 71 72 73 74 |
# File 'lib/rack/throttle/limiters/limiter.rb', line 68 def allowed?(request) case when whitelisted?(request) then true when blacklisted?(request) then false else true # override in subclasses end end |
#blacklisted?(request) ⇒ Boolean
Returns true if the originator of the given request is blacklisted
(not honoring rate limits, and thus permanently forbidden access
without the need to maintain further rate limit counters).
The default implementation always returns false. Override this
method in a subclass to implement custom blacklisting logic.
101 102 103 |
# File 'lib/rack/throttle/limiters/limiter.rb', line 101 def blacklisted?(request) false end |
#call(env) ⇒ Array(Integer, Hash, #each)
36 37 38 39 40 41 42 43 44 45 |
# File 'lib/rack/throttle/limiters/limiter.rb', line 36 def call(env) request = Rack::Request.new(env) match_results = @matchers.map { |m| m.match?(request) }.uniq applicable = @matchers.empty? || match_results == [true] if applicable and !allowed?(request) rate_limit_exceeded else app.call(env) end end |
#restricted_url?(path) ⇒ Boolean
Returns true if no :url_rule regex or if the request path
matches the :url regex, false otherwise.
You can override this class, though that might be weird.
55 56 57 |
# File 'lib/rack/throttle/limiters/limiter.rb', line 55 def restricted_url?(path) [:url_rule].nil? || [:url_rule].match(path) end |
#whitelisted?(request) ⇒ Boolean
Returns true if the originator of the given request is whitelisted
(not subject to further rate limits).
The default implementation always returns false. Override this
method in a subclass to implement custom whitelisting logic.
86 87 88 |
# File 'lib/rack/throttle/limiters/limiter.rb', line 86 def whitelisted?(request) false end |