Module: Sidekiq::Throttled

Defined in:
lib/sidekiq/throttled.rb,
lib/sidekiq/throttled/web.rb,
lib/sidekiq/throttled/errors.rb,
lib/sidekiq/throttled/worker.rb,
lib/sidekiq/throttled/version.rb,
lib/sidekiq/throttled/registry.rb,
lib/sidekiq/throttled/strategy.rb,
lib/sidekiq/throttled/web/stats.rb,
lib/sidekiq/throttled/middleware.rb,
lib/sidekiq/throttled/basic_fetch.rb,
lib/sidekiq/throttled/strategy/script.rb,
lib/sidekiq/throttled/strategy/threshold.rb,
lib/sidekiq/throttled/strategy/concurrency.rb

Overview

Concurrency and threshold throttling for Sidekiq.

Just add somewhere in your bootstrap:

require "sidekiq/throttled"
Sidekiq::Throttled.setup!

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

class MyWorker
  include Sidekiq::Worker
  include Sidekiq::Throttled::Worker

  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: Registry, Worker Classes: BasicFetch, Error, Strategy

Constant Summary collapse

VERSION =

Gem version

"0.2.0".freeze

Class Method Summary collapse

Class Method Details

.setup!void

This method returns an undefined value.

Hooks throttler into sidekiq.



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/sidekiq/throttled.rb', line 43

def setup!
  Sidekiq.configure_server do |config|
    require "sidekiq/throttled/basic_fetch"
    Sidekiq.options[:fetch] = Sidekiq::Throttled::BasicFetch

    require "sidekiq/throttled/middleware"
    config.server_middleware do |chain|
      chain.add Sidekiq::Throttled::Middleware
    end
  end
end

.throttled?(message) ⇒ TrueClass, FalseClass

Parameters:

  • message (String)

    JSON payload of job

Returns:

  • (TrueClass)

    if job is not allowed to be processed now

  • (FalseClass)

    otherwise



58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/sidekiq/throttled.rb', line 58

def throttled?(message)
  message = JSON.parse message
  job = message.fetch("class".freeze) { return false }
  jid = message.fetch("jid".freeze) { return false }

  Registry.get job do |strategy|
    return strategy.throttled?(jid, *message["args"])
  end

  false
rescue
  false
end