mirror of
https://github.com/samsonjs/rack-attack.git
synced 2026-03-25 09:25:49 +00:00
Cleanup instrumentation interface
This commit is contained in:
parent
359ebe9068
commit
61a046a203
5 changed files with 11 additions and 11 deletions
|
|
@ -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!
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
module Rack
|
||||
module Attack
|
||||
VERSION = '0.0.3'
|
||||
VERSION = '0.1.0'
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Reference in a new issue