Bug #14349
closedFix Net::HTTP documentation around connection reuse
Description
From Net::HTTP's docs:
::start immediately creates a connection to an HTTP server which is kept open for the duration of the block. The connection will remain open for multiple requests in the block if the server indicates it supports persistent connections.
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 with finish.
According to the above, I'd expect the following scripts to reuse the underlying HTTP connection:
Net::HTTP.start('httpbin.org', port=443, use_ssl: true) { |http|
10.times { http.get("/") }
}
http = Net::HTTP.new('httpbin.org', 443)
http.use_ssl = true
10.times { http.get("/") }
http.finish
The first one does indeed reuse connections, but the second doesn't. From a debug script (attached):
------------
Using #start
------------
Connection Alive? true
Connection Alive? true
Connection Alive? true
Connection Alive? true
Connection Alive? true
Connection Alive? true
Connection Alive? true
Connection Alive? true
Connection Alive? true
Connection Alive? true
Time taken: 4.11464
----------
Using #new
----------
Connection Alive? false
Connection Alive? false
Connection Alive? false
Connection Alive? false
Connection Alive? false
Connection Alive? false
Connection Alive? false
Connection Alive? false
Connection Alive? false
Connection Alive? false
Time taken: 12.337512
This happens because when start
is called without a block and a connection doesn't exist, it proxies to a call with a block. And when a block is passed to start
, it automatically calls finish
at the end.
I've attached a patch that I think solves the issue. With the patch, sockets will still get closed after single requests through methods like Net::HTTP.get
, since they use the block form for start
.
Command to run the new test that was added: make test-all TESTS='net/http/test_http.rb -n test_keep_alive_using_new'
Files