Ruby Version: 1.9.3
http://ruby-doc.org/stdlib-1.9.3/libdoc/net/http/rdoc/Net.html The documentation states that "If you wish to re-use a connection across multiple HTTP requests without automatically closing it you can use ::new instead of ::start"
http://ruby-doc.org/stdlib-1.9.3/libdoc/net/http/rdoc/Net/HTTP.html#method-c-new This documentation states that "Creates a new Net::HTTP object without opening a TCP connection or HTTP session. "
Scenario:
My aim is use re-use HTTP and TCP connections as much as possible. If i am using Net::HTTP.start as my HTTP object, and if there is a significant delay between two successive calls ( > 2 minutes), the first call after the delay is failing with EOFError : end of file reached.
So, I am planning to swap Net::HTTP.start with Net::HTTP.new
Questions:
If i use new instead of start, will new re-use connections? or will it try to create a new HTTP and TCP connection everytime a HTTP call is made?
Will there be a performance impact because of this? What is the best way to handle in scenarios like these, where we want to make HTTP calls in a high volume traffic?
I have not tested it myself, but if you read two sentences further in the docs, I think you'll get the answer to your questions.
If you wish to re-use a connection across multiple HTTP requests without automatically closing it you can use
::new
instead of::start
.request
will automatically open a connection to the server if one is not currently open. You can manually close the connection withfinish
.
So if you want to keep the connection alive between requests, you can do something like:
begin
http = Net::HTTP.new('example.com')
http.start
response1 = http.get('path1')
response2 = http.get('path2')
ensure
http.finish
end
(using the begin / ensure block so that the connection is guaranteed to close, even if an exception occurs since opening it).
The same can be achieved more easily using a block with the start
method:
Net::HTTP.start('example.com') do |http|
response1 = http.request_get('path1')
response2 = http.request_get('path2')
end
Yes, keep-alive connection will indeed be faster as there is no need to do the TCP handshakes needed to set up and close the TCP connection.
Also, take a look at this wonderful blog post about using the pipe-lining feature of HTTP1.1 which, if supported by the server, may greatly enhance performance. But I guess properly supporting pipe-lining at the server end is rather an advanced technique.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With