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
Much higher connection number than needed #68
It looks lik e httpclient employs a LRU strategy in choosing which TCP connection to a HTTP server to re-use when there is a new HTTP request instead of using an MRU strategy.
Consider following scenario: The user of httpclient uses 5 requests per second, each request takes 2 seconds to complete. The server has a timeout of 60 seconds until the server closes the TCP connection. Consider furthermore that there was a recent usage spike and thus there are 500 TCP connections between httpclient and the HTTP server.
It would be expected that, over time, the number of open TCP connections drops from 500 to about 10 (because, on average, every 0.2 seconds, there will be 1 TCP connection becoming free to be reused, and every 0.2 seconds, there will be a new HTTP request to be processed, keeping about 10 TCP connections busy).
The number of open TCP connections drops from 500 to about 300.
The reason is that httpclient does not let any of the 300 TCP connections to time out, because it does not re-use the most recently used TCP connection first (resulting in many warm TCP connections being cooled down and finally timed out), but it reuses the least recently used TCP connection first (resulting in many warm TCP connections being kept warm and thus never giving them a chance to time out).
Use an MRU approach.
Thanks for detailed explanation. Am I understanding you correctly?
diff --git a/lib/httpclient/session.rb b/lib/httpclient/session.rb index 306d2dc..dae4cc7 100644 --- a/lib/httpclient/session.rb +++ b/lib/httpclient/session.rb @@ -251,7 +251,7 @@ class HTTPClient def add_cached_session(sess) @sess_pool_mutex.synchronize do - @sess_pool << sess + @sess_pool.unshift(sess) end end end
I think it's server-friendly and I should commit this.
Do you think we should add a leaper thread for collecting cold (long-living but not used) sessions? I think MRU would cause many cold sessions which is not collected forever... (Related to #56)