Sha256: e1e93fee1a2c3d9ef0ba756e176120d0eafd748c4eef899adec3e06eda8cb85e

Contents?: true

Size: 1.81 KB

Versions: 5

Compression:

Stored size: 1.81 KB

Contents

module Ecoportal
  module API
    module Common
      class Client
        module TimeOut
          TIMEOUT_MARGIN = 0.15 # adaptative timeout with margin
          MIN_SIZE       = 5    # requests

          protected

          attr_writer :throughput

          # Keeps track of the current processing speed
          # @note it needs to be called in specific spots
          def throughput!(secs = nil, count: nil)
            throughput.record!(secs, count: count)
          end

          def throughput
            @throughput ||= Throughput.new
          end

          def to_count(value)
            value ||= 0
            return min_size if value.positive? && value < min_size
            return value    if value.positive?

            1
          end

          private

          def eta_for(count, approach: :conservative)
            kargs = {}.tap do |params|
              next params.merge!({ratio: rectified_throughput}) if approach == :conservative

              params.merge!({approach: approach})
            end

            throughput.eta_for(to_count(count), **kargs)
          end

          def timeout_for(count, waited: 0, max_wait: nil, approach: :conservative)
            (waited + eta_for(count, approach: approach)).then do |time|
              next time unless max_wait

              [max_wait, time].min
            end
          end

          def rectified_throughput
            throughput.ratio * (1 - timeout_margin)
          end

          def around_min_throughput?
            throughput.around_min_throughput?(timeout_margin)
          end

          def timeout_margin
            self.class::TIMEOUT_MARGIN
          end

          def min_size
            self.class::MIN_SIZE
          end
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
ecoportal-api-0.10.8 lib/ecoportal/api/common/client/time_out.rb
ecoportal-api-0.10.7 lib/ecoportal/api/common/client/time_out.rb
ecoportal-api-0.10.6 lib/ecoportal/api/common/client/time_out.rb
ecoportal-api-0.10.5 lib/ecoportal/api/common/client/time_out.rb
ecoportal-api-0.10.4 lib/ecoportal/api/common/client/time_out.rb