class Net::HTTP::Persistent

def request uri, req = nil, &block

def request uri, req = nil, &block
  retried      = false
  bad_response = false
  req = request_setup req || uri
  connection = connection_for uri
  connection_id = connection.object_id
  begin
    Thread.current[@request_key][connection_id] += 1
    response = connection.request req, &block
    if connection_close?(req) or
       (response.http_version <= '1.0' and
        not connection_keep_alive?(response)) or
       connection_close?(response) then
      connection.finish
    end
  rescue Net::HTTPBadResponse => e
    message = error_message connection
    finish connection
    raise Error, "too many bad responses #{message}" if
      bad_response or not can_retry? req
    bad_response = true
    retry
  rescue *RETRIED_EXCEPTIONS => e # retried on ruby 2
    request_failed e, req, connection if
      retried or not can_retry? req, @retried_on_ruby_2
    reset connection
    retried = true
    retry
  rescue Errno::EINVAL, Errno::ETIMEDOUT => e # not retried on ruby 2
    request_failed e, req, connection if retried or not can_retry? req
    reset connection
    retried = true
    retry
  rescue Exception => e
    finish connection
    raise
  ensure
    Thread.current[@timeout_key][connection_id] = Time.now
  end
  @http_versions["#{uri.host}:#{uri.port}"] ||= response.http_version
  response
end