1
0
Fork 0
forked from OpenNeo/impress
impress/config/initializers/attack.rb
Matchu bb20925382 Fix deprecation warning in Rack::Attack
Looking at the docs, I think what changed is that `throttled_responder` gets the request as an argument instead of the `env`? And has the same return type for the lambda as before?

So uhhh I don't remember how to test this, but uhh it's not crashing when the server starts anymore, and I feel like the most likely problem here would be that you get a 500 instead of a useful response in the rate limit case, so like. ehh I'll just leave it be!
2023-10-23 19:05:09 -07:00

22 lines
No EOL
775 B
Ruby

Rack::Attack.throttle('pets/ip', limit: 3, period: 30.seconds) 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_responder = lambda do |req|
if req.env['rack.attack.matched'] == 'pets/ip'
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