throttle pet loads per ip
This commit is contained in:
parent
5e60795f31
commit
abe35c6fd9
5 changed files with 30 additions and 0 deletions
2
Gemfile
2
Gemfile
|
@ -62,6 +62,8 @@ gem "rest-client", "~> 1.6.7"
|
||||||
|
|
||||||
gem "rails-i18n"
|
gem "rails-i18n"
|
||||||
|
|
||||||
|
gem 'rack-attack', '~> 2.2.0'
|
||||||
|
|
||||||
# Needed for the new asset pipeline
|
# Needed for the new asset pipeline
|
||||||
group :assets do
|
group :assets do
|
||||||
|
|
||||||
|
|
|
@ -190,6 +190,8 @@ GEM
|
||||||
dye (>= 0.1.1)
|
dye (>= 0.1.1)
|
||||||
yard (>= 0.6.3)
|
yard (>= 0.6.3)
|
||||||
rack (1.4.5)
|
rack (1.4.5)
|
||||||
|
rack-attack (2.2.0)
|
||||||
|
rack
|
||||||
rack-cache (1.2)
|
rack-cache (1.2)
|
||||||
rack (>= 0.4)
|
rack (>= 0.4)
|
||||||
rack-fiber_pool (0.9.2)
|
rack-fiber_pool (0.9.2)
|
||||||
|
@ -325,6 +327,7 @@ DEPENDENCIES
|
||||||
openneo-auth-signatory (~> 0.1.0)
|
openneo-auth-signatory (~> 0.1.0)
|
||||||
parallel (~> 0.5.17)
|
parallel (~> 0.5.17)
|
||||||
patron (~> 0.4.18)
|
patron (~> 0.4.18)
|
||||||
|
rack-attack (~> 2.2.0)
|
||||||
rack-fiber_pool
|
rack-fiber_pool
|
||||||
rails (= 3.2.13)
|
rails (= 3.2.13)
|
||||||
rails-i18n
|
rails-i18n
|
||||||
|
|
|
@ -50,6 +50,8 @@ module OpenneoImpressItems
|
||||||
config.assets.paths << Rails.root.join('app', 'assets', 'fonts')
|
config.assets.paths << Rails.root.join('app', 'assets', 'fonts')
|
||||||
config.assets.precompile << '*.js'
|
config.assets.precompile << '*.js'
|
||||||
config.assets.initialize_on_precompile = false
|
config.assets.initialize_on_precompile = false
|
||||||
|
|
||||||
|
config.middleware.insert_after ActionDispatch::Flash, Rack::Attack
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
23
config/initializers/attack.rb
Normal file
23
config/initializers/attack.rb
Normal file
|
@ -0,0 +1,23 @@
|
||||||
|
Rack::Attack.throttle('pets/ip', limit: 10, period: 1.minute) do |req|
|
||||||
|
Rails.logger.debug "Pets hit? #{req.path.inspect} #{req.ip.inspect}"
|
||||||
|
req.ip if req.path.start_with?('/pets/load')
|
||||||
|
end
|
||||||
|
|
||||||
|
PETS_THROTTLE_MESSAGE = "We've received a lot of pet names from you " +
|
||||||
|
"recently, so we're giving our servers a break. Try " +
|
||||||
|
"again in a minute or so. Thanks!"
|
||||||
|
|
||||||
|
Rack::Attack.throttled_response = lambda do |env|
|
||||||
|
if env['rack.attack.matched'] == 'pets/ip'
|
||||||
|
req = ActionDispatch::Request.new(env)
|
||||||
|
if req.path.end_with?('.json')
|
||||||
|
[503, {}, [PETS_THROTTLE_MESSAGE]]
|
||||||
|
else
|
||||||
|
flash = req.flash
|
||||||
|
flash[:warning] = PETS_THROTTLE_MESSAGE
|
||||||
|
[302, {"Location" => "/"}, [PETS_THROTTLE_MESSAGE]]
|
||||||
|
end
|
||||||
|
else
|
||||||
|
[503, {}, ["Retry later"]]
|
||||||
|
end
|
||||||
|
end
|
BIN
vendor/cache/rack-attack-2.2.0.gem
vendored
Normal file
BIN
vendor/cache/rack-attack-2.2.0.gem
vendored
Normal file
Binary file not shown.
Loading…
Reference in a new issue