Sha256: 6d6dcc92f347b153ac4f95c84f5bc5d021043309252ffd2627fdfbaf26943710

Contents?: true

Size: 1.74 KB

Versions: 1

Compression:

Stored size: 1.74 KB

Contents

# internal
require "sidekiq/throttled/errors"
require "sidekiq/throttled/strategy/concurrency"
require "sidekiq/throttled/strategy/threshold"

module Sidekiq
  module Throttled
    # Meta-strategy that couples {Concurrency} and {Threshold} strategies.
    class Strategy
      # @!attribute [r] concurrency
      #   @return [Strategy::Concurrency, nil]
      attr_reader :concurrency

      # @!attribute [r] threshold
      #   @return [Strategy::Threshold, nil]
      attr_reader :threshold

      # @param [#to_s] key
      # @param [Hash] concurrency Concurrency options.
      #   See {Strategy::Concurrency#initialize} for details.
      # @param [Hash] threshold Threshold options.
      #   See {Strategy::Threshold#initialize} for details.
      def initialize(key, concurrency: nil, threshold: nil)
        base_key      = "throttled:#{key}"

        @concurrency  = concurrency && Concurrency.new(base_key, concurrency)
        @threshold    = threshold && Threshold.new(base_key, threshold)

        return if @concurrency || @threshold

        fail ArgumentError, "Neither :concurrency nor :threshold given"
      end

      # @return [Boolean] whenever job is throttled or not.
      def throttled?(jid)
        return true if @concurrency && @concurrency.throttled?(jid)

        if @threshold && @threshold.throttled?
          finalize! jid
          return true
        end

        false
      end

      # Marks job as being processed.
      # @return [void]
      def finalize!(jid)
        @concurrency && @concurrency.finalize!(jid)
      end

      # Resets count of jobs of all avaliable strategies
      # @return [void]
      def reset!
        @concurrency && @concurrency.reset!
        @threshold && @threshold.reset!
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
sidekiq-throttled-0.1.0 lib/sidekiq/throttled/strategy.rb