class RateLimiter::Throttle
Limits the rate at which new records of an ActiveRecord model can be saved. Checks to see if RateLimiter
is enabled and, if all conditions are satisfied and if any other records have been created within the time interval for rate limiting.
Constants
- DEFAULT_INTERVAL
- DEFAULT_ON_FIELD
- DEFAULT_TIMESTAMP_FIELD
Public Class Methods
# File lib/rate_limiter/throttle.rb, line 13 def initialize(record, options = {}) @record = record @options = options end
Public Instance Methods
Has the rate limit been exceeded?
# File lib/rate_limiter/throttle.rb, line 19 def exceeded? enabled? && rate_limit? && others_for_rate_limiting.present? end
Private Instance Methods
Is RateLimiter
enabled for this particular record?
# File lib/rate_limiter/throttle.rb, line 26 def enabled? RateLimiter.enabled? && RateLimiter.request.enabled? && RateLimiter.request.enabled_for_model?(@record.class) end
Proc that if present must return true for RateLimiter
to be enabled for this particular record.
# File lib/rate_limiter/throttle.rb, line 34 def if_condition @options[:if_condition] end
The time interval (in seconds)
# File lib/rate_limiter/throttle.rb, line 39 def interval @options[:interval] || DEFAULT_INTERVAL end
Arel query for finding records within the interval.
# File lib/rate_limiter/throttle.rb, line 44 def interval_query_params @record.class.arel_table[timestamp_field].gteq(Time.current - interval) end
An Array of model attribute names to match against.
# File lib/rate_limiter/throttle.rb, line 49 def on Array(@options[:on] || DEFAULT_ON_FIELD) end
Arel query for finding records that match the on attributes.
# File lib/rate_limiter/throttle.rb, line 54 def on_query_params on.map { |attr| @record.class.arel_table[attr].eq(@record.send(attr)) }.inject(&:or) end
Other records that fall within the time interval.
# File lib/rate_limiter/throttle.rb, line 59 def others_for_rate_limiting @record.class.where(on_query_params).where(interval_query_params) end
Are all of the conditions for rate limiting met for this record?
# File lib/rate_limiter/throttle.rb, line 64 def rate_limit? (if_condition.blank? || if_condition.call(@record)) && !unless_condition.try(:call, @record) end
Timestamp field to test the interval against.
# File lib/rate_limiter/throttle.rb, line 69 def timestamp_field @options[:timestamp_field] || DEFAULT_TIMESTAMP_FIELD end
Proc that if present must return false for RateLimiter
to be enabled for this particular record.
# File lib/rate_limiter/throttle.rb, line 75 def unless_condition @options[:unless_condition] end