Skip to content

Commit a6ae5c2

Browse files
committed
honor connection timeout.
1 parent 0b8aac5 commit a6ae5c2

File tree

1 file changed

+23
-4
lines changed

1 file changed

+23
-4
lines changed

src/main/java/com/ning/http/client/providers/grizzly/GrizzlyAsyncHttpProvider.java

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1960,6 +1960,15 @@ private void doAsyncTrackedConnection(final Request request,
19601960
throws IOException, ExecutionException, InterruptedException {
19611961
final String url = request.getUrl();
19621962
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+
}
19631972
if (c == null) {
19641973
if (!connectionMonitor.acquire()) {
19651974
throw new IOException("Max connections exceeded");
@@ -1995,7 +2004,7 @@ Connection obtainTrackedConnection(final Request request,
19952004

19962005
Connection obtainConnection(final Request request,
19972006
final GrizzlyResponseFuture requestFuture)
1998-
throws IOException, ExecutionException, InterruptedException {
2007+
throws IOException, ExecutionException, InterruptedException, TimeoutException {
19992008

20002009
final Connection c = (obtainConnection0(request.getUrl(),
20012010
request,
@@ -2026,7 +2035,7 @@ private void doAsyncConnect(final String url,
20262035
private Connection obtainConnection0(final String url,
20272036
final Request request,
20282037
final GrizzlyResponseFuture requestFuture)
2029-
throws IOException, ExecutionException, InterruptedException {
2038+
throws IOException, ExecutionException, InterruptedException, TimeoutException {
20302039

20312040
final URI uri = AsyncHttpProviderUtils.createUri(url);
20322041
ProxyServer proxy = getProxyServer(request);
@@ -2035,8 +2044,18 @@ private Connection obtainConnection0(final String url,
20352044
}
20362045
String host = ((proxy != null) ? proxy.getHost() : uri.getHost());
20372046
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+
}
20402059

20412060
}
20422061

0 commit comments

Comments
 (0)