Sha256: 8943c00dbd3fc0bbb68b33f7a89327142114b711d69424496318ccff920c0f27

Contents?: true

Size: 1.35 KB

Versions: 1

Compression:

Stored size: 1.35 KB

Contents

# frozen_string_literal: true

module SidekiqHerokuScaler
  module Strategy
    class Latency
      attr_reader :inc_count, :dec_count

      def initialize(min_dynos_count:, max_dynos_count:,
                     max_latency:, min_latency:,
                     inc_count: nil, dec_count: nil)
        @min_dynos_count = min_dynos_count
        @max_dynos_count = max_dynos_count
        @max_latency = max_latency
        @min_latency = min_latency
        @inc_count = (inc_count || 1).to_i
        @dec_count = (dec_count || 1).to_i
      end

      def increase?(sidekiq_worker)
        sidekiq_worker.quantity < max_dynos_count &&
          (sidekiq_worker.latency > max_latency ||
            (sidekiq_worker.quantity.zero? && sidekiq_worker.latency.positive?)) &&
          sidekiq_worker.queues_size > sidekiq_worker.quantity * sidekiq_worker.concurrency
      end

      def decrease?(sidekiq_worker)
        sidekiq_worker.quantity > min_dynos_count &&
          sidekiq_worker.latency < min_latency &&
          (sidekiq_worker.quantity > 1 || !sidekiq_worker.jobs_running?)
      end

      def safe_quantity(quantity)
        return min_dynos_count if quantity < min_dynos_count

        quantity > max_dynos_count ? max_dynos_count : quantity
      end

      private

      attr_reader :min_dynos_count, :max_dynos_count, :max_latency, :min_latency
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
sidekiq-heroku-scaler-0.3.4 lib/sidekiq-heroku-scaler/strategy/latency.rb