Class: Sidekiq::Throttler

Inherits:
Object
  • Object
show all
Defined in:
lib/sidekiq/throttler.rb,
lib/sidekiq/throttler/version.rb,
lib/sidekiq/throttler/rate_limit.rb,
lib/sidekiq/throttler/storage/redis.rb,
lib/sidekiq/throttler/storage/memory.rb

Overview

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

Defined Under Namespace

Modules: Storage Classes: RateLimit

Constant Summary collapse

VERSION =
'0.4.1'

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Throttler

Returns a new instance of Throttler


16
17
18
# File 'lib/sidekiq/throttler.rb', line 16

def initialize(options = {})
  @options = options.dup
end

Instance Method Details

#call(worker, msg, queue) ⇒ Object

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

Parameters:

  • worker (Sidekiq::Worker)

    The worker the job belongs to.

  • msg (Hash)

    The job message.

  • queue (String)

    The current queue.


32
33
34
35
36
37
38
39
40
41
42
43
44
# 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