Class: Rack::Throttle::Second

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

Overview

This rate limiter strategy throttles the application by defining a maximum number of allowed HTTP requests per second (by default, 1 request per second.

Note that this strategy doesn't use a sliding time window, but rather tracks requests per distinct second. This means that the throttling counter is reset every second.

Examples:

Allowing up to 1 request/second

use Rack::Throttle::Second

Allowing up to 100 requests per second

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

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 = {}) ⇒ Second

Returns a new instance of Second.

Parameters:

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

Options Hash (options):

  • :max (Integer) — default: 1


22
23
24
# File 'lib/rack/throttle/second.rb', line 22

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

Instance Method Details

#max_per_second(request = nil) ⇒ Object Also known as: max_per_window



27
28
29
# File 'lib/rack/throttle/second.rb', line 27

def max_per_second(request = nil)
  @max_per_second ||= options[:max_per_second] || options[:max] || 1
end