Sha256: 67aef2bbf0c5adb87d372b240eec839631598eb4cab54b2fc036d4bd7ecb27b8

Contents?: true

Size: 1.37 KB

Versions: 1

Compression:

Stored size: 1.37 KB

Contents

# frozen_string_literal: true

module Spandx
  module Core
    class Http
      attr_reader :driver, :retries

      def initialize(driver: Http.default_driver, retries: 3)
        @driver = driver
        @retries = retries
        @circuits = Hash.new { |hash, key| hash[key] = Circuit.new(key) }
      end

      def get(uri, default: nil, escape: true)
        return default if Spandx.airgap?

        circuit = circuit_for(uri)
        return default if circuit.open?

        circuit.attempt do
          driver.with_retry(retries: retries) do |client|
            client.get(escape ? Addressable::URI.escape(uri) : uri)
          end
        end
      rescue *Net::Hippie::CONNECTION_ERRORS
        default
      end

      def ok?(response)
        response.is_a?(Net::HTTPSuccess)
      end

      def self.default_driver
        @default_driver ||= Net::Hippie::Client.new.tap do |client|
          client.logger = Spandx.logger
          client.open_timeout = 1
          client.read_timeout = 5
          client.follow_redirects = 3
        end
      end

      private

      def circuit_breaker_for(host, default)
        return default unless @circuits[host]

        @circuits[host] = false
        result = yield
        @circuits[host] = true
        result
      end

      def circuit_for(url)
        uri = URI.parse(url.to_s)
        @circuits[uri.host]
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
spandx-0.12.3 lib/spandx/core/http.rb