class Sidekiq::Throttler

Sidekiq server middleware. Throttles jobs when they exceed limits specified on the worker. Jobs that exceed the limit are requeued with a delay.

Constants

VERSION

Public Class Methods

new(options = {}) click to toggle source
# File lib/sidekiq/throttler.rb, line 16
def initialize(options = {})
  @options = options.dup
end

Public Instance Methods

call(worker, msg, queue) { || ... } click to toggle source

Passes the worker, arguments, and queue to {RateLimit} and either yields or requeues the job depending on whether the worker is throttled.

@param [Sidekiq::Worker] worker

The worker the job belongs to.

@param [Hash] msg

The job message.

@param [String] queue

The current queue.
# File lib/sidekiq/throttler.rb, line 32
def call(worker, msg, queue)
  rate_limit = RateLimit.new(worker, msg['args'], queue, @options)

  rate_limit.within_bounds do
    yield
  end

  rate_limit.exceeded do |delay|
    worker.class.perform_in(delay, *msg['args'])
  end

  rate_limit.execute
end