Class: Rack::Throttle::Daily

Inherits:
TimeWindow show all
Defined in:
lib/rack/throttle/daily.rb

Overview

This rate limiter strategy throttles the application by defining a maximum number of allowed HTTP requests per day (by default, 86,400 requests per 24 hours, which works out to an average of 1 request per second).

Note that this strategy doesn't use a sliding time window, but rather tracks requests per calendar day. This means that the throttling counter is reset at midnight (according to the server's local timezone) every night.

Examples:

Allowing up to 86,400 requests per day

use Rack::Throttle::Daily

Allowing up to 1,000 requests per day

use Rack::Throttle::Daily, :max => 1000

Instance Attribute Summary

Attributes inherited from Limiter

#app, #options

Instance Method Summary (collapse)

Methods inherited from TimeWindow

#allowed?

Methods inherited from Limiter

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

Constructor Details

- (Daily) initialize(app, options = {})

Returns a new instance of Daily

Options Hash (options):

  • :max (Integer) — default: 86400


24
25
26
# File 'lib/rack/throttle/daily.rb', line 24

def initialize(app, options = {})
  super
end

Instance Method Details

- (String) cache_key(request) (protected)



40
41
42
# File 'lib/rack/throttle/daily.rb', line 40

def cache_key(request)
  [super, Time.now.strftime('%Y-%m-%d')].join(':')
end

- (Object) max_per_day Also known as: max_per_window



29
30
31
# File 'lib/rack/throttle/daily.rb', line 29

def max_per_day
  @max_per_hour ||= options[:max_per_day] || options[:max] || 86_400
end