Skip to content

Throttling

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

WIP

The throttle method expects the following options

Option Type Description Examples
topic* String or supports .to_s The topic you would like to throttle "login", "send_sms", "redeem_voucher"
value* String or supports .to_s The identifier of the unique entity that is throttled based on the topic limits user_id, phone_number, voucher_code
raise_errors Boolean (Default: false)
only_failure Boolean (Default: false)

The throttle method checks if the given value did exceed the defined limits for the given topic. If the limit is exceeded then it returns RateLimit::Result Object, where result.success? will be false. Otherwise, it increments the attempts counter in the cache and sets result.success? to true.

Example

result = RateLimit.throttle(topic: :login, value: 123)

if result.success?
  # Do something
end
Clone this wiki locally