Skip to content

Commit 06c58c6

Browse files
committed
Release channel lock when channel is not offered to pool, see AsyncHttpClient#1377
1 parent 2d76bbd commit 06c58c6

File tree

2 files changed

+12
-15
lines changed

2 files changed

+12
-15
lines changed

client/src/main/java/org/asynchttpclient/netty/channel/ChannelManager.java

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,7 @@ public ChannelManager(final AsyncHttpClientConfig config, Timer nettyTimer) {
133133
maxTotalConnectionsEnabled = config.getMaxConnections() > 0;
134134
maxConnectionsPerHostEnabled = config.getMaxConnectionsPerHost() > 0;
135135

136-
freeChannels = maxTotalConnectionsEnabled ?
137-
new NonBlockingSemaphore(config.getMaxConnections()) :
138-
NonBlockingSemaphoreInfinite.INSTANCE;
136+
freeChannels = maxTotalConnectionsEnabled ? new NonBlockingSemaphore(config.getMaxConnections()) : NonBlockingSemaphoreInfinite.INSTANCE;
139137

140138
if (maxTotalConnectionsEnabled || maxConnectionsPerHostEnabled) {
141139
openChannels = new DefaultChannelGroup("asyncHttpClient", GlobalEventExecutor.INSTANCE) {
@@ -309,16 +307,15 @@ public final void tryToOfferChannelToPool(Channel channel, AsyncHandler<?> async
309307
if (asyncHandler instanceof AsyncHandlerExtensions)
310308
AsyncHandlerExtensions.class.cast(asyncHandler).onConnectionOffer(channel);
311309
if (channelPool.offer(channel, partitionKey)) {
312-
if (maxConnectionsPerHostEnabled)
310+
if (maxConnectionsPerHostEnabled) {
313311
channel.attr(partitionKeyAttr).setIfAbsent(partitionKey);
314-
} else {
315-
// rejected by pool
316-
closeChannel(channel);
312+
}
313+
return;
317314
}
318-
} else {
319-
// not offered
320-
closeChannel(channel);
321315
}
316+
// rejected by pool or not offered
317+
releaseChannelLock(partitionKey);
318+
closeChannel(channel);
322319
}
323320

324321
public Channel poll(Uri uri, String virtualHost, ProxyServer proxy, ChannelPoolPartitioning connectionPoolPartitioning) {
@@ -335,9 +332,8 @@ private boolean tryAcquireGlobal() {
335332
}
336333

337334
private NonBlockingSemaphoreLike getFreeConnectionsForHost(Object partitionKey) {
338-
return maxConnectionsPerHostEnabled ?
339-
freeChannelsPerHost.computeIfAbsent(partitionKey, pk -> new NonBlockingSemaphore(config.getMaxConnectionsPerHost())) :
340-
NonBlockingSemaphoreInfinite.INSTANCE;
335+
return maxConnectionsPerHostEnabled ? freeChannelsPerHost.computeIfAbsent(partitionKey, pk -> new NonBlockingSemaphore(config.getMaxConnectionsPerHost()))
336+
: NonBlockingSemaphoreInfinite.INSTANCE;
341337
}
342338

343339
private boolean tryAcquirePerHost(Object partitionKey) {

client/src/main/java/org/asynchttpclient/netty/handler/HttpHandler.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,11 @@ private void finishUpdate(final NettyResponseFuture<?> future, Channel channel,
4949
future.cancelTimeouts();
5050

5151
boolean keepAlive = future.isKeepAlive();
52-
if (expectOtherChunks && keepAlive)
52+
if (expectOtherChunks && keepAlive) {
5353
channelManager.drainChannelAndOffer(channel, future);
54-
else
54+
} else {
5555
channelManager.tryToOfferChannelToPool(channel, future.getAsyncHandler(), keepAlive, future.getPartitionKey());
56+
}
5657

5758
try {
5859
future.done();

0 commit comments

Comments
 (0)