Reputation:
I've got this code (roughly):
parsed_url = URI.parse(url_string)
puts "before Net::HTTP.start block"
response = Net::HTTP.start(parsed_url.host, parsed_url.port) { |http|
puts "inside Net::HTTP.start block"
http.read_timeout = 10
http.get(parsed_url.path)
}
When I execute the code against a url that timesout in the browser, it never gets into the block. Any other situation it works perfectly. Is there some behavior on the start method that I don't see? I was assuming that the connection wouldn't be opened at all until the http.get(parsed_url.path) statement so I'm confused why this code more doesn't execute...
thanks in advance...
Upvotes: 1
Views: 2165
Reputation: 3687
The documentation for the Net::HTTP class reads
Opens TCP connection and HTTP session.
So when you call Net::HTTP.start
, the connection is made and if you cannot access the given URL, then a Timeout::Error
is thrown inside the start
method and the code inside your block is indeed never executed.
Upvotes: 1
Reputation: 66867
Net:HTTP.start
calls do_start
which then calls connect
(both of them are private methods):
http://apidock.com/ruby/Net/HTTP/connect
The first 3 lines are
D "opening connection to #{conn_address()}..."
s = timeout(@open_timeout) { TCPSocket.open(conn_address(), conn_port()) }
D "opened"
and while I can't seem to find docs for the timeout
method, this could be your culprit.
Upvotes: 1