Not working for rails 5.0.1 api only app
I have put the rack-attack in the gem file. In the application.rb did config.middleware.use Rack::Attack
class Rack::Attack
throttle('req/ip', :limit => 2, :period => 2.minutes) do |req|
req.ip # unless req.path.start_with?('/assets')
end
end
The above is config/intializers/rack-attack.rb
Is there something else I need to do here? I did the same thing with rails 4.2 app, it works fine there. But here it does not seem to work
Well what's broken? Is there an error message that gors with it?
Well with the above config, I should get 429 status code if I make more than 2 requests for 2 minutes. But it's not stopping the client from making more than 2 requests
Have you tried restarting your rails server?
Yes I did .. but still it's the same, do you mind telling if it's working for you.
After playing, this worked for me:
config/initializers/rack-attack.rb:
class Rack::Attack
Rack::Attack.cache.store = ActiveSupport::Cache::MemoryStore.new
throttle('api/ip', limit: 2, period: 2.minutes) do |req|
req.ip
end
end
Looks like the docs do mention this, actually:
Optionally configure the cache store for throttling or fail2ban filtering
It is worded weird though.
@vamsipavanmahesh usually you want to state that the above resolved your issue before you close it, so people stuffing the internet at a later date can use the same technique. (The only reason I know this resolved your issue is because I talked to you outside of GitHub)
Okies, lesson learnt .. the above solution worked!!
After playing, this worked for me:
config/initializers/rack-attack.rb:
class Rack::Attack Rack::Attack.cache.store = ActiveSupport::Cache::MemoryStore.new throttle('api/ip', limit: 2, period: 2.minutes) do |req| req.ip end end
I Implement the solution and working fine for me Thanks