Remove TimePeriod references from ThrottlePolicy

pull/1543/head
Rick Carlino 2019-10-28 11:25:37 -05:00
parent a501decb1c
commit cb2e97cbb2
2 changed files with 10 additions and 3 deletions

View File

@ -14,7 +14,7 @@ class ThrottlePolicy
end
def track(unique_id, now = Time.now)
rules.each { |r| r.time_period.record_event(unique_id, now) }
rules.each { |r| r.record_event(unique_id, now) }
end
# If throttled, returns the timeperiod when device will be unthrottled
@ -22,8 +22,7 @@ class ThrottlePolicy
def is_throttled(unique_id)
rules
.map do |rule|
is_violation = rule.time_period.usage_count_for(unique_id) > rule.limit
is_violation ? Violation.new(rule) : nil
rule.violation?(unique_id) ? Violation.new(rule) : nil
end
.compact
.max

View File

@ -7,5 +7,13 @@ class ThrottlePolicy
@namespace = namespace
@time_period, @limit = time_period, limit
end
def violation?(unique_id)
time_period.usage_count_for(unique_id) > limit
end
def record_event(unique_id, now)
time_period.record_event(unique_id, now)
end
end
end