Skip to content

Commit 8468be5

Browse files
committed
thread factory for channelManager
1 parent 087b41a commit 8468be5

File tree

7 files changed

+68
-1
lines changed

7 files changed

+68
-1
lines changed

Diff for: client/src/main/java/org/asynchttpclient/AsyncHttpClientConfig.java

+15
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,13 @@ public interface AsyncHttpClientConfig {
5151
*/
5252
String getThreadPoolName();
5353

54+
/**
55+
* Return the name of {@link org.asynchttpclient.netty.channel.ChannelManager}, which is used for thread naming and debugging.
56+
*
57+
* @return the name.
58+
*/
59+
String getChannelThreadPoolName();
60+
5461
/**
5562
* Return the maximum number of connections an {@link AsyncHttpClient} can handle.
5663
*
@@ -149,6 +156,14 @@ public interface AsyncHttpClientConfig {
149156
*/
150157
ThreadFactory getThreadFactory();
151158

159+
/**
160+
* Return the {@link java.util.concurrent.ThreadFactory} an {@link org.asynchttpclient.netty.channel.ChannelManager} use for handling channels.
161+
*
162+
* @return the {@link java.util.concurrent.ThreadFactory} an {@link org.asynchttpclient.netty.channel.ChannelManager} use for handling channels.
163+
* If no {@link ThreadFactory} has been explicitly provided, this method will return <code>null</code>
164+
*/
165+
ThreadFactory getChannelThreadFactory();
166+
152167
/**
153168
* An instance of {@link ProxyServer} used by an {@link AsyncHttpClient}
154169
*

Diff for: client/src/main/java/org/asynchttpclient/DefaultAsyncHttpClientConfig.java

+32
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ public class DefaultAsyncHttpClientConfig implements AsyncHttpClientConfig {
112112

113113
// internals
114114
private final String threadPoolName;
115+
private final String channelThreadPoolName;
115116
private final int httpClientCodecMaxInitialLineLength;
116117
private final int httpClientCodecMaxHeaderSize;
117118
private final int httpClientCodecMaxChunkSize;
@@ -128,6 +129,7 @@ public class DefaultAsyncHttpClientConfig implements AsyncHttpClientConfig {
128129
private final int soRcvBuf;
129130
private final Timer nettyTimer;
130131
private final ThreadFactory threadFactory;
132+
private final ThreadFactory channelThreadFactory;
131133
private final Consumer<Channel> httpAdditionalChannelInitializer;
132134
private final Consumer<Channel> wsAdditionalChannelInitializer;
133135
private final ResponseBodyPartFactory responseBodyPartFactory;
@@ -199,6 +201,7 @@ private DefaultAsyncHttpClientConfig(// http
199201

200202
// internals
201203
String threadPoolName,
204+
String channelThreadPoolName,
202205
int httpClientCodecMaxInitialLineLength,
203206
int httpClientCodecMaxHeaderSize,
204207
int httpClientCodecMaxChunkSize,
@@ -212,6 +215,7 @@ private DefaultAsyncHttpClientConfig(// http
212215
ByteBufAllocator allocator,
213216
Timer nettyTimer,
214217
ThreadFactory threadFactory,
218+
ThreadFactory channelThreadFactory,
215219
Consumer<Channel> httpAdditionalChannelInitializer,
216220
Consumer<Channel> wsAdditionalChannelInitializer,
217221
ResponseBodyPartFactory responseBodyPartFactory,
@@ -287,6 +291,7 @@ private DefaultAsyncHttpClientConfig(// http
287291

288292
// internals
289293
this.threadPoolName = threadPoolName;
294+
this.channelThreadPoolName = channelThreadPoolName;
290295
this.httpClientCodecMaxInitialLineLength = httpClientCodecMaxInitialLineLength;
291296
this.httpClientCodecMaxHeaderSize = httpClientCodecMaxHeaderSize;
292297
this.httpClientCodecMaxChunkSize = httpClientCodecMaxChunkSize;
@@ -298,6 +303,7 @@ private DefaultAsyncHttpClientConfig(// http
298303
this.allocator = allocator;
299304
this.nettyTimer = nettyTimer;
300305
this.threadFactory = threadFactory;
306+
this.channelThreadFactory = channelThreadFactory;
301307
this.httpAdditionalChannelInitializer = httpAdditionalChannelInitializer;
302308
this.wsAdditionalChannelInitializer = wsAdditionalChannelInitializer;
303309
this.responseBodyPartFactory = responseBodyPartFactory;
@@ -581,6 +587,11 @@ public String getThreadPoolName() {
581587
return threadPoolName;
582588
}
583589

590+
@Override
591+
public String getChannelThreadPoolName() {
592+
return channelThreadPoolName;
593+
}
594+
584595
@Override
585596
public int getHttpClientCodecMaxInitialLineLength() {
586597
return httpClientCodecMaxInitialLineLength;
@@ -636,6 +647,11 @@ public ThreadFactory getThreadFactory() {
636647
return threadFactory;
637648
}
638649

650+
@Override
651+
public ThreadFactory getChannelThreadFactory() {
652+
return channelThreadFactory;
653+
}
654+
639655
@Override
640656
public Consumer<Channel> getHttpAdditionalChannelInitializer() {
641657
return httpAdditionalChannelInitializer;
@@ -732,6 +748,7 @@ public static class Builder {
732748

733749
// internals
734750
private String threadPoolName = defaultThreadPoolName();
751+
private String channelThreadPoolName = defaultChannelThreadPoolName();
735752
private int httpClientCodecMaxInitialLineLength = defaultHttpClientCodecMaxInitialLineLength();
736753
private int httpClientCodecMaxHeaderSize = defaultHttpClientCodecMaxHeaderSize();
737754
private int httpClientCodecMaxChunkSize = defaultHttpClientCodecMaxChunkSize();
@@ -743,6 +760,7 @@ public static class Builder {
743760
private EventLoopGroup eventLoopGroup;
744761
private Timer nettyTimer;
745762
private ThreadFactory threadFactory;
763+
private ThreadFactory channelThreadFactory;
746764
private Consumer<Channel> httpAdditionalChannelInitializer;
747765
private Consumer<Channel> wsAdditionalChannelInitializer;
748766
private ResponseBodyPartFactory responseBodyPartFactory = ResponseBodyPartFactory.EAGER;
@@ -814,6 +832,7 @@ public Builder(AsyncHttpClientConfig config) {
814832

815833
// internals
816834
threadPoolName = config.getThreadPoolName();
835+
channelThreadPoolName = config.getChannelThreadPoolName();
817836
httpClientCodecMaxInitialLineLength = config.getHttpClientCodecMaxInitialLineLength();
818837
httpClientCodecMaxHeaderSize = config.getHttpClientCodecMaxHeaderSize();
819838
httpClientCodecMaxChunkSize = config.getHttpClientCodecMaxChunkSize();
@@ -824,6 +843,7 @@ public Builder(AsyncHttpClientConfig config) {
824843
allocator = config.getAllocator();
825844
nettyTimer = config.getNettyTimer();
826845
threadFactory = config.getThreadFactory();
846+
channelThreadFactory = config.getChannelThreadFactory();
827847
httpAdditionalChannelInitializer = config.getHttpAdditionalChannelInitializer();
828848
wsAdditionalChannelInitializer = config.getWsAdditionalChannelInitializer();
829849
responseBodyPartFactory = config.getResponseBodyPartFactory();
@@ -1148,6 +1168,11 @@ public Builder setThreadPoolName(String threadPoolName) {
11481168
return this;
11491169
}
11501170

1171+
public Builder setChannelThreadPoolName(String channelThreadPoolName) {
1172+
this.channelThreadPoolName = channelThreadPoolName;
1173+
return this;
1174+
}
1175+
11511176
public Builder setHttpClientCodecMaxInitialLineLength(int httpClientCodecMaxInitialLineLength) {
11521177
this.httpClientCodecMaxInitialLineLength = httpClientCodecMaxInitialLineLength;
11531178
return this;
@@ -1204,6 +1229,11 @@ public Builder setThreadFactory(ThreadFactory threadFactory) {
12041229
return this;
12051230
}
12061231

1232+
public Builder setChannelThreadFactory(ThreadFactory channelThreadFactory) {
1233+
this.channelThreadFactory = channelThreadFactory;
1234+
return this;
1235+
}
1236+
12071237
public Builder setHttpAdditionalChannelInitializer(Consumer<Channel> httpAdditionalChannelInitializer) {
12081238
this.httpAdditionalChannelInitializer = httpAdditionalChannelInitializer;
12091239
return this;
@@ -1291,6 +1321,7 @@ public DefaultAsyncHttpClientConfig build() {
12911321
soSndBuf,
12921322
soRcvBuf,
12931323
threadPoolName,
1324+
channelThreadPoolName,
12941325
httpClientCodecMaxInitialLineLength,
12951326
httpClientCodecMaxHeaderSize,
12961327
httpClientCodecMaxChunkSize,
@@ -1304,6 +1335,7 @@ public DefaultAsyncHttpClientConfig build() {
13041335
allocator,
13051336
nettyTimer,
13061337
threadFactory,
1338+
channelThreadFactory,
13071339
httpAdditionalChannelInitializer,
13081340
wsAdditionalChannelInitializer,
13091341
responseBodyPartFactory,

Diff for: client/src/main/java/org/asynchttpclient/config/AsyncHttpClientConfigDefaults.java

+5
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ public final class AsyncHttpClientConfigDefaults {
2020

2121
public static final String ASYNC_CLIENT_CONFIG_ROOT = "org.asynchttpclient.";
2222
public static final String THREAD_POOL_NAME_CONFIG = "threadPoolName";
23+
public static final String CHANNEL_THREAD_POOL_NAME_CONFIG = "channelThreadPoolName";
2324
public static final String MAX_CONNECTIONS_CONFIG = "maxConnections";
2425
public static final String MAX_CONNECTIONS_PER_HOST_CONFIG = "maxConnectionsPerHost";
2526
public static final String ACQUIRE_FREE_CHANNEL_TIMEOUT = "acquireFreeChannelTimeout";
@@ -90,6 +91,10 @@ public static String defaultThreadPoolName() {
9091
return AsyncHttpClientConfigHelper.getAsyncHttpClientConfig().getString(ASYNC_CLIENT_CONFIG_ROOT + THREAD_POOL_NAME_CONFIG);
9192
}
9293

94+
public static String defaultChannelThreadPoolName() {
95+
return AsyncHttpClientConfigHelper.getAsyncHttpClientConfig().getString(ASYNC_CLIENT_CONFIG_ROOT + CHANNEL_THREAD_POOL_NAME_CONFIG);
96+
}
97+
9398
public static int defaultMaxConnections() {
9499
return AsyncHttpClientConfigHelper.getAsyncHttpClientConfig().getInt(ASYNC_CLIENT_CONFIG_ROOT + MAX_CONNECTIONS_CONFIG);
95100
}

Diff for: client/src/main/java/org/asynchttpclient/netty/channel/ChannelManager.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ public ChannelManager(final AsyncHttpClientConfig config, Timer nettyTimer) {
119119
handshakeTimeout = config.getHandshakeTimeout();
120120

121121
// check if external EventLoopGroup is defined
122-
ThreadFactory threadFactory = config.getThreadFactory() != null ? config.getThreadFactory() : new DefaultThreadFactory(config.getThreadPoolName());
122+
ThreadFactory threadFactory = config.getChannelThreadFactory() != null ? config.getChannelThreadFactory() : new DefaultThreadFactory(config.getChannelThreadPoolName());
123123
allowReleaseEventLoopGroup = config.getEventLoopGroup() == null;
124124
TransportFactory<? extends Channel, ? extends EventLoopGroup> transportFactory;
125125
if (allowReleaseEventLoopGroup) {

Diff for: client/src/main/resources/org/asynchttpclient/config/ahc-default.properties

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
org.asynchttpclient.threadPoolName=AsyncHttpClient
2+
org.asynchttpclient.channelThreadPoolName=AHC-Channel
23
org.asynchttpclient.maxConnections=-1
34
org.asynchttpclient.maxConnectionsPerHost=-1
45
org.asynchttpclient.acquireFreeChannelTimeout=0

Diff for: extras/typesafeconfig/src/main/java/org/asynchttpclient/extras/typesafeconfig/AsyncHttpClientTypesafeConfig.java

+10
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@ public String getThreadPoolName() {
5959
return getStringOpt(THREAD_POOL_NAME_CONFIG).orElse(defaultThreadPoolName());
6060
}
6161

62+
@Override
63+
public String getChannelThreadPoolName() {
64+
return getStringOpt(CHANNEL_THREAD_POOL_NAME_CONFIG).orElse(defaultChannelThreadPoolName());
65+
}
66+
6267
@Override
6368
public int getMaxConnections() {
6469
return getIntegerOpt(MAX_CONNECTIONS_CONFIG).orElse(defaultMaxConnections());
@@ -129,6 +134,11 @@ public ThreadFactory getThreadFactory() {
129134
return null;
130135
}
131136

137+
@Override
138+
public ThreadFactory getChannelThreadFactory() {
139+
return null;
140+
}
141+
132142
@Override
133143
public ProxyServerSelector getProxyServerSelector() {
134144
return ProxyServerSelector.NO_PROXY_SELECTOR;

Diff for: extras/typesafeconfig/src/test/java/org/asynchttpclient/extras/typesafeconfig/AsyncHttpClientTypesafeConfigTest.java

+4
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ public void testThreadPoolName() {
2929
test(AsyncHttpClientTypesafeConfig::getThreadPoolName, "threadPoolName", "MyHttpClient", "AsyncHttpClient");
3030
}
3131

32+
public void testChannelThreadPoolName() {
33+
test(AsyncHttpClientTypesafeConfig::getChannelThreadPoolName, "channelThreadPoolName", "MyHttpClient", "AHC-Channel");
34+
}
35+
3236
public void testMaxTotalConnections() {
3337
test(AsyncHttpClientTypesafeConfig::getMaxConnections, "maxConnections", 100, -1);
3438
}

0 commit comments

Comments
 (0)