Skip to content

Commit 3c176c0

Browse files
committed
Merge pull request AsyncHttpClient#13
2 parents 0ff3ce5 + 95fc68b commit 3c176c0

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
@@ -80,6 +80,7 @@ public class AsyncHttpClientConfig {
8080
private final boolean useRawUrl;
8181
private final boolean removeQueryParamOnRedirect;
8282
private final HostnameVerifier hostnameVerifier;
83+
private final int ioThreadMultiplier;
8384

8485
private AsyncHttpClientConfig(int maxTotalConnections,
8586
int maxConnectionPerHost,
@@ -106,7 +107,8 @@ private AsyncHttpClientConfig(int maxTotalConnections,
106107
boolean allowSslConnectionCaching,
107108
boolean useRawUrl,
108109
boolean removeQueryParamOnRedirect,
109-
HostnameVerifier hostnameVerifier) {
110+
HostnameVerifier hostnameVerifier,
111+
int ioThreadMultiplier) {
110112

111113
this.maxTotalConnections = maxTotalConnections;
112114
this.maxConnectionPerHost = maxConnectionPerHost;
@@ -132,6 +134,7 @@ private AsyncHttpClientConfig(int maxTotalConnections,
132134
this.allowSslConnectionPool = allowSslConnectionCaching;
133135
this.removeQueryParamOnRedirect = removeQueryParamOnRedirect;
134136
this.hostnameVerifier = hostnameVerifier;
137+
this.ioThreadMultiplier = ioThreadMultiplier;
135138

136139
if (applicationThreadPool == null) {
137140
this.applicationThreadPool = Executors.newCachedThreadPool();
@@ -426,6 +429,13 @@ public boolean isClosed(){
426429
public HostnameVerifier getHostnameVerifier() {
427430
return hostnameVerifier;
428431
}
432+
/***
433+
*
434+
* @return number to multiply by availableProcessors() that will determine # of NioWorkers to use
435+
*/
436+
public int getIoThreadMultiplier() {
437+
return ioThreadMultiplier;
438+
}
429439

430440
/**
431441
* Builder for an {@link AsyncHttpClient}
@@ -476,6 +486,7 @@ public boolean verify( String s, SSLSession sslSession ) {
476486
return true;
477487
}
478488
};
489+
private int ioThreadMultiplier = 2;
479490

480491
public Builder() {
481492
}
@@ -865,6 +876,11 @@ public Builder setUseProxyProperties(boolean useProxyProperties) {
865876
return this;
866877
}
867878

879+
public Builder setIOThreadMultiplier(int multiplier){
880+
this.ioThreadMultiplier = multiplier;
881+
return this;
882+
}
883+
868884
/**
869885
* Set the {@link HostnameVerifier}
870886
* @param hostnameVerifier {@link HostnameVerifier}
@@ -903,6 +919,7 @@ public Builder(AsyncHttpClientConfig prototype) {
903919
requestFilters.addAll(prototype.getRequestFilters());
904920
responseFilters.addAll(prototype.getResponseFilters());
905921
useRawUrl = prototype.isUseRawUrl();
922+
ioThreadMultiplier = prototype.getIoThreadMultiplier();
906923
}
907924

908925
/**
@@ -946,7 +963,8 @@ public AsyncHttpClientConfig build() {
946963
allowSslConnectionPool,
947964
useRawUrl,
948965
removeQueryParamOnRedirect,
949-
hostnameVerifier);
966+
hostnameVerifier,
967+
ioThreadMultiplier);
950968
}
951969
}
952970
}

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
@@ -194,7 +194,9 @@ public NettyAsyncHttpProvider(AsyncHttpClientConfig config) {
194194
} else {
195195
e = Executors.newCachedThreadPool();
196196
}
197-
socketChannelFactory = new NioClientSocketChannelFactory(e, config.executorService());
197+
int numWorkers = config.getIoThreadMultiplier() * Runtime.getRuntime().availableProcessors();
198+
log.info("Number of application's worked threads is {}", numWorkers);
199+
socketChannelFactory = new NioClientSocketChannelFactory(e, config.executorService(), numWorkers);
198200
}
199201
plainBootstrap = new ClientBootstrap(socketChannelFactory);
200202
secureBootstrap = new ClientBootstrap(socketChannelFactory);

0 commit comments

Comments
 (0)