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
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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.
*
@@ -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}
*
Original file line number Diff line number Diff line change
@@ -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;
@@ -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;
@@ -199,6 +201,7 @@ private DefaultAsyncHttpClientConfig(// http

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

// internals
this.threadPoolName = threadPoolName;
this.channelThreadPoolName = channelThreadPoolName;
this.httpClientCodecMaxInitialLineLength = httpClientCodecMaxInitialLineLength;
this.httpClientCodecMaxHeaderSize = httpClientCodecMaxHeaderSize;
this.httpClientCodecMaxChunkSize = httpClientCodecMaxChunkSize;
@@ -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;
@@ -581,6 +587,11 @@ public String getThreadPoolName() {
return threadPoolName;
}

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

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

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

@Override
public Consumer<Channel> getHttpAdditionalChannelInitializer() {
return httpAdditionalChannelInitializer;
@@ -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();
@@ -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;
@@ -814,6 +832,7 @@ public Builder(AsyncHttpClientConfig config) {

// internals
threadPoolName = config.getThreadPoolName();
channelThreadPoolName = config.getChannelThreadPoolName();
httpClientCodecMaxInitialLineLength = config.getHttpClientCodecMaxInitialLineLength();
httpClientCodecMaxHeaderSize = config.getHttpClientCodecMaxHeaderSize();
httpClientCodecMaxChunkSize = config.getHttpClientCodecMaxChunkSize();
@@ -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();
@@ -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;
@@ -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;
@@ -1291,6 +1321,7 @@ public DefaultAsyncHttpClientConfig build() {
soSndBuf,
soRcvBuf,
threadPoolName,
channelThreadPoolName,
httpClientCodecMaxInitialLineLength,
httpClientCodecMaxHeaderSize,
httpClientCodecMaxChunkSize,
@@ -1304,6 +1335,7 @@ public DefaultAsyncHttpClientConfig build() {
allocator,
nettyTimer,
threadFactory,
channelThreadFactory,
httpAdditionalChannelInitializer,
wsAdditionalChannelInitializer,
responseBodyPartFactory,
Original file line number Diff line number Diff line change
@@ -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";
@@ -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);
}
Original file line number Diff line number Diff line change
@@ -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) {
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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());
@@ -129,6 +134,11 @@ public ThreadFactory getThreadFactory() {
return null;
}

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

@Override
public ProxyServerSelector getProxyServerSelector() {
return ProxyServerSelector.NO_PROXY_SELECTOR;
Original file line number Diff line number Diff line change
@@ -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);
}