diff --git a/providers/netty3/src/main/java/org/asynchttpclient/providers/netty3/channel/ChannelManager.java b/providers/netty3/src/main/java/org/asynchttpclient/providers/netty3/channel/ChannelManager.java index c24b90e1ae..ce3a702395 100644 --- a/providers/netty3/src/main/java/org/asynchttpclient/providers/netty3/channel/ChannelManager.java +++ b/providers/netty3/src/main/java/org/asynchttpclient/providers/netty3/channel/ChannelManager.java @@ -328,8 +328,12 @@ public void preemptChannel(String poolKey) throws IOException { throw poolAlreadyClosed; if (!tryAcquireGlobal()) throw tooManyConnections; - if (!tryAcquirePerHost(poolKey)) + if (!tryAcquirePerHost(poolKey)) { + if (maxTotalConnectionsEnabled) + freeChannels.release(); + throw tooManyConnectionsPerHost; + } } public void close() { diff --git a/providers/netty4/src/main/java/org/asynchttpclient/providers/netty4/channel/ChannelManager.java b/providers/netty4/src/main/java/org/asynchttpclient/providers/netty4/channel/ChannelManager.java index f0ddddbc0d..41e8cb2596 100755 --- a/providers/netty4/src/main/java/org/asynchttpclient/providers/netty4/channel/ChannelManager.java +++ b/providers/netty4/src/main/java/org/asynchttpclient/providers/netty4/channel/ChannelManager.java @@ -93,7 +93,7 @@ public class ChannelManager { private final IOException poolAlreadyClosed; private final ChannelPool channelPool; - private final boolean maxConnectionsEnabled; + private final boolean maxTotalConnectionsEnabled; private final Semaphore freeChannels; private final ChannelGroup openChannels; private final boolean maxConnectionsPerHostEnabled; @@ -119,10 +119,10 @@ public ChannelManager(AsyncHttpClientConfig config, NettyAsyncHttpProviderConfig tooManyConnections = buildStaticException(String.format("Too many connections %s", config.getMaxConnections())); tooManyConnectionsPerHost = buildStaticException(String.format("Too many connections per host %s", config.getMaxConnectionsPerHost())); poolAlreadyClosed = buildStaticException("Pool is already closed"); - maxConnectionsEnabled = config.getMaxConnections() > 0; + maxTotalConnectionsEnabled = config.getMaxConnections() > 0; maxConnectionsPerHostEnabled = config.getMaxConnectionsPerHost() > 0; - if (maxConnectionsEnabled) { + if (maxTotalConnectionsEnabled) { openChannels = new CleanupChannelGroup("asyncHttpClient") { @Override public boolean remove(Object o) { @@ -280,7 +280,7 @@ public boolean removeAll(Channel connection) { } private boolean tryAcquireGlobal() { - return !maxConnectionsEnabled || freeChannels.tryAcquire(); + return !maxTotalConnectionsEnabled || freeChannels.tryAcquire(); } private Semaphore getFreeConnectionsForHost(String poolKey) { @@ -304,8 +304,12 @@ public void preemptChannel(String poolKey) throws IOException { throw poolAlreadyClosed; if (!tryAcquireGlobal()) throw tooManyConnections; - if (!tryAcquirePerHost(poolKey)) + if (!tryAcquirePerHost(poolKey)) { + if (maxTotalConnectionsEnabled) + freeChannels.release(); + throw tooManyConnectionsPerHost; + } } public void close() { @@ -334,7 +338,7 @@ public void closeChannel(Channel channel) { } public void abortChannelPreemption(String poolKey) { - if (maxConnectionsEnabled) + if (maxTotalConnectionsEnabled) freeChannels.release(); if (maxConnectionsPerHostEnabled) getFreeConnectionsForHost(poolKey).release();