Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Channel thread factory #1584 #1687

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -51,6 +51,13 @@ public interface AsyncHttpClientConfig {
*/
String getThreadPoolName();

/**
* Return the name of threadPool for {@link org.asynchttpclient.netty.channel.ChannelManager}, which is used for thread naming and debugging.
*
* @return the name.
*/
String getChannelThreadPoolName();

/**
* Return the maximum number of connections an {@link AsyncHttpClient} can handle.
*
Expand Down Expand Up @@ -149,6 +156,14 @@ public interface AsyncHttpClientConfig {
*/
ThreadFactory getThreadFactory();

/**
* Return the {@link java.util.concurrent.ThreadFactory} an {@link org.asynchttpclient.netty.channel.ChannelManager} use for handling I/O.
*
* @return the {@link java.util.concurrent.ThreadFactory} an {@link org.asynchttpclient.netty.channel.ChannelManager} use for handling I/O.
* If no {@link ThreadFactory} has been explicitly provided, this method will return <code>null</code>
*/
ThreadFactory getChannelThreadFactory();

/**
* An instance of {@link ProxyServer} used by an {@link AsyncHttpClient}
*
Expand Down
Expand Up @@ -112,6 +112,7 @@ public class DefaultAsyncHttpClientConfig implements AsyncHttpClientConfig {

// internals
private final String threadPoolName;
private final String channelThreadPoolName;
private final int httpClientCodecMaxInitialLineLength;
private final int httpClientCodecMaxHeaderSize;
private final int httpClientCodecMaxChunkSize;
Expand All @@ -128,6 +129,7 @@ public class DefaultAsyncHttpClientConfig implements AsyncHttpClientConfig {
private final int soRcvBuf;
private final Timer nettyTimer;
private final ThreadFactory threadFactory;
private final ThreadFactory channelThreadFactory;
private final Consumer<Channel> httpAdditionalChannelInitializer;
private final Consumer<Channel> wsAdditionalChannelInitializer;
private final ResponseBodyPartFactory responseBodyPartFactory;
Expand Down Expand Up @@ -199,6 +201,7 @@ private DefaultAsyncHttpClientConfig(// http

// internals
String threadPoolName,
String channelThreadPoolName,
int httpClientCodecMaxInitialLineLength,
int httpClientCodecMaxHeaderSize,
int httpClientCodecMaxChunkSize,
Expand All @@ -212,6 +215,7 @@ private DefaultAsyncHttpClientConfig(// http
ByteBufAllocator allocator,
Timer nettyTimer,
ThreadFactory threadFactory,
ThreadFactory channelThreadFactory,
Consumer<Channel> httpAdditionalChannelInitializer,
Consumer<Channel> wsAdditionalChannelInitializer,
ResponseBodyPartFactory responseBodyPartFactory,
Expand Down Expand Up @@ -287,6 +291,7 @@ private DefaultAsyncHttpClientConfig(// http

// internals
this.threadPoolName = threadPoolName;
this.channelThreadPoolName = channelThreadPoolName;
this.httpClientCodecMaxInitialLineLength = httpClientCodecMaxInitialLineLength;
this.httpClientCodecMaxHeaderSize = httpClientCodecMaxHeaderSize;
this.httpClientCodecMaxChunkSize = httpClientCodecMaxChunkSize;
Expand All @@ -298,6 +303,7 @@ private DefaultAsyncHttpClientConfig(// http
this.allocator = allocator;
this.nettyTimer = nettyTimer;
this.threadFactory = threadFactory;
this.channelThreadFactory = channelThreadFactory;
this.httpAdditionalChannelInitializer = httpAdditionalChannelInitializer;
this.wsAdditionalChannelInitializer = wsAdditionalChannelInitializer;
this.responseBodyPartFactory = responseBodyPartFactory;
Expand Down Expand Up @@ -581,6 +587,11 @@ public String getThreadPoolName() {
return threadPoolName;
}

@Override
public String getChannelThreadPoolName() {
return channelThreadPoolName;
}

@Override
public int getHttpClientCodecMaxInitialLineLength() {
return httpClientCodecMaxInitialLineLength;
Expand Down Expand Up @@ -636,6 +647,11 @@ public ThreadFactory getThreadFactory() {
return threadFactory;
}

@Override
public ThreadFactory getChannelThreadFactory() {
return channelThreadFactory;
}

@Override
public Consumer<Channel> getHttpAdditionalChannelInitializer() {
return httpAdditionalChannelInitializer;
Expand Down Expand Up @@ -732,6 +748,7 @@ public static class Builder {

// internals
private String threadPoolName = defaultThreadPoolName();
private String channelThreadPoolName = defaultChannelThreadPoolName();
private int httpClientCodecMaxInitialLineLength = defaultHttpClientCodecMaxInitialLineLength();
private int httpClientCodecMaxHeaderSize = defaultHttpClientCodecMaxHeaderSize();
private int httpClientCodecMaxChunkSize = defaultHttpClientCodecMaxChunkSize();
Expand All @@ -743,6 +760,7 @@ public static class Builder {
private EventLoopGroup eventLoopGroup;
private Timer nettyTimer;
private ThreadFactory threadFactory;
private ThreadFactory channelThreadFactory;
private Consumer<Channel> httpAdditionalChannelInitializer;
private Consumer<Channel> wsAdditionalChannelInitializer;
private ResponseBodyPartFactory responseBodyPartFactory = ResponseBodyPartFactory.EAGER;
Expand Down Expand Up @@ -814,6 +832,7 @@ public Builder(AsyncHttpClientConfig config) {

// internals
threadPoolName = config.getThreadPoolName();
channelThreadPoolName = config.getChannelThreadPoolName();
httpClientCodecMaxInitialLineLength = config.getHttpClientCodecMaxInitialLineLength();
httpClientCodecMaxHeaderSize = config.getHttpClientCodecMaxHeaderSize();
httpClientCodecMaxChunkSize = config.getHttpClientCodecMaxChunkSize();
Expand All @@ -824,6 +843,7 @@ public Builder(AsyncHttpClientConfig config) {
allocator = config.getAllocator();
nettyTimer = config.getNettyTimer();
threadFactory = config.getThreadFactory();
channelThreadFactory = config.getChannelThreadFactory();
httpAdditionalChannelInitializer = config.getHttpAdditionalChannelInitializer();
wsAdditionalChannelInitializer = config.getWsAdditionalChannelInitializer();
responseBodyPartFactory = config.getResponseBodyPartFactory();
Expand Down Expand Up @@ -1148,6 +1168,11 @@ public Builder setThreadPoolName(String threadPoolName) {
return this;
}

public Builder setChannelThreadPoolName(String channelThreadPoolName) {
this.channelThreadPoolName = channelThreadPoolName;
return this;
}

public Builder setHttpClientCodecMaxInitialLineLength(int httpClientCodecMaxInitialLineLength) {
this.httpClientCodecMaxInitialLineLength = httpClientCodecMaxInitialLineLength;
return this;
Expand Down Expand Up @@ -1204,6 +1229,11 @@ public Builder setThreadFactory(ThreadFactory threadFactory) {
return this;
}

public Builder setChannelThreadFactory(ThreadFactory channelThreadFactory) {
this.channelThreadFactory = channelThreadFactory;
return this;
}

public Builder setHttpAdditionalChannelInitializer(Consumer<Channel> httpAdditionalChannelInitializer) {
this.httpAdditionalChannelInitializer = httpAdditionalChannelInitializer;
return this;
Expand Down Expand Up @@ -1291,6 +1321,7 @@ public DefaultAsyncHttpClientConfig build() {
soSndBuf,
soRcvBuf,
threadPoolName,
channelThreadPoolName,
httpClientCodecMaxInitialLineLength,
httpClientCodecMaxHeaderSize,
httpClientCodecMaxChunkSize,
Expand All @@ -1304,6 +1335,7 @@ public DefaultAsyncHttpClientConfig build() {
allocator,
nettyTimer,
threadFactory,
channelThreadFactory,
httpAdditionalChannelInitializer,
wsAdditionalChannelInitializer,
responseBodyPartFactory,
Expand Down
Expand Up @@ -20,6 +20,7 @@ public final class AsyncHttpClientConfigDefaults {

public static final String ASYNC_CLIENT_CONFIG_ROOT = "org.asynchttpclient.";
public static final String THREAD_POOL_NAME_CONFIG = "threadPoolName";
public static final String CHANNEL_THREAD_POOL_NAME_CONFIG = "channelThreadPoolName";
public static final String MAX_CONNECTIONS_CONFIG = "maxConnections";
public static final String MAX_CONNECTIONS_PER_HOST_CONFIG = "maxConnectionsPerHost";
public static final String ACQUIRE_FREE_CHANNEL_TIMEOUT = "acquireFreeChannelTimeout";
Expand Down Expand Up @@ -90,6 +91,10 @@ public static String defaultThreadPoolName() {
return AsyncHttpClientConfigHelper.getAsyncHttpClientConfig().getString(ASYNC_CLIENT_CONFIG_ROOT + THREAD_POOL_NAME_CONFIG);
}

public static String defaultChannelThreadPoolName() {
return AsyncHttpClientConfigHelper.getAsyncHttpClientConfig().getString(ASYNC_CLIENT_CONFIG_ROOT + CHANNEL_THREAD_POOL_NAME_CONFIG);
}

public static int defaultMaxConnections() {
return AsyncHttpClientConfigHelper.getAsyncHttpClientConfig().getInt(ASYNC_CLIENT_CONFIG_ROOT + MAX_CONNECTIONS_CONFIG);
}
Expand Down
Expand Up @@ -119,7 +119,7 @@ public ChannelManager(final AsyncHttpClientConfig config, Timer nettyTimer) {
handshakeTimeout = config.getHandshakeTimeout();

// check if external EventLoopGroup is defined
ThreadFactory threadFactory = config.getThreadFactory() != null ? config.getThreadFactory() : new DefaultThreadFactory(config.getThreadPoolName());
ThreadFactory threadFactory = config.getChannelThreadFactory() != null ? config.getChannelThreadFactory() : new DefaultThreadFactory(config.getChannelThreadPoolName());
allowReleaseEventLoopGroup = config.getEventLoopGroup() == null;
TransportFactory<? extends Channel, ? extends EventLoopGroup> transportFactory;
if (allowReleaseEventLoopGroup) {
Expand Down
@@ -1,4 +1,5 @@
org.asynchttpclient.threadPoolName=AsyncHttpClient
org.asynchttpclient.channelThreadPoolName=AHC-Channel
org.asynchttpclient.maxConnections=-1
org.asynchttpclient.maxConnectionsPerHost=-1
org.asynchttpclient.acquireFreeChannelTimeout=0
Expand Down
Expand Up @@ -59,6 +59,11 @@ public String getThreadPoolName() {
return getStringOpt(THREAD_POOL_NAME_CONFIG).orElse(defaultThreadPoolName());
}

@Override
public String getChannelThreadPoolName() {
return getStringOpt(CHANNEL_THREAD_POOL_NAME_CONFIG).orElse(defaultChannelThreadPoolName());
}

@Override
public int getMaxConnections() {
return getIntegerOpt(MAX_CONNECTIONS_CONFIG).orElse(defaultMaxConnections());
Expand Down Expand Up @@ -129,6 +134,11 @@ public ThreadFactory getThreadFactory() {
return null;
}

@Override
public ThreadFactory getChannelThreadFactory() {
return null;
}

@Override
public ProxyServerSelector getProxyServerSelector() {
return ProxyServerSelector.NO_PROXY_SELECTOR;
Expand Down
Expand Up @@ -29,6 +29,10 @@ public void testThreadPoolName() {
test(AsyncHttpClientTypesafeConfig::getThreadPoolName, "threadPoolName", "MyHttpClient", "AsyncHttpClient");
}

public void testChannelThreadPoolName() {
test(AsyncHttpClientTypesafeConfig::getChannelThreadPoolName, "channelThreadPoolName", "MyHttpClient", "AHC-Channel");
}

public void testMaxTotalConnections() {
test(AsyncHttpClientTypesafeConfig::getMaxConnections, "maxConnections", 100, -1);
}
Expand Down