Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for period ranges #81

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions lib/rack/attack/cache.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,17 @@ def store=(store)

def count(unprefixed_key, period)
epoch_time = Time.now.to_i
expires_in = period - (epoch_time % period)
key = "#{prefix}:#{(epoch_time/period).to_i}:#{unprefixed_key}"
if period.is_a? Range
expires_in = period.end - epoch_time
# Manually create string representation to force cast both ends to int
# (instead of using Range#to_s)
period_key = "#{period.begin.to_i}..#{period.end.to_i}"
else
expires_in = period - (epoch_time % period)
period_key = (epoch_time/period).to_i
end

key = "#{prefix}:#{period_key}:#{unprefixed_key}"
do_count(key, expires_in)
end

Expand Down
7 changes: 4 additions & 3 deletions lib/rack/attack/throttle.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def initialize(name, options, block)
raise ArgumentError.new("Must pass #{opt.inspect} option") unless options[opt]
end
@limit = options[:limit]
@period = options[:period].to_i
@period = options[:period]
@type = options.fetch(:type, :throttle)
end

Expand All @@ -22,11 +22,12 @@ def [](req)
return false unless discriminator

key = "#{name}:#{discriminator}"
count = cache.count(key, period)
current_period = period.respond_to?(:call) ? period.call(req) : period
current_limit = limit.respond_to?(:call) ? limit.call(req) : limit
count = cache.count(key, current_period)
data = {
:count => count,
:period => period,
:period => current_period,
:limit => current_limit
}
(req.env['rack.attack.throttle_data'] ||= {})[name] = data
Expand Down
27 changes: 27 additions & 0 deletions spec/rack_attack_cache_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
require_relative 'spec_helper'

describe Rack::Attack::Cache do
describe "#count" do
before do
@cache = Rack::Attack::Cache.new
@store = MiniTest::Mock.new
@cache.store = @store
end
describe "with a integer period" do
it "should set key with using current time over period" do
Time.stub(:now, 30) do
@store.expect(:increment, true, ["rack::attack:1:cache-test-key", 1, {expires_in: 20}])
@cache.count("cache-test-key", 25)
end
end
end
describe "with a range period" do
it "should set key with using current time over period" do
Time.stub(:now, 6) do
@store.expect(:increment, true, ["rack::attack:5..10:cache-test-key", 1, {expires_in: 4}])
@cache.count("cache-test-key", (5..10))
end
end
end
end
end