Add @namespace to TimePeriod
parent
4e3aa00030
commit
c2688aab0a
|
@ -1,7 +1,12 @@
|
|||
# Handles devices that spin out of control and send too many logs to the server.
|
||||
# Class Hierarchy:
|
||||
# ThrottlePolicy has => Rules creates => Violation
|
||||
# Violation has => Rule has => TimePeriod
|
||||
# ThrottlePolicy
|
||||
# \
|
||||
# +----> Rule --> TimePeriod
|
||||
# |\
|
||||
# | `--> Rule --> TimePeriod
|
||||
# \_
|
||||
# `-> Rule --> TimePeriod
|
||||
class ThrottlePolicy
|
||||
attr_reader :rules
|
||||
|
||||
|
|
|
@ -4,8 +4,7 @@ class ThrottlePolicy
|
|||
attr_reader :time_period, :limit
|
||||
|
||||
def initialize(namespace, time_period, limit, now = Time.now)
|
||||
@namespace = namespace
|
||||
@time_period = ThrottlePolicy::TimePeriod.new(time_period, now)
|
||||
@time_period = ThrottlePolicy::TimePeriod.new(namespace, time_period, now)
|
||||
@limit = limit
|
||||
end
|
||||
|
||||
|
|
|
@ -14,16 +14,16 @@ class ThrottlePolicy
|
|||
:current_period, # Slice time into fixed size windows
|
||||
:entries
|
||||
|
||||
def initialize(active_support_duration, now = Time.now)
|
||||
raise "Backtrace this" unless active_support_duration.is_a?(ActiveSupport::Duration)
|
||||
@time_unit = active_support_duration
|
||||
reset_everything now
|
||||
def initialize(namespace, duration, now = Time.now)
|
||||
@time_unit = duration
|
||||
@namespace = namespace
|
||||
reset_everything(now)
|
||||
end
|
||||
|
||||
def record_event(unique_id, now = Time.now)
|
||||
period = calculate_period(now)
|
||||
case period <=> current_period
|
||||
when -1 then return # Out of date- don't record.
|
||||
when -1 then return # Out of date- don't record.
|
||||
when 0 then increment_count_for(unique_id) # Right on schedule.
|
||||
when 1 then reset_everything(now) # Clear out old data.
|
||||
end
|
||||
|
|
|
@ -5,14 +5,14 @@ describe ThrottlePolicy::TimePeriod do
|
|||
|
||||
it "sets a time unit window size" do
|
||||
expected_time_period = stub_time.to_i / 1.minute.to_i
|
||||
one_min = ThrottlePolicy::TimePeriod.new(1.minute, stub_time)
|
||||
one_min = ThrottlePolicy::TimePeriod.new("RSPEC0", 1.minute, stub_time)
|
||||
expect(one_min.current_period).to eq(expected_time_period)
|
||||
expect(one_min.time_unit).to eq(60)
|
||||
expect(one_min.entries).to eq({})
|
||||
end
|
||||
|
||||
it "increments the count" do
|
||||
t = ThrottlePolicy::TimePeriod.new(1.minute, stub_time)
|
||||
t = ThrottlePolicy::TimePeriod.new("RSPEC1", 1.minute, stub_time)
|
||||
uid = 123
|
||||
|
||||
# Ignore events from the past.
|
||||
|
@ -33,7 +33,7 @@ describe ThrottlePolicy::TimePeriod do
|
|||
end
|
||||
|
||||
it "tells you when the next time period starts" do
|
||||
one_hour = ThrottlePolicy::TimePeriod.new(1.hour, stub_time)
|
||||
one_hour = ThrottlePolicy::TimePeriod.new("RSPEC2", 1.hour, stub_time)
|
||||
next_hour = one_hour.when_does_next_period_start?
|
||||
expect(next_hour).to be_kind_of(Time)
|
||||
expect(next_hour.hour).to be(stub_time.hour + 1)
|
||||
|
|
Loading…
Reference in New Issue