You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

19 lines
559 B

  1. class Rack::Attack
  2. # Rate limits for the API
  3. throttle('api', limit: 150, period: 5.minutes) do |req|
  4. req.ip if req.path.match(/\A\/api\//)
  5. end
  6. self.throttled_response = lambda do |env|
  7. now = Time.now.utc
  8. match_data = env['rack.attack.match_data']
  9. headers = {
  10. 'X-RateLimit-Limit' => match_data[:limit].to_s,
  11. 'X-RateLimit-Remaining' => '0',
  12. 'X-RateLimit-Reset' => (now + (match_data[:period] - now.to_i % match_data[:period])).to_s
  13. }
  14. [429, headers, [{ error: 'Throttled' }.to_json]]
  15. end
  16. end