Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.Sign up
best practices for closing connections? #56
So HTTPClient, unlike net::http, will leave a connection open until you specifically close it. I believe I've read some people saying that indeed leaving it open for multiple requests gets you much better performance.
But I'm not sure how to handle making sure it gets closed, or how important it is to make sure it gets closed.
In my project, it is convenient for me to have an internal API for making http requests, that uses HTTPClient under the hood. It is convenient for my own implementation to just lazily create an HTTPClient the first time a connection is needed, and then re-use that same HTTPClient instance on subsequent calls. But the problem there is that my architecture at present doesn't know for sure when it's done, or when to close it.
How big a problem would it be, for my local server, or for the remote servers I'm connecting to (some of which are really my servers too, but I don't want to be rude to third parties either) -- if I never close the connection? Eventually (unpredictable when) the HTTPClient will get GC'd by ruby -- I'm not sure if the sockets will be closed even then. How bad is it to both servers involved to never close the socket?
How do people handle this in their actual use cases? My app is actually a rails app, although a weird one. I guess I could have like an after_filter or something that calls my_http_client.reset_all . My actual app's weirdness involving some thread concurrency makes this harder, but I'm curious how people handle it in the 'standard' situation too.
Alternately, I could just NOT leave the connections open, I could just use one connection per request, like net::http, even though HttpClient is more powerful than that, I still could choose to use it that way, and then I wouldn't need to worry about closing it later. How bad an idea might that be, will performance actually suffer, or it will it not matter much?
Appreciate any advice, or stories of how people handle this in their actual situations.
added a commit
Oct 13, 2011
This ticket is 3.5 years old already... I think @jrochkind should not be interested in already... My bad, too late response.
I'm closing this with following comment.
In current HTTPClient implementation, Keep-Alive connections are closed by;
So the best way to use I'm thinking is;