Skip to content

Commit 31864fa

Browse files
Spikhalskiyslandelle
authored andcommitted
Expose ioThreadsCount config property (AsyncHttpClient#1283)
1 parent d8a1dff commit 31864fa

File tree

5 files changed

+29
-6
lines changed

5 files changed

+29
-6
lines changed

client/src/main/java/org/asynchttpclient/AsyncHttpClientConfig.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,8 @@ public interface AsyncHttpClientConfig {
288288

289289
ByteBufAllocator getAllocator();
290290

291+
int getIoThreadsCount();
292+
291293
interface AdditionalChannelInitializer {
292294

293295
void initChannel(Channel channel) throws Exception;

client/src/main/java/org/asynchttpclient/DefaultAsyncHttpClientConfig.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ public class DefaultAsyncHttpClientConfig implements AsyncHttpClientConfig {
131131
private final AdditionalChannelInitializer httpAdditionalChannelInitializer;
132132
private final AdditionalChannelInitializer wsAdditionalChannelInitializer;
133133
private final ResponseBodyPartFactory responseBodyPartFactory;
134+
private final int ioThreadsCount;
134135

135136
private DefaultAsyncHttpClientConfig(//
136137
// http
@@ -203,7 +204,8 @@ private DefaultAsyncHttpClientConfig(//
203204
ThreadFactory threadFactory,//
204205
AdditionalChannelInitializer httpAdditionalChannelInitializer,//
205206
AdditionalChannelInitializer wsAdditionalChannelInitializer,//
206-
ResponseBodyPartFactory responseBodyPartFactory) {
207+
ResponseBodyPartFactory responseBodyPartFactory,//
208+
int ioThreadsCount) {
207209

208210
// http
209211
this.followRedirect = followRedirect;
@@ -276,6 +278,7 @@ private DefaultAsyncHttpClientConfig(//
276278
this.httpAdditionalChannelInitializer = httpAdditionalChannelInitializer;
277279
this.wsAdditionalChannelInitializer = wsAdditionalChannelInitializer;
278280
this.responseBodyPartFactory = responseBodyPartFactory;
281+
this.ioThreadsCount = ioThreadsCount;
279282
}
280283

281284
@Override
@@ -581,6 +584,11 @@ public ResponseBodyPartFactory getResponseBodyPartFactory() {
581584
return responseBodyPartFactory;
582585
}
583586

587+
@Override
588+
public int getIoThreadsCount() {
589+
return ioThreadsCount;
590+
}
591+
584592
/**
585593
* Builder for an {@link AsyncHttpClient}
586594
*/
@@ -659,6 +667,7 @@ public static class Builder {
659667
private AdditionalChannelInitializer httpAdditionalChannelInitializer;
660668
private AdditionalChannelInitializer wsAdditionalChannelInitializer;
661669
private ResponseBodyPartFactory responseBodyPartFactory = ResponseBodyPartFactory.EAGER;
670+
private int ioThreadsCount = defaultIoThreadsCount();
662671

663672
public Builder() {
664673
}
@@ -732,6 +741,7 @@ public Builder(AsyncHttpClientConfig config) {
732741
httpAdditionalChannelInitializer = config.getHttpAdditionalChannelInitializer();
733742
wsAdditionalChannelInitializer = config.getWsAdditionalChannelInitializer();
734743
responseBodyPartFactory = config.getResponseBodyPartFactory();
744+
ioThreadsCount = config.getIoThreadsCount();
735745
}
736746

737747
// http
@@ -1066,6 +1076,11 @@ public Builder setResponseBodyPartFactory(ResponseBodyPartFactory responseBodyPa
10661076
return this;
10671077
}
10681078

1079+
public Builder setIoThreadsCount(int ioThreadsCount) {
1080+
this.ioThreadsCount = ioThreadsCount;
1081+
return this;
1082+
}
1083+
10691084
private ProxyServerSelector resolveProxyServerSelector() {
10701085
if (proxyServerSelector != null)
10711086
return proxyServerSelector;
@@ -1139,7 +1154,8 @@ public DefaultAsyncHttpClientConfig build() {
11391154
threadFactory, //
11401155
httpAdditionalChannelInitializer, //
11411156
wsAdditionalChannelInitializer, //
1142-
responseBodyPartFactory);
1157+
responseBodyPartFactory, //
1158+
ioThreadsCount);
11431159
}
11441160
}
11451161
}

client/src/main/java/org/asynchttpclient/config/AsyncHttpClientConfigDefaults.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,4 +197,8 @@ public static int defaultShutdownTimeout() {
197197
public static boolean defaultUseNativeTransport() {
198198
return AsyncHttpClientConfigHelper.getAsyncHttpClientConfig().getBoolean(ASYNC_CLIENT_CONFIG_ROOT + "useNativeTransport");
199199
}
200+
201+
public static int defaultIoThreadsCount() {
202+
return AsyncHttpClientConfigHelper.getAsyncHttpClientConfig().getInt(ASYNC_CLIENT_CONFIG_ROOT + "ioThreadsCount");
203+
}
200204
}

client/src/main/java/org/asynchttpclient/netty/channel/ChannelManager.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -166,11 +166,11 @@ public boolean remove(Object o) {
166166
ChannelFactory<? extends Channel> channelFactory;
167167
if (allowReleaseEventLoopGroup) {
168168
if (config.isUseNativeTransport()) {
169-
eventLoopGroup = newEpollEventLoopGroup(threadFactory);
169+
eventLoopGroup = newEpollEventLoopGroup(config.getIoThreadsCount(), threadFactory);
170170
channelFactory = getEpollSocketChannelFactory();
171171

172172
} else {
173-
eventLoopGroup = new NioEventLoopGroup(0, threadFactory);
173+
eventLoopGroup = new NioEventLoopGroup(config.getIoThreadsCount(), threadFactory);
174174
channelFactory = NioSocketChannelFactory.INSTANCE;
175175
}
176176

@@ -224,10 +224,10 @@ private Bootstrap newBootstrap(ChannelFactory<? extends Channel> channelFactory,
224224
return bootstrap;
225225
}
226226

227-
private EventLoopGroup newEpollEventLoopGroup(ThreadFactory threadFactory) {
227+
private EventLoopGroup newEpollEventLoopGroup(int ioThreadsCount, ThreadFactory threadFactory) {
228228
try {
229229
Class<?> epollEventLoopGroupClass = Class.forName("io.netty.channel.epoll.EpollEventLoopGroup");
230-
return (EventLoopGroup) epollEventLoopGroupClass.getConstructor(int.class, ThreadFactory.class).newInstance(0, threadFactory);
230+
return (EventLoopGroup) epollEventLoopGroupClass.getConstructor(int.class, ThreadFactory.class).newInstance(ioThreadsCount, threadFactory);
231231
} catch (Exception e) {
232232
throw new IllegalArgumentException(e);
233233
}

client/src/main/resources/ahc-default.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,4 @@ org.asynchttpclient.keepEncodingHeader=false
4242
org.asynchttpclient.shutdownQuietPeriod=2000
4343
org.asynchttpclient.shutdownTimeout=15000
4444
org.asynchttpclient.useNativeTransport=false
45+
org.asynchttpclient.ioThreadsCount=0

0 commit comments

Comments
 (0)