Skip to content

Configuration

Mohamed Motaweh edited this page Oct 5, 2022 · 3 revisions

Customize the configuration by adding the following block to config/initializers/rate_limit.rb

RateLimit.configure do |config|
  config.redis             = Redis.new
  config.fail_safe         = true
  config.default_interval  = 60
  config.default_threshold = 2
  config.limits_file_path  = 'config/rate-limit.yml'
  config.on_success = proc { |result|
    # Success Logic Goes HERE
    # result.topic, result.value
  }
  config.on_failure = proc { |result|
    # Failure Logic Goes HERE
    # result.topic, result.value,  result.threshold,  result.interval
  }
end
attribute/method Description Default
redis Your redis instance Redis.new
fail_safe Silently handle cache failure when set to true, raises error if false true
default_interval The default threshold of the exceeded limit 60
default_threshold The default interval of the exceeded limit 2
limits_file_path A boolean for the success/failure states of the throttling 'config/rate-limit.yml'
on_success A Global proc that runs on every success rate limit attempt nil
on_failure A Global proc that runs on every failure rate limit attempt nil

Note: default_interval and default_threshold is only used when the given topic does not have defined limits

Define Limits

The config/rate-limit.yml should include the limits you want to enforce on each given topic. In the following format:

topic:
  threshold: interval
Example
  • maximum 2 login attempts per 60 seconds
  • maximum 1 send sms attempts per 60 seconds
  • maximum 5 send sms attempts per 300 seconds
  • maximum 10 send sms attempts per 3000 seconds
login:
  2: 60
send_sms:
  1: 60
  5: 300
  10: 3000
Clone this wiki locally