Sha256: 34457e2dee4ad57e7b24d6d4e8fc247a20177e130528665a659f203789b8927e

Contents?: true

Size: 1.15 KB

Versions: 4

Compression:

Stored size: 1.15 KB

Contents

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

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

    alias_method :max_per_window, :max_per_second

    protected

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

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
rack-throttle-0.7.1 lib/rack/throttle/second.rb
rack-throttle-0.7.0 lib/rack/throttle/second.rb
rack-throttle-0.6.0 lib/rack/throttle/second.rb
rack-throttle-0.5.0 lib/rack/throttle/second.rb