Skip to content

Commit 95fc68b

Browse files
committed
add io thread config param
1 parent fcdcec9 commit 95fc68b

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

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

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ public class AsyncHttpClientConfig {
7777
private final boolean allowSslConnectionPool;
7878
private final boolean useRawUrl;
7979
private final boolean removeQueryParamOnRedirect;
80+
private final int ioThreadMultiplier;
8081

8182
private AsyncHttpClientConfig(int maxTotalConnections,
8283
int maxConnectionPerHost,
@@ -102,7 +103,8 @@ private AsyncHttpClientConfig(int maxTotalConnections,
102103
int maxRequestRetry,
103104
boolean allowSslConnectionCaching,
104105
boolean useRawUrl,
105-
boolean removeQueryParamOnRedirect) {
106+
boolean removeQueryParamOnRedirect,
107+
int ioThreadMultiplier) {
106108

107109
this.maxTotalConnections = maxTotalConnections;
108110
this.maxConnectionPerHost = maxConnectionPerHost;
@@ -127,6 +129,7 @@ private AsyncHttpClientConfig(int maxTotalConnections,
127129
this.reaper = reaper;
128130
this.allowSslConnectionPool = allowSslConnectionCaching;
129131
this.removeQueryParamOnRedirect = removeQueryParamOnRedirect;
132+
this.ioThreadMultiplier = ioThreadMultiplier;
130133

131134
if (applicationThreadPool == null) {
132135
this.applicationThreadPool = Executors.newCachedThreadPool();
@@ -406,6 +409,13 @@ public boolean isRemoveQueryParamOnRedirect() {
406409
return removeQueryParamOnRedirect;
407410
}
408411

412+
/***
413+
*
414+
* @return number to multiply by availableProcessors() that will determine # of NioWorkers to use
415+
*/
416+
public int getIoThreadMultiplier() {
417+
return ioThreadMultiplier;
418+
}
409419

410420
/**
411421
* Builder for an {@link AsyncHttpClient}
@@ -450,6 +460,7 @@ public Thread newThread(Runnable r) {
450460
private boolean allowSslConnectionPool = true;
451461
private boolean useRawUrl = false;
452462
private boolean removeQueryParamOnRedirect = true;
463+
private int ioThreadMultiplier = 2;
453464

454465
public Builder() {
455466
}
@@ -839,6 +850,11 @@ public Builder setUseProxyProperties(boolean useProxyProperties) {
839850
return this;
840851
}
841852

853+
public Builder setIOThreadMultiplier(int multiplier){
854+
this.ioThreadMultiplier = multiplier;
855+
return this;
856+
}
857+
842858
/**
843859
* Create a config builder with values taken from the given prototype configuration.
844860
*
@@ -867,6 +883,7 @@ public Builder(AsyncHttpClientConfig prototype) {
867883
requestFilters.addAll(prototype.getRequestFilters());
868884
responseFilters.addAll(prototype.getResponseFilters());
869885
useRawUrl = prototype.isUseRawUrl();
886+
ioThreadMultiplier = prototype.getIoThreadMultiplier();
870887
}
871888

872889
/**
@@ -904,7 +921,8 @@ public AsyncHttpClientConfig build() {
904921
maxRequestRetry,
905922
allowSslConnectionPool,
906923
useRawUrl,
907-
removeQueryParamOnRedirect);
924+
removeQueryParamOnRedirect,
925+
ioThreadMultiplier);
908926
}
909927
}
910928
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,9 @@ public NettyAsyncHttpProvider(AsyncHttpClientConfig config) {
195195
} else {
196196
e = Executors.newCachedThreadPool();
197197
}
198-
socketChannelFactory = new NioClientSocketChannelFactory(e, config.executorService());
198+
int numWorkers = config.getIoThreadMultiplier()*Runtime.getRuntime().availableProcessors();
199+
log.info("numWorkers=" + numWorkers);
200+
socketChannelFactory = new NioClientSocketChannelFactory(e, config.executorService(), numWorkers);
199201
}
200202
plainBootstrap = new ClientBootstrap(socketChannelFactory);
201203
secureBootstrap = new ClientBootstrap(socketChannelFactory);

0 commit comments

Comments
 (0)