Speed ​​limit for rail controllers

I am looking for a speed limiting mechanism for my rails 3 application. I found something, but that is not what I need. I found a gem with a throttle and a gem. It seems that the throttle works for every rail request, but I need to limit the requests to just one action. Curbit was updated two years ago. Can someone tell me about any other speed limiting mechanisms I can use? Please note that it should work with caching.

+7
source share
2 answers

Well, finally, the throttle is a good solution.

You can do it as follows. You need to define your custom delimiter. It can be based on any of the following restrictions:

Rack::Throttle::Limiter Rack::Throttle::Interval Rack::Throttle::Hourly Rack::Throttle::Daily 

All you have to do is infer from one of the above classes in order to define custom logic. For example:

 class CustomLimiter < Rack::Throttle::Interval def allowed?(request) #custom logic here end end 

You must put this file in the path RAILS_ROOT/lib . Then, in the application.rb file, you must specify which class to use as the limiter. For example, if you want to apply a limiter to only one action, you can do it like this:

 #lib/custom_limiter.rb class CustomLimiter < Rack::Throttle::Interval def allowed?(request) path_info = Rails.application.routes.recognize_path request.url rescue {} if path_info[:controller] == "application" and path_info[:action] == "check_answer" super else true end end end #config/application.rb class Application < Rails::Application ... #Set up rate limiting config.require "custom_limiter" config.middleware.use CustomLimiter, :min => 0.2 ... end 

You may need to take this into account.

I hope it will be useful

UPD:

you can check another solution: rack-attack

+13
source

rack-throttle does what you want. Subclass Limiter and Define Your Own #allowed? . Just return true if the request is not the action you want to receive and do not consider it a limit. Take a look at daily.rb . Override #cache_set so that it does not save those that do not match the route you want to limit.

+2
source

All Articles