Class: Rack::Throttle::TimeWindow

Inherits:
Limiter
  • Object
show all
Defined in:
lib/rack/throttle/time_window.rb

Direct Known Subclasses

Daily, Hourly

Instance Attribute Summary

Attributes inherited from Limiter

#app, #options

Instance Method Summary collapse

Methods inherited from Limiter

#blacklisted?, #cache, #cache_get, #cache_has?, #cache_key, #cache_set, #call, #client_identifier, #http_error, #http_status, #initialize, #rate_limit_exceeded, #request_start_time, #whitelisted?

Constructor Details

This class inherits a constructor from Rack::Throttle::Limiter

Instance Method Details

#allowed?(request) ⇒ Boolean

Returns true if fewer than the maximum number of requests permitted for the current window of time have been made.

Parameters:

  • request (Rack::Request)

Returns:

  • (Boolean)


10
11
12
13
14
15
16
17
18
19
# File 'lib/rack/throttle/time_window.rb', line 10

def allowed?(request)
  count = cache_get(key = cache_key(request)).to_i + 1 rescue 1
  allowed = count <= max_per_window.to_i
  begin
    cache_set(key, count)
    allowed
  rescue => e
    allowed = true
  end
end