Sha256: b390f8cfafb0400ffaa379d04b82cb9cbc2620a696cd544741c7db97575af247

Contents?: true

Size: 1.46 KB

Versions: 1

Compression:

Stored size: 1.46 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?) &&
          (!sidekiq_worker.jobs_running? || safe_quantity(sidekiq_worker.quantity - dec_count).positive?)
      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.5 lib/sidekiq-heroku-scaler/strategy/latency.rb