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
# File 'lib/sidekiq/throttled/strategy.rb', line 25

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

  if concurrency
    @concurrency = Concurrency.new(
      key, concurrency.merge(:key_suffix => key_suffix)
    )
  end
  if threshold
    @threshold = Threshold.new(
      key, threshold.merge(:key_suffix => key_suffix)
    )
  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_keys?Boolean

Returns:

  • (Boolean)


44
45
46
47
# File 'lib/sidekiq/throttled/strategy.rb', line 44

def dynamic_keys?
  (@concurrency && @concurrency.dynamic_keys?) ||
    (@threshold && @threshold.dynamic_keys?)
end

#finalize!(jid, *job_args) ⇒ void

This method returns an undefined value.

Marks job as being processed.



63
64
65
# File 'lib/sidekiq/throttled/strategy.rb', line 63

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



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

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.



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

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