Class: Sidekiq::Throttled::Strategy

Inherits:
Object
  • Object
show all
Defined in:
lib/sidekiq/throttled/strategy.rb,
lib/sidekiq/throttled/strategy/base.rb,
lib/sidekiq/throttled/strategy/threshold.rb,
lib/sidekiq/throttled/strategy/concurrency.rb

Overview

Meta-strategy that couples Concurrency and Threshold strategies.

Defined Under Namespace

Modules: Base Classes: Concurrency, Threshold

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, concurrency: nil, threshold: nil, key_suffix: nil, observer: nil) ⇒ Strategy

Returns a new instance of Strategy.

Parameters:

Raises:

  • (ArgumentError)


34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/sidekiq/throttled/strategy.rb', line 34

def initialize(name, concurrency: nil, threshold: nil, key_suffix: nil, observer: nil) # rubocop:disable Metrics/MethodLength
  @observer = observer

  @concurrency = StrategyCollection.new(concurrency,
    strategy:   Concurrency,
    name:       name,
    key_suffix: key_suffix)

  @threshold = StrategyCollection.new(threshold,
    strategy:   Threshold,
    name:       name,
    key_suffix: key_suffix)

  return if @concurrency.any? || @threshold.any?

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

Instance Attribute Details

#concurrencyObject (readonly)

Returns the value of attribute concurrency.



17
18
19
# File 'lib/sidekiq/throttled/strategy.rb', line 17

def concurrency
  @concurrency
end

#observerObject (readonly)

Returns the value of attribute observer.



25
26
27
# File 'lib/sidekiq/throttled/strategy.rb', line 25

def observer
  @observer
end

#thresholdObject (readonly)

Returns the value of attribute threshold.



21
22
23
# File 'lib/sidekiq/throttled/strategy.rb', line 21

def threshold
  @threshold
end

Instance Method Details

#dynamic?Boolean

Returns whenever strategy has dynamic config.

Returns:

  • (Boolean)

    whenever strategy has dynamic config



53
54
55
56
57
58
# File 'lib/sidekiq/throttled/strategy.rb', line 53

def dynamic?
  return true if @concurrency&.dynamic?
  return true if @threshold&.dynamic?

  false
end

#finalize!(jid, *job_args) ⇒ void

This method returns an undefined value.

Marks job as being processed.



79
80
81
# File 'lib/sidekiq/throttled/strategy.rb', line 79

def finalize!(jid, *job_args)
  @concurrency&.finalize!(jid, *job_args)
end

#reset!void

This method returns an undefined value.

Resets count of jobs of all avaliable strategies



85
86
87
88
# File 'lib/sidekiq/throttled/strategy.rb', line 85

def reset!
  @concurrency&.reset!
  @threshold&.reset!
end

#throttled?(jid, *job_args) ⇒ Boolean

Returns whenever job is throttled or not.

Returns:

  • (Boolean)

    whenever job is throttled or not.



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/sidekiq/throttled/strategy.rb', line 61

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