Module: Sidekiq::Throttled

Defined in:
lib/sidekiq/throttled.rb,
lib/sidekiq/throttled/job.rb,
lib/sidekiq/throttled/web.rb,
lib/sidekiq/throttled/config.rb,
lib/sidekiq/throttled/errors.rb,
lib/sidekiq/throttled/worker.rb,
lib/sidekiq/throttled/message.rb,
lib/sidekiq/throttled/version.rb,
lib/sidekiq/throttled/cooldown.rb,
lib/sidekiq/throttled/registry.rb,
lib/sidekiq/throttled/strategy.rb,
lib/sidekiq/throttled/web/stats.rb,
lib/sidekiq/throttled/expirable_set.rb,
lib/sidekiq/throttled/strategy/base.rb,
lib/sidekiq/throttled/middlewares/server.rb,
lib/sidekiq/throttled/strategy/threshold.rb,
lib/sidekiq/throttled/patches/basic_fetch.rb,
lib/sidekiq/throttled/patches/super_fetch.rb,
lib/sidekiq/throttled/strategy_collection.rb,
lib/sidekiq/throttled/strategy/concurrency.rb,
lib/sidekiq/throttled/patches/throttled_retriever.rb

Overview

Concurrency and threshold throttling for Sidekiq.

Just add somewhere in your bootstrap:

require "sidekiq/throttled"

Once you’ve done that you can include Job to your job classes and configure throttling:

class MyJob
  include Sidekiq::Job
  include Sidekiq::Throttled::Job

  sidekiq_options :queue => :my_queue

  sidekiq_throttle({
    # Allow maximum 10 concurrent jobs of this class at a time.
    :concurrency => { :limit => 10 },
    # Allow maximum 1K jobs being processed within one hour window.
    :threshold => { :limit => 1_000, :period => 1.hour }
  })

  def perform
    # ...
  end
end

Defined Under Namespace

Modules: Job, Middlewares, Patches, Registry, Web Classes: Config, Cooldown, Error, ExpirableSet, Message, Strategy, StrategyCollection

Constant Summary collapse

Worker =

A new module, Sidekiq::Job, was added in Sidekiq version 6.3.0 as a simple alias for Sidekiq::Worker as the term “worker” was considered too generic and confusing. Many people call a Sidekiq process a “worker” whereas others call the thread that executes jobs a “worker”.

Job
VERSION =

Gem version

"1.4.0"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.cooldownCooldown? (readonly)

Returns:



55
56
57
# File 'lib/sidekiq/throttled.rb', line 55

def cooldown
  @cooldown
end

Class Method Details

.configure {|config| ... } ⇒ Object

Examples:

Sidekiq::Throttled.configure do |config|
  config.cooldown_period = nil # Disable queues cooldown manager
end

Yield Parameters:



63
64
65
66
67
68
69
70
71
72
# File 'lib/sidekiq/throttled.rb', line 63

def configure
  MUTEX.synchronize do
    config = @config.dup

    yield config

    @config   = config.freeze
    @cooldown = Cooldown[@config]
  end
end

.setup!Object

Deprecated.

Will be removed in 2.0.0



92
93
94
95
96
97
98
99
100
101
# File 'lib/sidekiq/throttled.rb', line 92

def setup!
  warn "Sidekiq::Throttled.setup! was deprecated"

  Sidekiq.configure_server do |config|
    config.server_middleware do |chain|
      chain.remove(Sidekiq::Throttled::Middlewares::Server)
      chain.add(Sidekiq::Throttled::Middlewares::Server)
    end
  end
end

.throttled?(message) ⇒ Boolean

Tells whenever job is throttled or not.

Parameters:

  • message (String)

    Job’s JSON payload

Returns:

  • (Boolean)


78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/sidekiq/throttled.rb', line 78

def throttled?(message)
  message = Message.new(message)
  return false unless message.job_class && message.job_id

  Registry.get(message.job_class) do |strategy|
    return strategy.throttled?(message.job_id, *message.job_args)
  end

  false
rescue StandardError
  false
end