Skip to content

Commit

Permalink
Set the max number of connection per host and total to -1 by default
Browse files Browse the repository at this point in the history
  • Loading branch information
jfarcand committed Jul 27, 2010
1 parent 32d757b commit 3006d89
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 19 deletions.
4 changes: 2 additions & 2 deletions src/main/java/com/ning/http/client/AsyncHttpClientConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -232,8 +232,8 @@ public SSLEngine getSSLEngine(){
* Builder for an {@link AsyncHttpClient}
*/
public static class Builder {
private int defaultMaxTotalConnections = Integer.getInteger(ASYNC_CLIENT + "defaultMaxTotalConnections", 2000);
private int defaultMaxConnectionPerHost = Integer.getInteger(ASYNC_CLIENT + "defaultMaxConnectionsPerHost", 2000);
private int defaultMaxTotalConnections = Integer.getInteger(ASYNC_CLIENT + "defaultMaxTotalConnections", -1);
private int defaultMaxConnectionPerHost = Integer.getInteger(ASYNC_CLIENT + "defaultMaxConnectionsPerHost", -1);
private int defaultConnectionTimeOutInMs = Integer.getInteger(ASYNC_CLIENT + "defaultConnectionTimeoutInMS", 60 * 1000);
private int defaultIdleConnectionTimeoutInMs = Integer.getInteger(ASYNC_CLIENT + "defaultIdleConnectionTimeoutInMS", 60 * 1000);
private int defaultRequestTimeoutInMs = Integer.getInteger(ASYNC_CLIENT + "defaultRequestTimeoutInMS", 60 * 1000);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,6 @@ private <T> Future<T> doConnect(final Request request, final AsyncHandler<T> asy
throw new IOException("Closed");
}


if (config.getMaxTotalConnections() != -1 && activeConnectionsCount.getAndIncrement() >= config.getMaxTotalConnections()) {
activeConnectionsCount.decrementAndGet();
throw new IOException("Too many connections");
Expand Down Expand Up @@ -494,7 +493,9 @@ private <T> Future<T> doConnect(final Request request, final AsyncHandler<T> asy
}
bootstrap.setOption("connectTimeout", config.getConnectionTimeoutInMs());
} catch (Throwable t){
activeConnectionsCount.decrementAndGet();
if (config.getMaxTotalConnections() != -1) {
activeConnectionsCount.decrementAndGet();
}
log.error(t);
c.future().abort(t.getCause());
return c.future();
Expand All @@ -510,9 +511,11 @@ protected void channelIdle(ChannelHandlerContext ctx, IdleState state, long last
closeChannel(ctx);

for (Entry<String,Channel> e: connectionsPool.entrySet()) {
if (e.getValue().equals(ctx.getChannel())) {
if (e.getValue().equals(ctx.getChannel())) {
connectionsPool.remove(e.getKey());
activeConnectionsCount.decrementAndGet();
if (config.getMaxTotalConnections() != -1) {
activeConnectionsCount.decrementAndGet();
}
break;
}
}
Expand Down Expand Up @@ -639,22 +642,25 @@ public void channelClosed(ChannelHandlerContext ctx, ChannelStateEvent e) throws
}

private void markAsDoneAndCacheConnection(final NettyResponseFuture<?> future, final Channel channel) throws MalformedURLException {
if (future.getKeepAlive() ){
AtomicInteger connectionPerHost = connectionsPerHost.get(getBaseUrl(future.getURI()));
if (connectionPerHost == null) {
connectionPerHost = new AtomicInteger(1);
connectionsPerHost.put(getBaseUrl(future.getURI()),connectionPerHost);
}

if (connectionPerHost.getAndIncrement() < config.getMaxConnectionPerHost()) {
connectionsPool.put(getBaseUrl(future.getURI()), channel);
} else {
connectionPerHost.decrementAndGet();
log.warn("Maximum connections per hosts reached " + config.getMaxConnectionPerHost());
if (future.getKeepAlive()){
if (config.getMaxConnectionPerHost() != -1) {
AtomicInteger connectionPerHost = connectionsPerHost.get(getBaseUrl(future.getURI()));
if (connectionPerHost == null) {
connectionPerHost = new AtomicInteger(1);
connectionsPerHost.put(getBaseUrl(future.getURI()),connectionPerHost);
}

if (connectionPerHost.getAndIncrement() < config.getMaxConnectionPerHost()) {
connectionsPool.put(getBaseUrl(future.getURI()), channel);
} else {
connectionPerHost.decrementAndGet();
log.warn("Maximum connections per hosts reached " + config.getMaxConnectionPerHost());
}
}
} else {
} else if (config.getMaxTotalConnections() != -1) {
activeConnectionsCount.decrementAndGet();
}

future.done();
}

Expand Down

0 comments on commit 3006d89

Please sign in to comment.