Skip to content

Commit 05ec1af

Browse files
author
Stephane Landelle
committed
ChannelManager.preemptChannel should release global semaphore if tryAcquirePerHost fails, close AsyncHttpClient#805
1 parent 04df4e2 commit 05ec1af

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

providers/netty3/src/main/java/org/asynchttpclient/providers/netty3/channel/ChannelManager.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,8 +328,12 @@ public void preemptChannel(String poolKey) throws IOException {
328328
throw poolAlreadyClosed;
329329
if (!tryAcquireGlobal())
330330
throw tooManyConnections;
331-
if (!tryAcquirePerHost(poolKey))
331+
if (!tryAcquirePerHost(poolKey)) {
332+
if (maxTotalConnectionsEnabled)
333+
freeChannels.release();
334+
332335
throw tooManyConnectionsPerHost;
336+
}
333337
}
334338

335339
public void close() {

providers/netty4/src/main/java/org/asynchttpclient/providers/netty4/channel/ChannelManager.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public class ChannelManager {
9393
private final IOException poolAlreadyClosed;
9494

9595
private final ChannelPool channelPool;
96-
private final boolean maxConnectionsEnabled;
96+
private final boolean maxTotalConnectionsEnabled;
9797
private final Semaphore freeChannels;
9898
private final ChannelGroup openChannels;
9999
private final boolean maxConnectionsPerHostEnabled;
@@ -119,10 +119,10 @@ public ChannelManager(AsyncHttpClientConfig config, NettyAsyncHttpProviderConfig
119119
tooManyConnections = buildStaticException(String.format("Too many connections %s", config.getMaxConnections()));
120120
tooManyConnectionsPerHost = buildStaticException(String.format("Too many connections per host %s", config.getMaxConnectionsPerHost()));
121121
poolAlreadyClosed = buildStaticException("Pool is already closed");
122-
maxConnectionsEnabled = config.getMaxConnections() > 0;
122+
maxTotalConnectionsEnabled = config.getMaxConnections() > 0;
123123
maxConnectionsPerHostEnabled = config.getMaxConnectionsPerHost() > 0;
124124

125-
if (maxConnectionsEnabled) {
125+
if (maxTotalConnectionsEnabled) {
126126
openChannels = new CleanupChannelGroup("asyncHttpClient") {
127127
@Override
128128
public boolean remove(Object o) {
@@ -280,7 +280,7 @@ public boolean removeAll(Channel connection) {
280280
}
281281

282282
private boolean tryAcquireGlobal() {
283-
return !maxConnectionsEnabled || freeChannels.tryAcquire();
283+
return !maxTotalConnectionsEnabled || freeChannels.tryAcquire();
284284
}
285285

286286
private Semaphore getFreeConnectionsForHost(String poolKey) {
@@ -304,8 +304,12 @@ public void preemptChannel(String poolKey) throws IOException {
304304
throw poolAlreadyClosed;
305305
if (!tryAcquireGlobal())
306306
throw tooManyConnections;
307-
if (!tryAcquirePerHost(poolKey))
307+
if (!tryAcquirePerHost(poolKey)) {
308+
if (maxTotalConnectionsEnabled)
309+
freeChannels.release();
310+
308311
throw tooManyConnectionsPerHost;
312+
}
309313
}
310314

311315
public void close() {
@@ -334,7 +338,7 @@ public void closeChannel(Channel channel) {
334338
}
335339

336340
public void abortChannelPreemption(String poolKey) {
337-
if (maxConnectionsEnabled)
341+
if (maxTotalConnectionsEnabled)
338342
freeChannels.release();
339343
if (maxConnectionsPerHostEnabled)
340344
getFreeConnectionsForHost(poolKey).release();

0 commit comments

Comments
 (0)