Skip to content

Commit

Permalink
Make strategies inheritance optional.
Browse files Browse the repository at this point in the history
  • Loading branch information
ixti committed Oct 3, 2018
1 parent a470eb6 commit 4f2b83f
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 1 deletion.
6 changes: 6 additions & 0 deletions lib/sidekiq/throttled.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
# internal
require "sidekiq/throttled/version"
require "sidekiq/throttled/communicator"
require "sidekiq/throttled/configuration"
require "sidekiq/throttled/queues_pauser"
require "sidekiq/throttled/registry"
require "sidekiq/throttled/worker"
Expand Down Expand Up @@ -47,6 +48,11 @@ module Throttled
class << self
include Utils

# @return [Configuration]
def configuration
@configuration ||= Configuration.new
end

# Hooks throttler into sidekiq.
#
# @return [void]
Expand Down
48 changes: 48 additions & 0 deletions lib/sidekiq/throttled/configuration.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# frozen_string_literal: true

module Sidekiq
module Throttled
# Configuration holder.
class Configuration
# Class constructor.
def initialize
reset!
end

# Reset configuration to defaults.
#
# @return [void]
def reset!
@inherit_strategies = false
end

# Instructs throttler to lookup strategies in parent classes, if there's
# no own strategy:
#
# class Foo
# include Sidekiq::Worker
# include Sidekiq::Worker::Throttled
#
# sidekiq_throttle :concurrency => { :limit => 42 }
# end
#
# class Bar < Foo
# end
#
# By default in the example above, `Bar` won't have throttling options.
# Set this flag to `true` to enable this lookup in initializer, after
# that `Bar` will use `Foo` throttling bucket.
def inherit_strategies=(value)
@inherit_strategies = value ? true : false
end

# Whenever throttled workers should inherit parent's strategies or not.
# Default: `false`.
#
# @return [Boolean]
def inherit_strategies?
@inherit_strategies
end
end
end
end
2 changes: 2 additions & 0 deletions lib/sidekiq/throttled/registry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ def find(name)
# @param name [Class, #to_s]
# @return [Strategy, nil]
def find_by_class(name)
return unless Throttled.configuration.inherit_strategies?

const = name.is_a?(Class) ? name : constantize(name)
return unless const.is_a?(Class)

Expand Down
10 changes: 9 additions & 1 deletion spec/sidekiq/throttled/registry_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,15 @@ def capture_output

before { described_class.add(parent_class.name, threshold) }

it { is_expected.to be_a Sidekiq::Throttled::Strategy }
it { is_expected.to be_nil }

context "when configuration has inherit strategy turned on" do
before { Sidekiq::Throttled.configuration.inherit_strategies = true }

after { Sidekiq::Throttled.configuration.reset! }

it { is_expected.to be described_class.get("Parent") }
end
end
end

Expand Down

0 comments on commit 4f2b83f

Please sign in to comment.