Sha256: ddca2778137542ec657aeb5b45db2f25fc567c94555a4175637810e9a00f7077

Contents?: true

Size: 1.17 KB

Versions: 4

Compression:

Stored size: 1.17 KB

Contents

require 'sidekiq'
require 'active_support/core_ext/numeric/time'
require 'singleton'

require 'sidekiq/throttler/version'
require 'sidekiq/throttler/rate_limit'

require 'sidekiq/throttler/storage/memory'
require 'sidekiq/throttler/storage/redis'

module Sidekiq
  ##
  # Sidekiq server middleware. Throttles jobs when they exceed limits specified
  # on the worker. Jobs that exceed the limit are requeued with a delay.
  class Throttler
    def initialize(options = {})
      @options = options.dup
    end

    ##
    # 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.
    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

  end # Throttler
end # Sidekiq

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
sidekiq-throttler-0.5.1 lib/sidekiq/throttler.rb
sidekiq-throttler-0.4.1 lib/sidekiq/throttler.rb
sidekiq-throttler-0.4.0 lib/sidekiq/throttler.rb
sidekiq-throttler-0.3.1 lib/sidekiq/throttler.rb