Skip to content

Commit 9376eee

Browse files
committed
Add a way to turn off SSL connection poolling
1 parent 9ee811b commit 9376eee

File tree

3 files changed

+60
-3
lines changed

3 files changed

+60
-3
lines changed

src/main/java/com/ning/http/client/AsyncHttpClientConfig.java

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ public class AsyncHttpClientConfig {
7373
private final List<IOExceptionFilter> ioExceptionFilters;
7474
private final int requestCompressionLevel;
7575
private final int maxRequestRetry;
76+
private final boolean allowSslConnectionPool;
7677

7778
private AsyncHttpClientConfig(int maxTotalConnections,
7879
int maxConnectionPerHost,
@@ -94,7 +95,9 @@ private AsyncHttpClientConfig(int maxTotalConnections,
9495
List<RequestFilter> requestFilters,
9596
List<ResponseFilter> responseFilters,
9697
List<IOExceptionFilter> ioExceptionFilters,
97-
int requestCompressionLevel, int maxRequestRetry) {
98+
int requestCompressionLevel,
99+
int maxRequestRetry,
100+
boolean allowSslConnectionCaching) {
98101

99102
this.maxTotalConnections = maxTotalConnections;
100103
this.maxConnectionPerHost = maxConnectionPerHost;
@@ -117,6 +120,7 @@ private AsyncHttpClientConfig(int maxTotalConnections,
117120
this.requestCompressionLevel = requestCompressionLevel;
118121
this.maxRequestRetry = maxRequestRetry;
119122
this.reaper = reaper;
123+
this.allowSslConnectionPool = allowSslConnectionCaching;
120124

121125
if (applicationThreadPool == null) {
122126
this.applicationThreadPool = Executors.newCachedThreadPool();
@@ -362,6 +366,15 @@ public int getMaxRequestRetry() {
362366
return maxRequestRetry;
363367
}
364368

369+
/**
370+
* Return true is SSL connection polling is enabled. Default is true.
371+
* @return
372+
*/
373+
public boolean isSslConnectionPoolEnabled() {
374+
return allowSslConnectionPool;
375+
}
376+
377+
365378
/**
366379
* Builder for an {@link AsyncHttpClient}
367380
*/
@@ -401,6 +414,7 @@ public Thread newThread(Runnable r) {
401414
private final List<RequestFilter> requestFilters = new LinkedList<RequestFilter>();
402415
private final List<ResponseFilter> responseFilters = new LinkedList<ResponseFilter>();
403416
private final List<IOExceptionFilter> ioExceptionFilters = new LinkedList<IOExceptionFilter>();
417+
private boolean allowSslConnectionPool = true;
404418

405419
public Builder() {
406420
}
@@ -738,6 +752,16 @@ public Builder setMaxRequestRetry(int maxRequestRetry) {
738752
return this;
739753
}
740754

755+
/**
756+
* Return true is if connections pooling is enabled.
757+
* @param allowSslConnectionPool true if enabled
758+
* @return true is if connections pooling is enabled.
759+
*/
760+
public Builder setAllowSslConnectionPool(boolean allowSslConnectionPool) {
761+
this.allowSslConnectionPool = allowSslConnectionPool;
762+
return this;
763+
}
764+
741765
/**
742766
* Create a config builder with values taken from the given prototype configuration.
743767
*
@@ -796,7 +820,8 @@ public AsyncHttpClientConfig build() {
796820
responseFilters,
797821
ioExceptionFilters,
798822
requestCompressionLevel,
799-
maxRequestRetry);
823+
maxRequestRetry,
824+
allowSslConnectionPool);
800825
}
801826
}
802827
}

src/main/java/com/ning/http/client/providers/netty/NettyConnectionsPool.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ public NettyConnectionsPool(NettyAsyncHttpProvider provider) {
5454
* {@inheritDoc}
5555
*/
5656
public boolean offer(String uri, Channel channel) {
57+
if (!provider.getConfig().isSslConnectionPoolEnabled() && uri.startsWith("https") ) {
58+
return false;
59+
}
60+
5761
log.debug("Adding uri: {} for channel {}", uri, channel);
5862
channel.getPipeline().getContext(NettyAsyncHttpProvider.class).setAttachment(new NettyAsyncHttpProvider.DiscardEvent());
5963

@@ -117,6 +121,10 @@ public void run(Timeout timeout) {
117121
* {@inheritDoc}
118122
*/
119123
public Channel poll(String uri) {
124+
if (!provider.getConfig().isSslConnectionPoolEnabled() && uri.startsWith("https") ) {
125+
return null;
126+
}
127+
120128
Channel channel = null;
121129
ConcurrentLinkedQueue<Channel> pooledConnectionForHost = connectionsPool.get(uri);
122130
if (pooledConnectionForHost != null) {
@@ -154,7 +162,7 @@ public Channel poll(String uri) {
154162
*/
155163
public boolean removeAll(Channel channel) {
156164
if (isClosed.get()) return false;
157-
165+
158166
boolean isRemoved = false;
159167
Iterator<Map.Entry<String, ConcurrentLinkedQueue<Channel>>> i = connectionsPool.entrySet().iterator();
160168
while (i.hasNext()) {

src/test/java/com/ning/http/client/async/BasicHttpsTest.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,30 @@ public void multipleSSLRequestsTest() throws Throwable {
243243
c.close();
244244
}
245245

246+
@Test(groups = {"standalone", "default_provider"})
247+
public void multipleSSLWithoutCacheTest() throws Throwable {
248+
final AsyncHttpClient c = new AsyncHttpClient(new Builder().setSSLContext(createSSLContext()).setAllowSslConnectionPool(false).build());
249+
250+
String body = "hello there";
251+
c.preparePost(getTargetUrl())
252+
.setBody(body)
253+
.setHeader("Content-Type", "text/html")
254+
.execute();
255+
256+
c.preparePost(getTargetUrl())
257+
.setBody(body)
258+
.setHeader("Content-Type", "text/html")
259+
.execute();
260+
261+
Response response = c.preparePost(getTargetUrl())
262+
.setBody(body)
263+
.setHeader("Content-Type", "text/html")
264+
.execute().get();
265+
266+
assertEquals(response.getResponseBody(), body);
267+
c.close();
268+
}
269+
246270
@Test(groups = {"standalone", "default_provider"})
247271
public void reconnectsAfterFailedCertificationPath() throws Throwable {
248272
final AsyncHttpClient c = new AsyncHttpClient(new Builder().setSSLContext(createSSLContext()).build());

0 commit comments

Comments
 (0)