Sha256: 58b4983da141e71a869eb74c4da79b5f1c4aef7a889aa1bff83032cef8f8c30d

Contents?: true

Size: 1.49 KB

Versions: 3

Compression:

Stored size: 1.49 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
        semaphore = Mutex.new
        @circuits = Hash.new do |hash, key|
          semaphore.synchronize do
            hash[key] = Circuit.new(key)
          end
        end
      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, URI::InvalidURIError
        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

3 entries across 3 versions & 1 rubygems

Version Path
spandx-0.13.5 lib/spandx/core/http.rb
spandx-0.13.4 lib/spandx/core/http.rb
spandx-0.13.3 lib/spandx/core/http.rb