module Semian::NetHTTP

def connect

def connect
  with_cleared_dynamic_options do
    return super if disabled?
    acquire_semian_resource(adapter: :http, scope: :connection) { super }
  end
end

def disabled?

def disabled?
  raw_semian_options.nil? || @semian_enabled == false
end

def handle_error_responses(result)

def handle_error_responses(result)
  if raw_semian_options.fetch(:open_circuit_server_errors, false)
    semian_resource.mark_failed(result) if result.is_a?(::Net::HTTPServerError)
  end
  result
end

def raw_semian_options

def raw_semian_options
  @raw_semian_options ||= begin
    @raw_semian_options = Semian::NetHTTP.retrieve_semian_configuration(address, port)
    @raw_semian_options = @raw_semian_options.dup unless @raw_semian_options.nil?
  end
end

def reset_exceptions

def reset_exceptions
  self.exceptions = Semian::NetHTTP::DEFAULT_ERRORS.dup
end

def resource_exceptions

def resource_exceptions
  Semian::NetHTTP.exceptions
end

def retrieve_semian_configuration(host, port)

def retrieve_semian_configuration(host, port)
  @semian_configuration.call(host, port) if @semian_configuration.respond_to?(:call)
end

def semian_configuration=(configuration)

def semian_configuration=(configuration)
  raise Semian::NetHTTP::SemianConfigurationChangedError unless @semian_configuration.nil?
  @semian_configuration = configuration
end

def semian_identifier

def semian_identifier
  "nethttp_#{raw_semian_options[:name]}"
end

def transport_request(*)

def transport_request(*)
  with_cleared_dynamic_options do
    return super if disabled?
    acquire_semian_resource(adapter: :http, scope: :query) do
      handle_error_responses(super)
    end
  end
end

def with_cleared_dynamic_options

def with_cleared_dynamic_options
  unless @resource_acquisition_in_progress
    @resource_acquisition_in_progress = true
    resource_acquisition_started = true
  end
  yield
ensure
  if resource_acquisition_started
    if @raw_semian_options&.fetch(:dynamic, false)
      # Clear @raw_semian_options if the resource was flagged as dynamic.
      @raw_semian_options = nil
    end
    @resource_acquisition_in_progress = false
  end
end

def with_resource_timeout(timeout)

def with_resource_timeout(timeout)
  prev_read_timeout = read_timeout
  prev_open_timeout = open_timeout
  begin
    self.read_timeout = timeout
    self.open_timeout = timeout
    yield
  ensure
    self.read_timeout = prev_read_timeout
    self.open_timeout = prev_open_timeout
  end
end