Sha256: 42a8481af315f5a5a2e47e23a9e642610857aff65793d5968837512372f29a2b

Contents?: true

Size: 1.25 KB

Versions: 13

Compression:

Stored size: 1.25 KB

Contents

module Rack; module Throttle
  ##
  # 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).
  #
  # @example Allowing up to 3,600 requests per hour
  #   use Rack::Throttle::Hourly
  #
  # @example Allowing up to 100 requests per hour
  #   use Rack::Throttle::Hourly, :max => 100
  #
  class Hourly < TimeWindow
    ##
    # @param  [#call]                  app
    # @param  [Hash{Symbol => Object}] options
    # @option options [Integer] :max   (3600)
    def initialize(app, options = {})
      super
    end

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

    alias_method :max_per_window, :max_per_hour

    protected

    ##
    # @param  [Rack::Request] request
    # @return [String]
    def cache_key(request)
      [super, Time.now.strftime('%Y-%m-%dT%H')].join(':')
    end
  end
end; end

Version data entries

13 entries across 13 versions & 4 rubygems

Version Path
rack-throttle-0.4.2 lib/rack/throttle/hourly.rb
rack-throttle-0.4.1 lib/rack/throttle/hourly.rb
improved-rack-throttle-0.9.0 lib/rack/throttle/limiters/hourly.rb
rack-throttle-0.4.0 lib/rack/throttle/hourly.rb
improved-rack-throttle-0.8.0 lib/rack/throttle/limiters/hourly.rb
improved-rack-throttle-0.7.1 lib/rack/throttle/limiters/hourly.rb
improved-rack-throttle-0.7.0 lib/rack/throttle/limiters/hourly.rb
improved-rack-throttle-0.6.0 lib/rack/throttle/hourly.rb
improved-rack-throttle-0.5.0 lib/rack/throttle/hourly.rb
viximo-rack-throttle-0.4.0 lib/rack/throttle/hourly.rb
railslove-rack-throttle-0.0.1 lib/rack/throttle/hourly.rb
railslove-rack-throttle-0.0.0 lib/rack/throttle/hourly.rb
rack-throttle-0.3.0 lib/rack/throttle/hourly.rb