@@ -1960,6 +1960,15 @@ private void doAsyncTrackedConnection(final Request request,
1960
1960
throws IOException , ExecutionException , InterruptedException {
1961
1961
final String url = request .getUrl ();
1962
1962
Connection c = pool .poll (AsyncHttpProviderUtils .getBaseUrl (url ));
1963
+ if (c != null && !c .isOpen ()) {
1964
+ System .out .println ("STALE CONNECTION" );
1965
+ try {
1966
+ c .close ();
1967
+ } catch (IOException ignored ) {
1968
+ } finally {
1969
+ c = null ;
1970
+ }
1971
+ }
1963
1972
if (c == null ) {
1964
1973
if (!connectionMonitor .acquire ()) {
1965
1974
throw new IOException ("Max connections exceeded" );
@@ -1995,7 +2004,7 @@ Connection obtainTrackedConnection(final Request request,
1995
2004
1996
2005
Connection obtainConnection (final Request request ,
1997
2006
final GrizzlyResponseFuture requestFuture )
1998
- throws IOException , ExecutionException , InterruptedException {
2007
+ throws IOException , ExecutionException , InterruptedException , TimeoutException {
1999
2008
2000
2009
final Connection c = (obtainConnection0 (request .getUrl (),
2001
2010
request ,
@@ -2026,7 +2035,7 @@ private void doAsyncConnect(final String url,
2026
2035
private Connection obtainConnection0 (final String url ,
2027
2036
final Request request ,
2028
2037
final GrizzlyResponseFuture requestFuture )
2029
- throws IOException , ExecutionException , InterruptedException {
2038
+ throws IOException , ExecutionException , InterruptedException , TimeoutException {
2030
2039
2031
2040
final URI uri = AsyncHttpProviderUtils .createUri (url );
2032
2041
ProxyServer proxy = getProxyServer (request );
@@ -2035,8 +2044,18 @@ private Connection obtainConnection0(final String url,
2035
2044
}
2036
2045
String host = ((proxy != null ) ? proxy .getHost () : uri .getHost ());
2037
2046
int port = ((proxy != null ) ? proxy .getPort () : uri .getPort ());
2038
- return connectionHandler .connect (new InetSocketAddress (host , getPort (uri , port )),
2039
- createConnectionCompletionHandler (request , requestFuture , null )).get ();
2047
+ int cTimeout = provider .clientConfig .getConnectionTimeoutInMs ();
2048
+ if (cTimeout > 0 ) {
2049
+ return connectionHandler .connect (new InetSocketAddress (host , getPort (uri , port )),
2050
+ createConnectionCompletionHandler (request ,
2051
+ requestFuture ,
2052
+ null )).get (cTimeout , TimeUnit .MILLISECONDS );
2053
+ } else {
2054
+ return connectionHandler .connect (new InetSocketAddress (host , getPort (uri , port )),
2055
+ createConnectionCompletionHandler (request ,
2056
+ requestFuture ,
2057
+ null )).get ();
2058
+ }
2040
2059
2041
2060
}
2042
2061
0 commit comments