Class: Aikido::Zen::Sinks::ActionController::Throttler
- Inherits:
-
Object
- Object
- Aikido::Zen::Sinks::ActionController::Throttler
- Defined in:
- lib/aikido/zen/sinks/action_controller.rb
Overview
Implements the “middleware” for rate limiting in Rails apps, where we need to check at the end of the ‘before_action` chain, rather than in an actual Rack middleware, to allow for calls to Zen.track_user being made from before_actions in the host app, thus allowing rate-limiting by user ID rather than solely by IP.
Instance Method Summary collapse
-
#initialize(config: Aikido::Zen.config, settings: Aikido::Zen.runtime_settings, rate_limiter: Aikido::Zen::RateLimiter.new) ⇒ Throttler
constructor
A new instance of Throttler.
- #throttle(controller) ⇒ Object
Constructor Details
#initialize(config: Aikido::Zen.config, settings: Aikido::Zen.runtime_settings, rate_limiter: Aikido::Zen::RateLimiter.new) ⇒ Throttler
Returns a new instance of Throttler.
12 13 14 15 16 17 18 19 20 |
# File 'lib/aikido/zen/sinks/action_controller.rb', line 12 def initialize( config: Aikido::Zen.config, settings: Aikido::Zen.runtime_settings, rate_limiter: Aikido::Zen::RateLimiter.new ) @config = config @settings = settings @rate_limiter = rate_limiter end |
Instance Method Details
#throttle(controller) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/aikido/zen/sinks/action_controller.rb', line 22 def throttle(controller) context = controller.request.env[Aikido::Zen::ENV_KEY] request = context.request if should_throttle?(request) status, headers, body = @config.rate_limited_responder.call(request) controller.headers.update(headers) controller.render plain: Array(body).join, status: status return true end false end |