Skip to content

Commit d8b4872

Browse files
author
Stephane Landelle
committed
Fix Exception message when maxConnectionPerHost is reached, close AsyncHttpClient#799
1 parent 577615a commit d8b4872

File tree

5 files changed

+36
-17
lines changed

5 files changed

+36
-17
lines changed

api/src/main/java/org/asynchttpclient/util/MiscUtils.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,10 @@ public static void closeSilently(Closeable closeable) {
5858
} catch (IOException e) {
5959
}
6060
}
61+
62+
public static IOException buildStaticException(String message) {
63+
IOException ioe = new IOException(message);
64+
ioe.setStackTrace(new StackTraceElement[] {});
65+
return ioe;
66+
}
6167
}

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import static org.asynchttpclient.providers.netty.commons.util.HttpUtils.WEBSOCKET;
1717
import static org.asynchttpclient.providers.netty.commons.util.HttpUtils.isSecure;
1818
import static org.asynchttpclient.providers.netty.commons.util.HttpUtils.isWebSocket;
19+
import static org.asynchttpclient.util.MiscUtils.buildStaticException;
1920
import static org.jboss.netty.channel.Channels.pipeline;
2021
import static org.jboss.netty.handler.ssl.SslHandler.getDefaultBufferPool;
2122

@@ -93,6 +94,9 @@ public class ChannelManager {
9394
private final ConcurrentHashMap<Integer, String> channelId2KeyPool;
9495
private final long handshakeTimeout;
9596
private final Timer nettyTimer;
97+
private final IOException tooManyConnections;
98+
private final IOException tooManyConnectionsPerHost;
99+
private final IOException poolAlreadyClosed;
96100

97101
private final ClientSocketChannelFactory socketChannelFactory;
98102
private final boolean allowReleaseSocketChannelFactory;
@@ -118,6 +122,9 @@ public ChannelManager(AsyncHttpClientConfig config, NettyAsyncHttpProviderConfig
118122
}
119123
this.channelPool = channelPool;
120124

125+
tooManyConnections = buildStaticException(String.format("Too many connections %s", config.getMaxConnections()));
126+
tooManyConnectionsPerHost = buildStaticException(String.format("Too many connections per host %s", config.getMaxConnectionsPerHost()));
127+
poolAlreadyClosed = buildStaticException("Pool is already closed");
121128
maxTotalConnectionsEnabled = config.getMaxConnections() > 0;
122129
maxConnectionsPerHostEnabled = config.getMaxConnectionsPerHost() > 0;
123130

@@ -316,8 +323,13 @@ private boolean tryAcquirePerHost(String poolKey) {
316323
return !maxConnectionsPerHostEnabled || getFreeConnectionsForHost(poolKey).tryAcquire();
317324
}
318325

319-
public boolean preemptChannel(String poolKey) {
320-
return channelPool.isOpen() && tryAcquireGlobal() && tryAcquirePerHost(poolKey);
326+
public void preemptChannel(String poolKey) throws IOException {
327+
if (!channelPool.isOpen())
328+
throw poolAlreadyClosed;
329+
if (!tryAcquireGlobal())
330+
throw tooManyConnections;
331+
if (!tryAcquirePerHost(poolKey))
332+
throw tooManyConnectionsPerHost;
321333
}
322334

323335
public void close() {

providers/netty3/src/main/java/org/asynchttpclient/providers/netty3/request/NettyRequestSender.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ public final class NettyRequestSender {
7070
private final Timer nettyTimer;
7171
private final AtomicBoolean closed;
7272
private final NettyRequestFactory requestFactory;
73-
private final IOException tooManyConnections;
7473

7574
public NettyRequestSender(AsyncHttpClientConfig config,//
7675
NettyAsyncHttpProviderConfig nettyConfig,//
@@ -82,8 +81,6 @@ public NettyRequestSender(AsyncHttpClientConfig config,//
8281
this.nettyTimer = nettyTimer;
8382
this.closed = closed;
8483
requestFactory = new NettyRequestFactory(config, nettyConfig);
85-
tooManyConnections = new IOException(String.format("Too many connections %s", config.getMaxConnections()));
86-
tooManyConnections.setStackTrace(new StackTraceElement[] {});
8784
}
8885

8986
public <T> ListenableFuture<T> sendRequest(final Request request,//
@@ -275,8 +272,7 @@ private <T> ListenableFuture<T> sendRequestWithNewChannel(//
275272
if (config.getMaxConnectionsPerHost() > 0)
276273
poolKey = channelManager.getPartitionId(future);
277274

278-
if (!channelManager.preemptChannel(poolKey))
279-
throw tooManyConnections;
275+
channelManager.preemptChannel(poolKey);
280276

281277
channelPreempted = true;
282278
}

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

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import static org.asynchttpclient.providers.netty.commons.util.HttpUtils.WEBSOCKET;
1717
import static org.asynchttpclient.providers.netty.commons.util.HttpUtils.isSecure;
1818
import static org.asynchttpclient.providers.netty.commons.util.HttpUtils.isWebSocket;
19+
import static org.asynchttpclient.util.MiscUtils.buildStaticException;
1920
import io.netty.bootstrap.Bootstrap;
2021
import io.netty.channel.Channel;
2122
import io.netty.channel.ChannelInitializer;
@@ -80,16 +81,16 @@ public class ChannelManager {
8081
private final AsyncHttpClientConfig config;
8182
private final NettyAsyncHttpProviderConfig nettyConfig;
8283
private final SSLEngineFactory sslEngineFactory;
83-
8484
private final EventLoopGroup eventLoopGroup;
8585
private final boolean allowReleaseEventLoopGroup;
86-
8786
private final Bootstrap plainBootstrap;
8887
private final Bootstrap secureBootstrap;
8988
private final Bootstrap webSocketBootstrap;
9089
private final Bootstrap secureWebSocketBootstrap;
91-
9290
private final long handshakeTimeout;
91+
private final IOException tooManyConnections;
92+
private final IOException tooManyConnectionsPerHost;
93+
private final IOException poolAlreadyClosed;
9394

9495
private final ChannelPool channelPool;
9596
private final boolean maxConnectionsEnabled;
@@ -115,6 +116,9 @@ public ChannelManager(AsyncHttpClientConfig config, NettyAsyncHttpProviderConfig
115116
}
116117
this.channelPool = channelPool;
117118

119+
tooManyConnections = buildStaticException(String.format("Too many connections %s", config.getMaxConnections()));
120+
tooManyConnectionsPerHost = buildStaticException(String.format("Too many connections per host %s", config.getMaxConnectionsPerHost()));
121+
poolAlreadyClosed = buildStaticException("Pool is already closed");
118122
maxConnectionsEnabled = config.getMaxConnections() > 0;
119123
maxConnectionsPerHostEnabled = config.getMaxConnectionsPerHost() > 0;
120124

@@ -295,8 +299,13 @@ private boolean tryAcquirePerHost(String poolKey) {
295299
return !maxConnectionsPerHostEnabled || getFreeConnectionsForHost(poolKey).tryAcquire();
296300
}
297301

298-
public boolean preemptChannel(String poolKey) {
299-
return channelPool.isOpen() && tryAcquireGlobal() && tryAcquirePerHost(poolKey);
302+
public void preemptChannel(String poolKey) throws IOException {
303+
if (!channelPool.isOpen())
304+
throw poolAlreadyClosed;
305+
if (!tryAcquireGlobal())
306+
throw tooManyConnections;
307+
if (!tryAcquirePerHost(poolKey))
308+
throw tooManyConnectionsPerHost;
300309
}
301310

302311
public void close() {

providers/netty4/src/main/java/org/asynchttpclient/providers/netty4/request/NettyRequestSender.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@ public final class NettyRequestSender {
7272
private final Timer nettyTimer;
7373
private final AtomicBoolean closed;
7474
private final NettyRequestFactory requestFactory;
75-
private final IOException tooManyConnections;
7675

7776
public NettyRequestSender(AsyncHttpClientConfig config,//
7877
NettyAsyncHttpProviderConfig nettyConfig,//
@@ -84,8 +83,6 @@ public NettyRequestSender(AsyncHttpClientConfig config,//
8483
this.nettyTimer = nettyTimer;
8584
this.closed = closed;
8685
requestFactory = new NettyRequestFactory(config, nettyConfig);
87-
tooManyConnections = new IOException(String.format("Too many connections %s", config.getMaxConnections()));
88-
tooManyConnections.setStackTrace(new StackTraceElement[] {});
8986
}
9087

9188
public <T> ListenableFuture<T> sendRequest(final Request request,//
@@ -277,8 +274,7 @@ private <T> ListenableFuture<T> sendRequestWithNewChannel(//
277274
if (config.getMaxConnectionsPerHost() > 0)
278275
poolKey = channelManager.getPartitionId(future);
279276

280-
if (!channelManager.preemptChannel(poolKey))
281-
throw tooManyConnections;
277+
channelManager.preemptChannel(poolKey);
282278
}
283279

284280
if (asyncHandler instanceof AsyncHandlerExtensions)

0 commit comments

Comments
 (0)