Skip to content

Commit

Permalink
allow to specify delay and multipliers for throttle
Browse files Browse the repository at this point in the history
  • Loading branch information
doits committed Feb 17, 2020
1 parent 9e52aaa commit 2c540fa
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions lib/active_job/traffic_control/throttle.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,28 @@ module Throttle
extend ::ActiveSupport::Concern

class_methods do
def throttle(threshold:, period:, drop: false, key: nil)
def throttle(
threshold:,
period:,
drop: false,
key: nil,
delay: period,
min_delay_multiplier: 1,
max_delay_multiplier: 5
)
raise ArgumentError, "Threshold needs to be an integer > 0" if threshold.to_i < 1
raise ArgumentError, "min_delay_multiplier needs to be a number >= 0" unless min_delay_multiplier.is_a?(Numeric) && min_delay_multiplier >= 0
raise ArgumentError, "max_delay_multiplier needs to be a number >= max_delay_multiplier" unless max_delay_multiplier.is_a?(Numeric) && max_delay_multiplier >= min_delay_multiplier
raise ArgumentError, "delay needs to a number > 0 " unless delay.is_a?(Numeric) && delay > 0

self.job_throttling = {
threshold: threshold,
period: period,
drop: drop,
key: key
key: key,
delay: delay,
min_delay_multiplier: min_delay_multiplier,
max_delay_multiplier: max_delay_multiplier
}
end

Expand Down Expand Up @@ -42,8 +56,10 @@ def throttling_lock_key(job)
elsif self.class.job_throttling[:drop]
drop("throttling")
else
period = self.class.job_throttling[:period]
reenqueue(period...(period * 5), "throttling")
delay = self.class.job_throttling[:delay]
min_delay_multiplier = self.class.job_throttling[:min_delay_multiplier]
max_delay_multiplier = self.class.job_throttling[:max_delay_multiplier]
reenqueue((delay * min_delay_multiplier)...(delay * max_delay_multiplier), "throttling")
end
end
else
Expand Down

0 comments on commit 2c540fa

Please sign in to comment.