commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Adrian Sutton <>
Subject RE: [httpclient] MultiThreadedHttpConnectionManager : connections available
Date Thu, 10 Apr 2003 23:49:32 GMT
Hi Benoit,
I'm not sure I understand the problem then, so let me just give you a bunch
of information and see if we can sort it out. :)

MultiThreadedHttpConnectionManager (the manager from here in) currently
works on the basis that there is a maximum number of connections per host
but no total maximum limit on the number of connections.  So if you have 1
host, the maximum number of open connections will be 1 *
getMaxConnectionsPerHost(), if you have 25 hosts you connect to the maximum
number of open connections will be 25 * getMaxConnectionsPerHost().  Every
time you connect to a new host an extra "getMaxConnectionsPerHost()"
connections may be opened.

Now, the limit in here is per host, so by default (because the Http spec
says to) there is a maximum of 2 connections per host.  So if I were to try
the following sequence of connects:

1. connect
2. connect
3. connect
4. connect
5. connect
6. connect

The connections: 1, 2, 3, 5 would not have to wait for a connection since
when they occur there are less than 2 connections to the host. However,
connection 4 would wait for either connection 1 or 3 to finish before it
could start because there are already 2 connections to

So from this we see that deciding whether or not retrieving a connection
will block comes down to finding out if the number of connections in use is
smaller than the maximum number of connections allowed per host, ie:
getMaxConnectionsPerHost() - getConnectionsInUse(HostConfiguration)

Now, the problem that Mike is working on is that if you connect once to a
large number of hosts there is no limit to the number of connections you can
make and eventually the OS gets swamped or you run out of RAM because of all
the stored idle connections.  To address this, Mike has added a maximum
number of total connections limit as well as the per host connection limit,
so with the patch that is about to be committed, you'd need to use:

Math.min(getMaxTotalConnections() - getTotalConnectionsInUse(),
getMaxConnectionsPerHost() - getConnectionsInUse(HostConfig));

which is more complex but still not too bad.

As for a non-blocking getConnection, there sort of is one, just pass in a
timeout value that is extremely low.  Something like:

getConnection(HostConfig, 1);

if a connection is available it will be returned immediately, if not the
thread will only block for 1 millisecond before throwing an exception.
However, I *strongly* recommend against this approach.  It is not the way
HttpClient was designed to be used and you will have to spend time making
sure you honor the contracts of usage that HttpClient applies (and they
aren't particularly well document since you're not supposed to mess with
them, they also change from time to time).  It's really just more trouble
than it's worth when you can tell whether or not a connection is available

Hope that helps,

Adrian Sutton, Software Engineer
Ephox Corporation

-----Original Message-----
From: Benoit Mathieu []
Sent: Thursday, 10 April 2003 5:01 PM
To: Jakarta Commons Users List
Subject: Re: [httpclient] MultiThreadedHttpConnectionManager :
connections available

Hi Adrian,

These methods exists in alpha2, and I tried your proposition. But it seems 
that getConnectionsInUse returns the connections created for the host and
the connections available. I think it is the same bug as the one Mike Becke 
is fixing ...

Do you think it is possible to add a non blocking version of 
getConnection(HostConfiguration) in MultiThreadedHttpConnectionManager ? It 
would return a connection if one exists or can be created, or return null if

no connections are available ...



View raw message