Class: Rack::Throttle::Minute

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

Overview

This rate limiter strategy throttles the application by defining a maximum number of allowed HTTP requests per minute (by default, 60 requests per minute, 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 minute. This means that the throttling counter is reset every minute.

Examples:

Allowing up to 60 requests/minute

use Rack::Throttle::Minute

Allowing up to 100 requests per hour

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

Constant Summary

Constants inherited from Limiter

Limiter::CODE, Limiter::MESSAGE

Instance Attribute Summary

Attributes inherited from Limiter

#app, #options

Instance Method Summary collapse

Methods inherited from TimeWindow

#allowed?

Methods inherited from Limiter

#allowed?, #blacklisted?, #call, #whitelisted?

Constructor Details

#initialize(app, options = {}) ⇒ Minute

Returns a new instance of Minute.

Parameters:

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

Options Hash (options):

  • :max (Integer) — default: 60


23
24
25
# File 'lib/rack/throttle/minute.rb', line 23

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

Instance Method Details

#max_per_minuteObject Also known as: max_per_window



28
29
30
# File 'lib/rack/throttle/minute.rb', line 28

def max_per_minute
  @max_per_hour ||= options[:max_per_minute] || options[:max] || 60
end

#retry_afterObject



32
33
34
# File 'lib/rack/throttle/minute.rb', line 32

def retry_after
  "60" # simplification, because the strategy is not sliding
end