diff --git a/lib/rack/attack.rb b/lib/rack/attack.rb index 34fddec..5b22baf 100644 --- a/lib/rack/attack.rb +++ b/lib/rack/attack.rb @@ -34,7 +34,7 @@ module Rack::Attack @notifier ||= ActiveSupport::Notifications if defined?(ActiveSupport::Notifications) @blacklisted_response ||= lambda {|env| [503, {}, ['Blocked']] } @throttled_response ||= lambda {|env| - retry_after = env['rack.attack.throttled'][:period] rescue nil + retry_after = env['rack.attack.matched'][:period] rescue nil [503, {'Retry-After' => retry_after}, ['Retry later']] } @@ -75,8 +75,8 @@ module Rack::Attack end end - def instrument(payload) - notifier.instrument('rack.attack', payload) if notifier + def instrument(type, payload) + notifier.instrument("rack.attack.#{type}", payload) if notifier end def clear! diff --git a/lib/rack/attack/check.rb b/lib/rack/attack/check.rb index 8e97558..032b322 100644 --- a/lib/rack/attack/check.rb +++ b/lib/rack/attack/check.rb @@ -10,8 +10,8 @@ module Rack def [](req) block[req].tap {|match| if match - Rack::Attack.instrument(:type => type, :name => name, :request => req) - req.env["rack.attack.#{type}"] = name + req.env["rack.attack.matched"] = {type => name} + Rack::Attack.instrument(type, req) end } end diff --git a/lib/rack/attack/throttle.rb b/lib/rack/attack/throttle.rb index 068ff6e..b8f61ba 100644 --- a/lib/rack/attack/throttle.rb +++ b/lib/rack/attack/throttle.rb @@ -22,9 +22,9 @@ module Rack key = "#{name}:#{discriminator}" count = cache.count(key, period) (count > limit).tap do |throttled| - Rack::Attack.instrument(:type => :throttle, :name => name, :request => req, :count => count, :throttled => throttled) if throttled - req.env['rack.attack.throttled'] = {:name => name, :count => count, :period => period, :limit => limit} + req.env['rack.attack.matched'] = {:throttle => name, :count => count, :period => period, :limit => limit} + Rack::Attack.instrument(:throttle, req) end end end diff --git a/lib/rack/attack/version.rb b/lib/rack/attack/version.rb index 3d4b612..bec5ffc 100644 --- a/lib/rack/attack/version.rb +++ b/lib/rack/attack/version.rb @@ -1,5 +1,5 @@ module Rack module Attack - VERSION = '0.0.3' + VERSION = '0.1.0' end end diff --git a/spec/rack_attack_spec.rb b/spec/rack_attack_spec.rb index ecaa7ee..18cd583 100644 --- a/spec/rack_attack_spec.rb +++ b/spec/rack_attack_spec.rb @@ -37,7 +37,7 @@ describe 'Rack::Attack' do last_response.status.must_equal 503 end it "should tag the env" do - last_request.env['rack.attack.blacklist'].must_equal "ip #{@bad_ip}" + last_request.env['rack.attack.matched'].must_equal({:blacklist => "ip #{@bad_ip}"}) end allow_ok_requests @@ -57,7 +57,7 @@ describe 'Rack::Attack' do last_response.status.must_equal 200 end it "should tag the env" do - last_request.env['rack.attack.whitelist'].must_equal 'good ua' + last_request.env['rack.attack.matched'].must_equal({:whitelist => 'good ua'}) end end end @@ -86,7 +86,7 @@ describe 'Rack::Attack' do last_response.status.must_equal 503 end it 'should tag the env' do - last_request.env['rack.attack.throttled'].must_equal({:name => 'ip/sec', :count => 2, :limit => 1, :period => 1}) + last_request.env['rack.attack.matched'].must_equal({:throttle => 'ip/sec', :count => 2, :limit => 1, :period => 1}) end it 'should set a Retry-After header' do last_response.headers['Retry-After'].must_equal 1