Sha256: b6595f2fd62cf28277f76fe3bc2eeb4a0ad43bac8d29c07bdf363384c51e59a4

Contents?: true

Size: 1.24 KB

Versions: 13

Compression:

Stored size: 1.24 KB

Contents

module Rack; module Throttle
  ##
  # This rate limiter strategy throttles the application by defining a
  # maximum number of allowed HTTP requests per day (by default, 86,400
  # requests per 24 hours, 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 calendar day. This means that the throttling counter
  # is reset at midnight (according to the server's local timezone) every
  # night.
  #
  # @example Allowing up to 86,400 requests per day
  #   use Rack::Throttle::Daily
  #
  # @example Allowing up to 1,000 requests per day
  #   use Rack::Throttle::Daily, :max => 1000
  #
  class Daily < TimeWindow
    ##
    # @param  [#call]                  app
    # @param  [Hash{Symbol => Object}] options
    # @option options [Integer] :max   (86400)
    def initialize(app, options = {})
      super
    end

    ##
    def max_per_day
      @max_per_hour ||= options[:max_per_day] || options[:max] || 86_400
    end

    alias_method :max_per_window, :max_per_day

    protected

    ##
    # @param  [Rack::Request] request
    # @return [String]
    def cache_key(request)
      [super, Time.now.strftime('%Y-%m-%d')].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/daily.rb
rack-throttle-0.4.1 lib/rack/throttle/daily.rb
improved-rack-throttle-0.9.0 lib/rack/throttle/limiters/daily.rb
rack-throttle-0.4.0 lib/rack/throttle/daily.rb
improved-rack-throttle-0.8.0 lib/rack/throttle/limiters/daily.rb
improved-rack-throttle-0.7.1 lib/rack/throttle/limiters/daily.rb
improved-rack-throttle-0.7.0 lib/rack/throttle/limiters/daily.rb
improved-rack-throttle-0.6.0 lib/rack/throttle/daily.rb
improved-rack-throttle-0.5.0 lib/rack/throttle/daily.rb
viximo-rack-throttle-0.4.0 lib/rack/throttle/daily.rb
railslove-rack-throttle-0.0.1 lib/rack/throttle/daily.rb
railslove-rack-throttle-0.0.0 lib/rack/throttle/daily.rb
rack-throttle-0.3.0 lib/rack/throttle/daily.rb