LewlSauce
LewlSauce

Reputation: 5872

Cannot get timeout to work properly with net http in Ruby

I am having issues with the timeout of my HTTP Get requests and have tried several things to get it working with no success. Here's my get_request function:

require 'net/http'
require 'openssl'

def get_request(url, headers = "")
  uri = URI.parse(url)
  request = Net::HTTP::Get.new(uri)
  unless headers.empty?
    headers.each { |key, value| request[key] = value }
  end
  if uri.port == 443
    if @proxy
      response = Net::HTTP.start(uri.host, uri.port, @proxy_ip, @proxy_port, :use_ssl => true, :verify_mode => OpenSSL::SSL::VERIFY_NONE) { |http|
        http.request(request)
      }
    else
      response = Net::HTTP.start(uri.host, uri.port, :use_ssl => true, :verify_mode => OpenSSL::SSL::VERIFY_NONE) { |http|
        http.request(request)
      }
    end
  else
    response = Net::HTTP.start(uri.host, uri.port) { |http|
      http.request(request)
    }
  end

  if response.code.to_i == 302 or response.code.to_i == 301
    get_request(response["Location"])
  else
    return response
  end

  return response
end

I have tried inserting:

http.read_timeout = 3

right before each of these lines:

http.request(request)

but this does not seem to work. The script still hangs for a good 20+ seconds.

I have also tried to insert:

request.read_timeout = 3

right below:

request = Net::HTTP::Get.new(uri)

but this still does not work. I just simply want the script to stop the GET request after 3 seconds, but after placing a puts "Loading get request" line after the method starts, I notice that the request still takes a solid 10+ seconds.

This suggestion has been posted in several areas, but it just doesn't seem to be working for me.

Upvotes: 1

Views: 279

Answers (1)

peter
peter

Reputation: 42192

I use open-uri fot that reason, never had a problem with that. The method below starts a procedure on a Tomcat server. The response happens to take some time depending on the load on the server. Before it failed sometimes.

   def start_sending(url)
    require 'open-uri'
    open(url, :read_timeout => 5 * 60) do |response|
      # if there is a response it always contains "Ok" for this url
      if response.read[/Return: Ok/i] 
        log "sending ok"
        return true
      else
        log "error sending, no confirmation received\n#{response.read}"
        return false
      end
    end
  rescue => e 
    log_error("#{e.message}\n#{e.backtrace}")
  end

Upvotes: 1

Related Questions