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; throw poolAlreadyClosed;
if (!tryAcquireGlobal()) if (!tryAcquireGlobal())
throw tooManyConnections; throw tooManyConnections;
if (!tryAcquirePerHost(poolKey)) if (!tryAcquirePerHost(poolKey)) {
if (maxTotalConnectionsEnabled)
freeChannels.release();

throw tooManyConnectionsPerHost; throw tooManyConnectionsPerHost;
}
} }


public void close() { public void close() {
Expand Down
Expand Up @@ -93,7 +93,7 @@ public class ChannelManager {
private final IOException poolAlreadyClosed; private final IOException poolAlreadyClosed;


private final ChannelPool channelPool; private final ChannelPool channelPool;
private final boolean maxConnectionsEnabled; private final boolean maxTotalConnectionsEnabled;
private final Semaphore freeChannels; private final Semaphore freeChannels;
private final ChannelGroup openChannels; private final ChannelGroup openChannels;
private final boolean maxConnectionsPerHostEnabled; 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())); tooManyConnections = buildStaticException(String.format("Too many connections %s", config.getMaxConnections()));
tooManyConnectionsPerHost = buildStaticException(String.format("Too many connections per host %s", config.getMaxConnectionsPerHost())); tooManyConnectionsPerHost = buildStaticException(String.format("Too many connections per host %s", config.getMaxConnectionsPerHost()));
poolAlreadyClosed = buildStaticException("Pool is already closed"); poolAlreadyClosed = buildStaticException("Pool is already closed");
maxConnectionsEnabled = config.getMaxConnections() > 0; maxTotalConnectionsEnabled = config.getMaxConnections() > 0;
maxConnectionsPerHostEnabled = config.getMaxConnectionsPerHost() > 0; maxConnectionsPerHostEnabled = config.getMaxConnectionsPerHost() > 0;


if (maxConnectionsEnabled) { if (maxTotalConnectionsEnabled) {
openChannels = new CleanupChannelGroup("asyncHttpClient") { openChannels = new CleanupChannelGroup("asyncHttpClient") {
@Override @Override
public boolean remove(Object o) { public boolean remove(Object o) {
Expand Down Expand Up @@ -280,7 +280,7 @@ public boolean removeAll(Channel connection) {
} }


private boolean tryAcquireGlobal() { private boolean tryAcquireGlobal() {
return !maxConnectionsEnabled || freeChannels.tryAcquire(); return !maxTotalConnectionsEnabled || freeChannels.tryAcquire();
} }


private Semaphore getFreeConnectionsForHost(String poolKey) { private Semaphore getFreeConnectionsForHost(String poolKey) {
Expand All @@ -304,8 +304,12 @@ public void preemptChannel(String poolKey) throws IOException {
throw poolAlreadyClosed; throw poolAlreadyClosed;
if (!tryAcquireGlobal()) if (!tryAcquireGlobal())
throw tooManyConnections; throw tooManyConnections;
if (!tryAcquirePerHost(poolKey)) if (!tryAcquirePerHost(poolKey)) {
if (maxTotalConnectionsEnabled)
freeChannels.release();

throw tooManyConnectionsPerHost; throw tooManyConnectionsPerHost;
}
} }


public void close() { public void close() {
Expand Down Expand Up @@ -334,7 +338,7 @@ public void closeChannel(Channel channel) {
} }


public void abortChannelPreemption(String poolKey) { public void abortChannelPreemption(String poolKey) {
if (maxConnectionsEnabled) if (maxTotalConnectionsEnabled)
freeChannels.release(); freeChannels.release();
if (maxConnectionsPerHostEnabled) if (maxConnectionsPerHostEnabled)
getFreeConnectionsForHost(poolKey).release(); getFreeConnectionsForHost(poolKey).release();
Expand Down

0 comments on commit 05ec1af

Please sign in to comment.