Instantiate TimePeriod from within ThrottlePolicy
parent
ee31e44c1a
commit
7410ac6504
|
@ -2,12 +2,11 @@
|
|||
# Listens to *ALL* incoming logs and stores them to the DB.
|
||||
# Also handles throttling.
|
||||
class LogService < AbstractServiceRunner
|
||||
T = ThrottlePolicy::TimePeriod
|
||||
THROTTLE_POLICY =
|
||||
ThrottlePolicy.new(name,
|
||||
T.new(1.minute) => 0.5 * 1_000,
|
||||
T.new(1.hour) => 0.5 * 10_000,
|
||||
T.new(1.day) => 0.5 * 100_000)
|
||||
THROTTLE_POLICY = ThrottlePolicy.new(name, {
|
||||
1.minute => 0.5 * 1_000,
|
||||
1.hour => 0.5 * 10_000,
|
||||
1.day => 0.5 * 100_000,
|
||||
})
|
||||
|
||||
LOG_TPL = Rails.env.test? ?
|
||||
"\e[32m.\e[0m" : "FBOS LOG (device_%s): %s\n"
|
||||
|
|
|
@ -5,9 +5,9 @@ class TelemetryService < AbstractServiceRunner
|
|||
MESSAGE = "TELEMETRY MESSAGE FROM %s"
|
||||
FAILURE = "FAILED TELEMETRY MESSAGE FROM %s"
|
||||
THROTTLE_POLICY = ThrottlePolicy.new(name, {
|
||||
ThrottlePolicy::TimePeriod.new(1.minute) => 25,
|
||||
ThrottlePolicy::TimePeriod.new(1.hour) => 250,
|
||||
ThrottlePolicy::TimePeriod.new(1.day) => 1500,
|
||||
1.minute => 25,
|
||||
1.hour => 250,
|
||||
1.day => 1500,
|
||||
})
|
||||
|
||||
def process(delivery_info, payload)
|
||||
|
|
|
@ -6,9 +6,10 @@ class ThrottlePolicy
|
|||
attr_reader :rules
|
||||
|
||||
# Dictionary<TimePeriod, Integer>
|
||||
def initialize(namespace, policy_rules)
|
||||
def initialize(namespace, policy_rules, now = Time.now)
|
||||
@rules = policy_rules.map do |(time_period, limit)|
|
||||
Rule.new(namespace, time_period, limit)
|
||||
tp = ThrottlePolicy::TimePeriod.new(time_period, now)
|
||||
Rule.new(namespace, tp, limit)
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -4,9 +4,9 @@ NOW = Time.new("2018-05-18T09:38:02.259-05:00")
|
|||
klass = ThrottlePolicy::TimePeriod
|
||||
describe klass do
|
||||
let(:policy) do
|
||||
ThrottlePolicy.new("rspec", klass.new(1.minute, NOW) => 1,
|
||||
klass.new(1.hour, NOW) => 10,
|
||||
klass.new(1.day, NOW) => 100)
|
||||
ThrottlePolicy.new("rspec", { 1.minute => 1,
|
||||
1.hour => 10,
|
||||
1.day => 100 }, NOW)
|
||||
end
|
||||
|
||||
it "initializes" do
|
||||
|
|
Loading…
Reference in New Issue