Sha256: 028d17b0600bb8564f6e98d21da0bd8c8aec46af3bc7c10af3a87580cdfd9a99

Contents?: true

Size: 1.53 KB

Versions: 2

Compression:

Stored size: 1.53 KB

Contents

# frozen_string_literal: true

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

      # rubocop:disable Metrics/ParameterLists
      def initialize(min_dynos_count:, max_dynos_count:,
                     max_latency:, min_latency:,
                     inc_count: nil, dec_count: nil,
                     smart_decrease: false)
        @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
        @smart_decrease = smart_decrease
      end
      # rubocop:enable Metrics/ParameterLists

      def increase?(sidekiq_worker)
        sidekiq_worker.quantity < max_dynos_count &&
          (sidekiq_worker.latency > max_latency ||
            (sidekiq_worker.quantity.zero? && sidekiq_worker.latency.positive?))
      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].min
      end

      private

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

Version data entries

2 entries across 2 versions & 1 rubygems

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