mirror of
https://github.com/samsonjs/rack-attack.git
synced 2026-04-27 15:07:41 +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)
|
@notifier ||= ActiveSupport::Notifications if defined?(ActiveSupport::Notifications)
|
||||||
@blacklisted_response ||= lambda {|env| [503, {}, ['Blocked']] }
|
@blacklisted_response ||= lambda {|env| [503, {}, ['Blocked']] }
|
||||||
@throttled_response ||= lambda {|env|
|
@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']]
|
[503, {'Retry-After' => retry_after}, ['Retry later']]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -75,8 +75,8 @@ module Rack::Attack
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def instrument(payload)
|
def instrument(type, payload)
|
||||||
notifier.instrument('rack.attack', payload) if notifier
|
notifier.instrument("rack.attack.#{type}", payload) if notifier
|
||||||
end
|
end
|
||||||
|
|
||||||
def clear!
|
def clear!
|
||||||
|
|
|
||||||
|
|
@ -10,8 +10,8 @@ module Rack
|
||||||
def [](req)
|
def [](req)
|
||||||
block[req].tap {|match|
|
block[req].tap {|match|
|
||||||
if match
|
if match
|
||||||
Rack::Attack.instrument(:type => type, :name => name, :request => req)
|
req.env["rack.attack.matched"] = {type => name}
|
||||||
req.env["rack.attack.#{type}"] = name
|
Rack::Attack.instrument(type, req)
|
||||||
end
|
end
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -22,9 +22,9 @@ module Rack
|
||||||
key = "#{name}:#{discriminator}"
|
key = "#{name}:#{discriminator}"
|
||||||
count = cache.count(key, period)
|
count = cache.count(key, period)
|
||||||
(count > limit).tap do |throttled|
|
(count > limit).tap do |throttled|
|
||||||
Rack::Attack.instrument(:type => :throttle, :name => name, :request => req, :count => count, :throttled => throttled)
|
|
||||||
if 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
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
module Rack
|
module Rack
|
||||||
module Attack
|
module Attack
|
||||||
VERSION = '0.0.3'
|
VERSION = '0.1.0'
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -37,7 +37,7 @@ describe 'Rack::Attack' do
|
||||||
last_response.status.must_equal 503
|
last_response.status.must_equal 503
|
||||||
end
|
end
|
||||||
it "should tag the env" do
|
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
|
end
|
||||||
|
|
||||||
allow_ok_requests
|
allow_ok_requests
|
||||||
|
|
@ -57,7 +57,7 @@ describe 'Rack::Attack' do
|
||||||
last_response.status.must_equal 200
|
last_response.status.must_equal 200
|
||||||
end
|
end
|
||||||
it "should tag the env" do
|
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
|
end
|
||||||
end
|
end
|
||||||
|
|
@ -86,7 +86,7 @@ describe 'Rack::Attack' do
|
||||||
last_response.status.must_equal 503
|
last_response.status.must_equal 503
|
||||||
end
|
end
|
||||||
it 'should tag the env' do
|
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
|
end
|
||||||
it 'should set a Retry-After header' do
|
it 'should set a Retry-After header' do
|
||||||
last_response.headers['Retry-After'].must_equal 1
|
last_response.headers['Retry-After'].must_equal 1
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue