Class: Rack::Throttle::Interval
- Inherits:
-
Limiter
- Object
- Limiter
- Rack::Throttle::Interval
- Defined in:
- lib/rack/throttle/interval.rb
Overview
This rate limiter strategy throttles the application by enforcing a minimum interval (by default, 1 second) between subsequent allowed HTTP requests.
Instance Attribute Summary
Attributes inherited from Limiter
Instance Method Summary (collapse)
-
- (Boolean) allowed?(request)
Returns
trueif sufficient time (equal to or more than #minimum_interval) has passed since the last request and the given presentrequest. -
- (Interval) initialize(app, options = {})
constructor
A new instance of Interval.
-
- (Float) minimum_interval
Returns the required minimal interval (in terms of seconds) that must elapse between two subsequent HTTP requests.
-
- (Float) retry_after
Returns the number of seconds before the client is allowed to retry an HTTP request.
Methods inherited from Limiter
#blacklisted?, #cache, #cache_get, #cache_has?, #cache_key, #cache_set, #call, #client_identifier, #http_error, #http_status, #rate_limit_exceeded, #request_start_time, #whitelisted?
Constructor Details
- (Interval) initialize(app, options = {})
A new instance of Interval
18 19 20 |
# File 'lib/rack/throttle/interval.rb', line 18 def initialize(app, = {}) super end |
Instance Method Details
- (Boolean) allowed?(request)
Returns true if sufficient time (equal to or more than
#minimum_interval) has passed since the last request and the given
present request.
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/rack/throttle/interval.rb', line 29 def allowed?(request) t1 = request_start_time(request) t0 = cache_get(key = cache_key(request)) rescue nil allowed = !t0 || (dt = t1 - t0.to_f) >= minimum_interval begin cache_set(key, t1) allowed rescue => e # If an error occurred while trying to update the timestamp stored # in the cache, we will fall back to allowing the request through. # This prevents the Rack application blowing up merely due to a # backend cache server (Memcached, Redis, etc.) being offline. allowed = true end end |
- (Float) minimum_interval
Returns the required minimal interval (in terms of seconds) that must elapse between two subsequent HTTP requests.
59 60 61 |
# File 'lib/rack/throttle/interval.rb', line 59 def minimum_interval @min ||= (@options[:min] || 1.0).to_f end |
- (Float) retry_after
Returns the number of seconds before the client is allowed to retry an HTTP request.
50 51 52 |
# File 'lib/rack/throttle/interval.rb', line 50 def retry_after minimum_interval end |