@@ -133,9 +133,7 @@ public ChannelManager(final AsyncHttpClientConfig config, Timer nettyTimer) {
133133maxTotalConnectionsEnabled =config .getMaxConnections () >0 ;
134134maxConnectionsPerHostEnabled =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
140138if (maxTotalConnectionsEnabled ||maxConnectionsPerHostEnabled ) {
141139openChannels =new DefaultChannelGroup ("asyncHttpClient" ,GlobalEventExecutor .INSTANCE ) {
@@ -309,16 +307,15 @@ public final void tryToOfferChannelToPool(Channel channel, AsyncHandler<?> async
309307if (asyncHandler instanceof AsyncHandlerExtensions )
310308AsyncHandlerExtensions .class .cast (asyncHandler ).onConnectionOffer (channel );
311309if (channelPool .offer (channel ,partitionKey )) {
312- if (maxConnectionsPerHostEnabled )
310+ if (maxConnectionsPerHostEnabled ) {
313311channel .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
324321public Channel poll (Uri uri ,String virtualHost ,ProxyServer proxy ,ChannelPoolPartitioning connectionPoolPartitioning ) {
@@ -335,9 +332,8 @@ private boolean tryAcquireGlobal() {
335332 }
336333
337334private 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
343339private boolean tryAcquirePerHost (Object partitionKey ) {