Skip to content

Instantly share code, notes, and snippets.

@justincampbell
Created March 6, 2015 19:07
Show Gist options
  • Save justincampbell/03022978ad4bffaade5d to your computer and use it in GitHub Desktop.
Save justincampbell/03022978ad4bffaade5d to your computer and use it in GitHub Desktop.
Dividata::Application.config.middleware.use Rack::Attack
Rack::Attack.whitelist("localhost") do |request|
['127.0.0.1'].include? request.ip
end
Rack::Attack.throttle 'requests/minute', limit: 60, period: 1.minute, &:ip
Rack::Attack.throttle 'requests/hour', limit: 600, period: 1.hour, &:ip
Rack::Attack.throttle 'requests/day', limit: 3000, period: 1.day, &:ip
ActiveSupport::Notifications.subscribe('rack.attack') do |_, _, _, _, request|
matched = request.env['rack.attack.matched']
match_data = request.env['rack.attack.match_data']
match_type = request.env['rack.attack.match_type']
unless match_type == :whitelist
count = match_data[:count]
limit = match_data[:limit]
message = "Throttled #{request.ip} (#{count}/#{limit} #{matched})"
Rails.logger.error message
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment