Skip to content

Commit

Permalink
ChannelManager.preemptChannel should release global semaphore if tryA…
Browse files Browse the repository at this point in the history
…cquirePerHost fails, close #805
  • Loading branch information
Stephane Landelle committed Jan 15, 2015
1 parent 04df4e2 commit 05ec1af
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
Expand Up @@ -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() {
Expand Down
Expand Up @@ -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;
Expand All @@ -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) {
Expand Down Expand Up @@ -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) {
Expand All @@ -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() {
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 05ec1af

Please sign in to comment.