Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: allow to specify delay and multipliers for throttle #14

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion activejob-traffic_control.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Gem::Specification.new do |spec|
spec.add_dependency "activesupport", ">= 4.2"
spec.add_dependency "suo"

spec.add_development_dependency "bundler", "~> 1.12"
spec.add_development_dependency "bundler", "~> 2.0"
spec.add_development_dependency "rake", "~> 10.0"
spec.add_development_dependency "minitest", "~> 5.0"
spec.add_development_dependency "codeclimate-test-reporter", "~> 0.4.7"
Expand Down
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