-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
132 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# frozen_string_literal: true | ||
|
||
module Sidekiq | ||
module Throttled | ||
class Config | ||
# Period in seconds to exclude queue from polling in case it returned | ||
# {#cooldown_threshold} throttled jobs in a row. | ||
# | ||
# Set this to `0.0` to disable cooldown completely. | ||
# | ||
# @return [Float] | ||
attr_reader :cooldown_period | ||
|
||
# Amount of throttled jobs returned from the same queue subsequently after | ||
# which queue will be excluded from polling for the durations of | ||
# {#cooldown_period}. | ||
# | ||
# @return [Integer] | ||
attr_reader :cooldown_threshold | ||
|
||
def initialize | ||
@cooldown_period = 2.0 | ||
@cooldown_threshold = 1 | ||
end | ||
|
||
# @!attribute [w] cooldown_period | ||
def cooldown_period=(value) | ||
raise ArgumentError, "cooldown period must be a non-negative Float" unless value.is_a?(Float) && 0.0 <= value | ||
|
||
@cooldown_period = value | ||
end | ||
|
||
# @!attribute [w] cooldown_threshold | ||
def cooldown_threshold=(value) | ||
unless value.is_a?(Integer) && value.positive? | ||
raise ArgumentError, "cooldown threshold must be a positive Integer" | ||
end | ||
|
||
@cooldown_threshold = value | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# frozen_string_literal: true | ||
|
||
require "concurrent" | ||
|
||
require_relative "./expirable_set" | ||
|
||
module Sidekiq | ||
module Throttled | ||
class Cooldown | ||
class << self | ||
def [](config) | ||
return unless config.cooldown_period.positive? | ||
|
||
new(period: config.cooldown_period, threshold: config.cooldown_threshold) | ||
end | ||
end | ||
|
||
def initialize(period:, threshold:) | ||
@queues = ExpirableSet.new(period) | ||
@threshold = threshold | ||
@tracker = Concurrent::Map.new | ||
end | ||
|
||
def notify_throttled(queue) | ||
@queues.add(queue) if @threshold <= @tracker.merge_pair(queue, 1, &:succ) | ||
end | ||
|
||
def notify_admitted(queue) | ||
@tracker.delete(queue) | ||
end | ||
|
||
# List of queues that should not be polled | ||
# | ||
# @return [Array<String>] | ||
def queues | ||
@queues.to_a | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters