Class: Rack::Throttle::Hourly

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

Overview

This rate limiter strategy throttles the application by defining a maximum number of allowed HTTP requests per hour (by default, 3,600 requests per 60 minutes, 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 distinct hour. This means that the throttling counter is reset every hour on the hour (according to the server's local timezone).

Examples:

Allowing up to 3,600 requests per hour

  use Rack::Throttle::Hourly

Allowing up to 100 requests per hour

  use Rack::Throttle::Hourly, :max => 100

Instance Method Summary

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

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

A new instance of Hourly

Parameters:

  • (#call) app
  • (Hash{Symbol => Object}) options (defaults to: {})

Options Hash (options):

  • (Integer) :max — default: 3600


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

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

Instance Method Details

- (String) cache_key(request) (protected)

Parameters:

  • (Rack::Request) request

Returns:

  • (String)


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

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

- (Object) max_per_hour Also known as: max_per_window



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

def max_per_hour
  @max_per_hour ||= options[:max_per_hour] || options[:max] || 3_600
end