Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
42 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
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 | ||
@max_per_hour ||= 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:%S')].join(':') | ||
end | ||
end | ||
end; end |