Class: Chokepoint::Limiter
- Inherits:
-
Object
- Object
- Chokepoint::Limiter
- Defined in:
- lib/chokepoint/limiter.rb
Overview
This is the base class for rate limiter implementations.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
-
#allowed?(context = nil) ⇒ Boolean
Returns ‘false` if the rate limit has been exceeded for the given `request`, or `true` otherwise.
-
#blacklisted?(context) ⇒ Boolean
abstract
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).
-
#initialize(name, options = {}) ⇒ Limiter
constructor
A new instance of Limiter.
- #throttle(context = nil) ⇒ Object
-
#whitelisted?(context) ⇒ Boolean
abstract
Returns ‘true` if the originator of the given `request` is whitelisted (not subject to further rate limits).
Constructor Details
#initialize(name, options = {}) ⇒ Limiter
Returns a new instance of Limiter.
23 24 25 |
# File 'lib/chokepoint/limiter.rb', line 23 def initialize(name, = {}) @name, @options = name, end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
13 14 15 |
# File 'lib/chokepoint/limiter.rb', line 13 def name @name end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
14 15 16 |
# File 'lib/chokepoint/limiter.rb', line 14 def @options end |
Instance Method Details
#allowed?(context = nil) ⇒ 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.
40 41 42 43 44 45 46 |
# File 'lib/chokepoint/limiter.rb', line 40 def allowed?(context = nil) case when whitelisted?(context) then true when blacklisted?(context) then false else true # override in subclasses end end |
#blacklisted?(context) ⇒ 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.
73 74 75 |
# File 'lib/chokepoint/limiter.rb', line 73 def blacklisted?(context) false end |
#throttle(context = nil) ⇒ Object
27 28 29 |
# File 'lib/chokepoint/limiter.rb', line 27 def throttle(context = nil) allowed?(context) ? yield : rate_limit_exceeded(context) end |
#whitelisted?(context) ⇒ 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.
58 59 60 |
# File 'lib/chokepoint/limiter.rb', line 58 def whitelisted?(context) false end |