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) ⇒ Strategy

Returns a new instance of Strategy

Raises:

  • (ArgumentError)


28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/sidekiq/throttled/strategy.rb', line 28

def initialize(name, concurrency: nil, threshold: nil, key_suffix: nil)
  key = "throttled:#{name}"

  @concurrency =
    if concurrency
      concurrency[:key_suffix] ||= key_suffix
      Concurrency.new(key, **concurrency)
    end

  @threshold =
    if threshold
      threshold[:key_suffix] ||= key_suffix
      Threshold.new(key, **threshold)
    end

  return if @concurrency || @threshold

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

Instance Attribute Details

#concurrencyObject (readonly)

Returns the value of attribute concurrency



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

def concurrency
  @concurrency
end

#thresholdObject (readonly)

Returns the value of attribute threshold



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

def threshold
  @threshold
end

Instance Method Details

#dynamic?Boolean



49
50
51
52
53
54
# File 'lib/sidekiq/throttled/strategy.rb', line 49

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

  false
end

#finalize!(jid, *job_args) ⇒ void

This method returns an undefined value.

Marks job as being processed.



70
71
72
# File 'lib/sidekiq/throttled/strategy.rb', line 70

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

#reset!void

This method returns an undefined value.

Resets count of jobs of all avaliable strategies



76
77
78
79
# File 'lib/sidekiq/throttled/strategy.rb', line 76

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

#throttled?(jid, *job_args) ⇒ Boolean



57
58
59
60
61
62
63
64
65
66
# File 'lib/sidekiq/throttled/strategy.rb', line 57

def throttled?(jid, *job_args)
  return true if @concurrency && @concurrency.throttled?(jid, *job_args)

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

  false
end