Class: Chokepoint::Hourly

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

Overview

This rate limiter strategy throttles the block by defining a maximum number of allowed calls 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 calls per distinct hour. This means that the throttling counter is reset every hour on the hour (according to the local timezone).

Examples:

Allowing up to 3,600 requests per hour

Chokepoint::Hourly.new('activity').throttle do ... end

Allowing up to 100 requests per hour

Chokepoint::Hourly.new('activity', :max => 100).throttle do ... end

Instance Attribute Summary

Attributes inherited from Limiter

#name, #options

Instance Method Summary collapse

Methods inherited from TimeWindow

#allowed?

Methods inherited from Limiter

#allowed?, #blacklisted?, #throttle, #whitelisted?

Constructor Details

#initialize(name, options = {}) ⇒ Hourly

Returns a new instance of Hourly.

Parameters:

  • name (String)
  • options (Hash{Symbol => Object}) (defaults to: {})

Options Hash (options):

  • :max (Integer) — default: 3600


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

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

Instance Method Details

#max_per_hourObject Also known as: max_per_window



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

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