class Sidekiq::Throttled::Strategy

Meta-strategy that couples {Concurrency} and {Threshold} strategies.

@private

Attributes

concurrency[R]

@!attribute [r] concurrency

@return [Strategy::Concurrency, nil]
observer[R]

@!attribute [r] observer

@return [Proc, nil]
threshold[R]

@!attribute [r] threshold

@return [Strategy::Threshold, nil]

Public Class Methods

new( name, concurrency: nil, threshold: nil, key_suffix: nil, observer: nil ) click to toggle source

@param [#to_s] name @param [Hash] concurrency Concurrency options.

See keyword args of {Strategy::Concurrency#initialize} for details.

@param [Hash] threshold Threshold options.

See keyword args of {Strategy::Threshold#initialize} for details.

@param [#call] key_suffix Dynamic key suffix generator. @param [#call] observer Process called after throttled.

# File lib/sidekiq/throttled/strategy.rb, line 33
def initialize(
  name,
  concurrency: nil, threshold: nil, key_suffix: nil, observer: nil
)
  @observer    = observer
  @concurrency = make_strategy(Concurrency, name, key_suffix, concurrency)
  @threshold   = make_strategy(Threshold, name, key_suffix, threshold)

  return if @concurrency || @threshold

  raise ArgumentError, "Neither :concurrency nor :threshold given"
end

Public Instance Methods

dynamic?() click to toggle source

@return [Boolean] whenever strategy has dynamic config

# File lib/sidekiq/throttled/strategy.rb, line 47
def dynamic?
  return true if @concurrency&.dynamic?
  return true if @threshold&.dynamic?

  false
end
finalize!(jid, *job_args) click to toggle source

Marks job as being processed. @return [void]

# File lib/sidekiq/throttled/strategy.rb, line 72
def finalize!(jid, *job_args)
  @concurrency&.finalize!(jid, *job_args)
end
reset!() click to toggle source

Resets count of jobs of all avaliable strategies @return [void]

# File lib/sidekiq/throttled/strategy.rb, line 78
def reset!
  @concurrency&.reset!
  @threshold&.reset!
end
throttled?(jid, *job_args) click to toggle source

@return [Boolean] whenever job is throttled or not.

# File lib/sidekiq/throttled/strategy.rb, line 55
def throttled?(jid, *job_args)
  if @concurrency&.throttled?(jid, *job_args)
    @observer&.call(:concurrency, *job_args)
    return true
  end

  if @threshold&.throttled?(*job_args)
    @observer&.call(:threshold, *job_args)
    finalize!(jid, *job_args)
    return true
  end

  false
end

Private Instance Methods

make_strategy(strategy, name, key_suffix, options) click to toggle source

@return [Base, nil]

# File lib/sidekiq/throttled/strategy.rb, line 86
def make_strategy(strategy, name, key_suffix, options)
  return unless options

  strategy.new("throttled:#{name}", :key_suffix => key_suffix, **options)
end