Class: Sidekiq::Throttled::Strategy

Inherits:
Object
  • Object
show all
Defined in:
lib/sidekiq/throttled/strategy.rb,
lib/sidekiq/throttled/strategy/script.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

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.

Parameters:

Raises:

  • (ArgumentError)


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

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.



13
14
15
# File 'lib/sidekiq/throttled/strategy.rb', line 13

def concurrency
  @concurrency
end

#thresholdObject (readonly)

Returns the value of attribute threshold.



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

def threshold
  @threshold
end

Instance Method Details

#dynamic?Boolean

Returns whenever strategy has dynamic config.

Returns:

  • (Boolean)

    whenever strategy has dynamic config



46
47
48
49
50
51
# File 'lib/sidekiq/throttled/strategy.rb', line 46

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.



67
68
69
# File 'lib/sidekiq/throttled/strategy.rb', line 67

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



73
74
75
76
# File 'lib/sidekiq/throttled/strategy.rb', line 73

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

#throttled?(jid, *job_args) ⇒ Boolean

Returns whenever job is throttled or not.

Returns:

  • (Boolean)

    whenever job is throttled or not.



54
55
56
57
58
59
60
61
62
63
# File 'lib/sidekiq/throttled/strategy.rb', line 54

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