Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
60 commits
Select commit Hold shift + click to select a range
1ddd05d
Http-netty4 connection pool implementation
georgebanasios Jun 28, 2025
a379a00
Merge remote-tracking branch 'origin/main' into connection-pool-http-…
georgebanasios Jun 29, 2025
784ade2
fix duplicate http response handler in the pipeline
georgebanasios Jun 29, 2025
7edd7a9
apply ALPN after the correct handler
georgebanasios Jun 29, 2025
ce50c9e
fix formatting
georgebanasios Jun 30, 2025
6b73bb3
fix Netty4ChannelBinaryData
georgebanasios Jun 30, 2025
c8693a1
fix connection pool release race condition
georgebanasios Jun 30, 2025
e1525d8
adjust chunked request
georgebanasios Jun 30, 2025
f26b6c3
Revert "adjust chunked request"
georgebanasios Jun 30, 2025
1602d4b
fix proxy logic when acquiring a channel
georgebanasios Jul 1, 2025
989b302
cleanup the pipeline after the last content
georgebanasios Jul 1, 2025
f0a7847
create channel wrapper
georgebanasios Jul 1, 2025
c8c648b
remove channel closure from response handler
georgebanasios Jul 1, 2025
8dea326
try and fix the release deadlock
georgebanasios Jul 1, 2025
e076602
handle case where read handlers are added to a closed channel
georgebanasios Jul 2, 2025
8de78bd
remove synchronized
georgebanasios Jul 2, 2025
ed27af7
apply ALPN only on new connections
georgebanasios Jul 2, 2025
51fa938
try and fix deadlock
georgebanasios Jul 2, 2025
f3eee88
Revert "try and fix deadlock"
georgebanasios Jul 2, 2025
61c273b
deadlock fix v2
georgebanasios Jul 3, 2025
40b929a
Revert "deadlock fix v2"
georgebanasios Jul 3, 2025
792a06e
deadlock fix v3
georgebanasios Jul 3, 2025
56bbb97
Revert "deadlock fix v3"
georgebanasios Jul 3, 2025
f849ec8
deadlock fix attempt v4
georgebanasios Jul 4, 2025
140a095
fix on input stream close method
georgebanasios Jul 4, 2025
c0f9beb
deadlock fix attempt v5
georgebanasios Jul 4, 2025
a2d0bcd
Revert "deadlock fix attempt v5"
georgebanasios Jul 4, 2025
d66fd1d
deadlock fix attempt v6
georgebanasios Jul 4, 2025
42f72c7
make method more efficient
georgebanasios Jul 5, 2025
4606fe4
fix response handler tests
georgebanasios Jul 5, 2025
6fdf2e7
make close method of netty binary data non blocking
georgebanasios Jul 5, 2025
623503c
cleanup connection on netty binary data
georgebanasios Jul 6, 2025
f085ece
add exception handling on cleanup handler
georgebanasios Jul 6, 2025
d66d154
Netty4ChannelBinaryData adjustments
georgebanasios Jul 6, 2025
aa1fba9
try to not block on send method
georgebanasios Jul 7, 2025
720b395
disable connection pooling functionality
georgebanasios Jul 8, 2025
670687f
adjust cleanups
georgebanasios Jul 9, 2025
c0022f4
use event for pipeline cleanup
georgebanasios Jul 9, 2025
7b3c8fe
attempt to synchronize the pipeline modification and cleaup for the s…
georgebanasios Jul 10, 2025
1a28409
self removal of handlers
georgebanasios Jul 10, 2025
ed9b857
attempt to fix race condition
georgebanasios Jul 10, 2025
fec1ffc
close clients after tests
georgebanasios Jul 13, 2025
ca36245
Revert "close clients after tests"
georgebanasios Jul 13, 2025
437616b
http2 connecetions
georgebanasios Jul 13, 2025
40ef6ee
synchronize connection pool
georgebanasios Jul 13, 2025
59f9240
Revert "synchronize connection pool"
georgebanasios Jul 14, 2025
9e65d78
check connection
georgebanasios Jul 16, 2025
84be996
Revert "check connection"
georgebanasios Jul 16, 2025
e092c9c
try to centralize waiting queue logic
georgebanasios Jul 16, 2025
df003e0
Revert "try to centralize waiting queue logic"
georgebanasios Jul 16, 2025
dbf1c69
enable half closure
georgebanasios Jul 16, 2025
9a3477b
add lock on acquire/release
georgebanasios Jul 16, 2025
6855d45
documentation fix & pr comments
georgebanasios Jul 19, 2025
814285d
fix ssl/alpn on pooled connections
georgebanasios Jul 21, 2025
91f72e0
Merge remote-tracking branch 'origin/main' into connection-pool-http-…
georgebanasios Jul 22, 2025
879c638
remove duplicate line & javadoc fix
georgebanasios Jul 22, 2025
d4694d8
add javadoc on netty provider
georgebanasios Jul 23, 2025
61be579
add HTTP client tests for pooled connections
georgebanasios Jul 23, 2025
5551d47
fix builder logging configuration logic
georgebanasios Jul 23, 2025
c64019a
fix merge conflicts
georgebanasios Aug 1, 2025
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
Prev Previous commit
Next Next commit
disable connection pooling functionality
  • Loading branch information
georgebanasios committed Jul 9, 2025
commit 720b3953e95a9938acec54aea21902b1cd7f4699
6 changes: 6 additions & 0 deletions sdk/clientcore/http-netty4/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,12 @@
<version>2.5.2</version> <!-- {x-version-update;org.conscrypt:conscrypt-openjdk-uber;external_dependency} -->
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>4.11.0</version> <!-- {x-version-update;org.mockito:mockito-core;external_dependency} -->
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
7 changes: 7 additions & 0 deletions sdk/clientcore/http-netty4/spotbugs-exclude.xml
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,11 @@
<Bug pattern="RV_RETURN_VALUE_IGNORED_BAD_PRACTICE" />
<Class name="io.clientcore.http.netty4.implementation.Netty4ChannelInputStream" />
</Match>
<Match>
<Bug pattern="DP_DO_INSIDE_DO_PRIVILEGED" />
<Or>
<Class name="io.clientcore.http.netty4.NettyHttpClientBuilderTests" />
<Class name="io.clientcore.http.netty4.implementation.Netty4ConnectionPoolTests" />
</Or>
</Match>
</FindBugsFilter>

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -293,15 +293,14 @@ public NettyHttpClientBuilder maximumHttpVersion(HttpProtocolVersion httpVersion
* Sets the maximum number of connections allowed per remote address in the connection pool.
* <p>
* If not set, a default value of 500 is used.
* <p>
* <strong>A value of {@code 0} or less will disable connection pooling, and hence each request will
* get a newly created connection.</strong>
*
* @param connectionPoolSize The maximum number of connections. Must be greater than or equal to 0.
* @param connectionPoolSize The maximum number of connections.
* @return The updated builder.
*/
public NettyHttpClientBuilder connectionPoolSize(int connectionPoolSize) {
if (connectionPoolSize < 0) {
throw LOGGER.throwableAtError()
.log("connectionPoolSize must be greater than or equal to 0", IllegalArgumentException::new);
}
this.connectionPoolSize = connectionPoolSize;
return this;
}
Expand Down Expand Up @@ -404,14 +403,17 @@ public HttpClient build() {
ProxyOptions buildProxyOptions
= (proxyOptions == null) ? ProxyOptions.fromConfiguration(buildConfiguration, true) : proxyOptions;

Netty4ConnectionPool connectionPool
= new Netty4ConnectionPool(bootstrap, new ChannelInitializationProxyHandler(buildProxyOptions),
sslContextModifier, connectionPoolSize, connectionIdleTimeout, maxConnectionLifetime,
pendingAcquireTimeout, maxPendingAcquires, maximumHttpVersion);
Netty4ConnectionPool connectionPool = null;
if (connectionPoolSize > 0) {
connectionPool
= new Netty4ConnectionPool(bootstrap, new ChannelInitializationProxyHandler(buildProxyOptions),
sslContextModifier, connectionPoolSize, connectionIdleTimeout, maxConnectionLifetime,
pendingAcquireTimeout, maxPendingAcquires, maximumHttpVersion);
}

return new NettyHttpClient(group, connectionPool, buildProxyOptions,
new ChannelInitializationProxyHandler(buildProxyOptions), getTimeoutMillis(readTimeout),
getTimeoutMillis(responseTimeout), getTimeoutMillis(writeTimeout));
return new NettyHttpClient(bootstrap, group, connectionPool, buildProxyOptions,
new ChannelInitializationProxyHandler(buildProxyOptions), sslContextModifier, maximumHttpVersion,
getTimeoutMillis(readTimeout), getTimeoutMillis(responseTimeout), getTimeoutMillis(writeTimeout));
}

static long getTimeoutMillis(Duration duration) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,10 @@ public void close() {
// We need to drain it to ensure the connection can be safely reused.
if (!streamDrained.get()) {
drainAndCleanupAsync();
} else {
eagerContent = null;
channel.disconnect();
channel.close();
}
}
}
Expand Down Expand Up @@ -241,7 +245,6 @@ private void drainStream() {
bytes = eagerContent.toByteArray();
}
} finally {
closed.set(true);
drainLatch.countDown();
eagerContent = null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,9 @@
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.EventLoopGroup;
import io.netty.handler.codec.http2.Http2SecurityUtil;
import io.netty.handler.proxy.ProxyHandler;
import io.netty.handler.ssl.ApplicationProtocolConfig;
import io.netty.handler.ssl.ApplicationProtocolNames;
import io.netty.handler.ssl.SslContext;
import io.netty.handler.ssl.SslContextBuilder;
import io.netty.handler.ssl.SslProvider;
import io.netty.handler.ssl.SupportedCipherSuiteFilter;
import io.netty.util.AttributeKey;
import io.netty.util.concurrent.Future;
import io.netty.util.concurrent.Promise;
Expand All @@ -48,14 +43,16 @@
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Consumer;

import static io.clientcore.http.netty4.implementation.Netty4HandlerNames.CONNECTION_SETUP_ERROR;
import static io.clientcore.http.netty4.implementation.Netty4HandlerNames.PROXY;
import static io.clientcore.http.netty4.implementation.Netty4HandlerNames.SSL;
import static io.clientcore.http.netty4.implementation.Netty4HandlerNames.SSL_INITIALIZER;
import static io.clientcore.http.netty4.implementation.Netty4Utility.buildSslContext;

/**
* A pool of Netty channels that can be reused for requests to the same remote address.
*/
public final class Netty4ConnectionPool implements Closeable {
public class Netty4ConnectionPool implements Closeable {

private static final AttributeKey<PooledConnection> POOLED_CONNECTION_KEY
= AttributeKey.valueOf("pooled-connection-key");
Expand Down Expand Up @@ -373,7 +370,9 @@ public void initChannel(Channel channel) throws SSLException {
ProxyHandler proxyHandler = channelInitializationProxyHandler.createProxy(proxyChallenges);
pipeline.addFirst(PROXY, proxyHandler);

pipeline.addLast("proxy-exception-handler", new ChannelInboundHandlerAdapter() {
// This handler's only job is to catch any exception during the
// connection setup (proxying, SSL handshake) and fail the promise.
pipeline.addLast(CONNECTION_SETUP_ERROR, new ChannelInboundHandlerAdapter() {
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
promise.tryFailure(cause);
Expand All @@ -385,7 +384,7 @@ public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
// Add SSL handling if the request is HTTPS.
if (isHttps) {
InetSocketAddress inetSocketAddress = (InetSocketAddress) key.getFinalDestination();
SslContext ssl = buildSslContext();
SslContext ssl = buildSslContext(maximumHttpVersion, sslContextModifier);
// SSL handling is added last here. This is done as proxying could require SSL handling too.
channel.pipeline()
.addLast(SSL, ssl.newHandler(channel.alloc(), inetSocketAddress.getHostString(),
Expand Down Expand Up @@ -419,11 +418,15 @@ public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
return;
}
ChannelPipeline pipeline = newChannel.pipeline();
if (pipeline.get("proxy-exception-handler") != null) {
pipeline.remove("proxy-exception-handler");
if (pipeline.get(CONNECTION_SETUP_ERROR) != null) {
pipeline.remove(CONNECTION_SETUP_ERROR);
}
promise.setSuccess(newChannel);
} else {
ChannelPipeline pipeline = newChannel.pipeline();
if (pipeline.get(CONNECTION_SETUP_ERROR) != null) {
pipeline.remove(CONNECTION_SETUP_ERROR);
}
promise.setFailure(proxyFuture.cause());
newChannel.close();
activeConnections.decrementAndGet();
Expand All @@ -437,37 +440,6 @@ public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
return promise;
}

private SslContext buildSslContext() throws SSLException {
SslContextBuilder sslContextBuilder
= SslContextBuilder.forClient().endpointIdentificationAlgorithm("HTTPS");
if (maximumHttpVersion == HttpProtocolVersion.HTTP_2) {
// If HTTP/2 is the maximum version, we need to ensure that ALPN is enabled.
SslProvider sslProvider = SslContext.defaultClientProvider();
ApplicationProtocolConfig.SelectorFailureBehavior selectorBehavior;
ApplicationProtocolConfig.SelectedListenerFailureBehavior selectedBehavior;
if (sslProvider == SslProvider.JDK) {
selectorBehavior = ApplicationProtocolConfig.SelectorFailureBehavior.FATAL_ALERT;
selectedBehavior = ApplicationProtocolConfig.SelectedListenerFailureBehavior.FATAL_ALERT;
} else {
// Netty OpenSslContext doesn't support FATAL_ALERT, use NO_ADVERTISE and ACCEPT
// instead.
selectorBehavior = ApplicationProtocolConfig.SelectorFailureBehavior.NO_ADVERTISE;
selectedBehavior = ApplicationProtocolConfig.SelectedListenerFailureBehavior.ACCEPT;
}

sslContextBuilder.ciphers(Http2SecurityUtil.CIPHERS, SupportedCipherSuiteFilter.INSTANCE)
.applicationProtocolConfig(
new ApplicationProtocolConfig(ApplicationProtocolConfig.Protocol.ALPN, selectorBehavior,
selectedBehavior, ApplicationProtocolNames.HTTP_2, ApplicationProtocolNames.HTTP_1_1));
}
if (sslContextModifier != null) {
// Allow the caller to modify the SslContextBuilder before it is built.
sslContextModifier.accept(sslContextBuilder);
}

return sslContextBuilder.build();
}

private boolean isHealthy(PooledConnection connection) {
if (!connection.isActiveAndWriteable()) {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ public final class Netty4HandlerNames {
*/
public static final String PIPELINE_CLEANUP = "clientcore.pipelinecleanup";

public static final String CONNECTION_SETUP_ERROR = "clientcore.connectionsetuperror";

private Netty4HandlerNames() {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public void cleanup(ChannelHandlerContext ctx, boolean closeChannel) {
pipeline.remove(this);
}

if (closeChannel || !ctx.channel().isActive()) {
if (closeChannel || !ctx.channel().isActive() || connectionPool == null) {
ctx.channel().close();
} else {
connectionPool.release(ctx.channel());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,23 @@
import io.netty.handler.codec.http2.DelegatingDecompressorFrameListener;
import io.netty.handler.codec.http2.Http2Connection;
import io.netty.handler.codec.http2.Http2FrameListener;
import io.netty.handler.codec.http2.Http2SecurityUtil;
import io.netty.handler.codec.http2.Http2Settings;
import io.netty.handler.codec.http2.HttpToHttp2ConnectionHandlerBuilder;
import io.netty.handler.codec.http2.InboundHttp2ToHttpAdapterBuilder;
import io.netty.handler.ssl.ApplicationProtocolConfig;
import io.netty.handler.ssl.ApplicationProtocolNames;
import io.netty.handler.ssl.SslContext;
import io.netty.handler.ssl.SslContextBuilder;
import io.netty.handler.ssl.SslProvider;
import io.netty.handler.ssl.SupportedCipherSuiteFilter;
import io.netty.handler.stream.ChunkedInput;
import io.netty.handler.stream.ChunkedNioFile;
import io.netty.handler.stream.ChunkedStream;
import io.netty.handler.stream.ChunkedWriteHandler;
import io.netty.util.Version;

import javax.net.ssl.SSLException;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.channels.FileChannel;
Expand All @@ -58,6 +66,7 @@
import java.util.Objects;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Consumer;

import static io.netty.handler.codec.http.DefaultHttpHeadersFactory.trailersFactory;

Expand Down Expand Up @@ -554,6 +563,37 @@ private static io.netty.handler.codec.http.HttpRequest toNettyHttpRequest(HttpRe
}
}

public static SslContext buildSslContext(HttpProtocolVersion maximumHttpVersion,
Consumer<SslContextBuilder> sslContextModifier) throws SSLException {
SslContextBuilder sslContextBuilder = SslContextBuilder.forClient().endpointIdentificationAlgorithm("HTTPS");
if (maximumHttpVersion == HttpProtocolVersion.HTTP_2) {
// If HTTP/2 is the maximum version, we need to ensure that ALPN is enabled.
SslProvider sslProvider = SslContext.defaultClientProvider();
ApplicationProtocolConfig.SelectorFailureBehavior selectorBehavior;
ApplicationProtocolConfig.SelectedListenerFailureBehavior selectedBehavior;
if (sslProvider == SslProvider.JDK) {
selectorBehavior = ApplicationProtocolConfig.SelectorFailureBehavior.FATAL_ALERT;
selectedBehavior = ApplicationProtocolConfig.SelectedListenerFailureBehavior.FATAL_ALERT;
} else {
// Netty OpenSslContext doesn't support FATAL_ALERT, use NO_ADVERTISE and ACCEPT
// instead.
selectorBehavior = ApplicationProtocolConfig.SelectorFailureBehavior.NO_ADVERTISE;
selectedBehavior = ApplicationProtocolConfig.SelectedListenerFailureBehavior.ACCEPT;
}

sslContextBuilder.ciphers(Http2SecurityUtil.CIPHERS, SupportedCipherSuiteFilter.INSTANCE)
.applicationProtocolConfig(
new ApplicationProtocolConfig(ApplicationProtocolConfig.Protocol.ALPN, selectorBehavior,
selectedBehavior, ApplicationProtocolNames.HTTP_2, ApplicationProtocolNames.HTTP_1_1));
}
if (sslContextModifier != null) {
// Allow the caller to modify the SslContextBuilder before it is built.
sslContextModifier.accept(sslContextBuilder);
}

return sslContextBuilder.build();
}

private Netty4Utility() {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

package io.clientcore.http.netty4;

import io.clientcore.core.http.client.HttpProtocolVersion;
import io.clientcore.core.http.models.HttpMethod;
import io.clientcore.core.http.models.HttpRequest;
import io.clientcore.core.http.models.ProxyOptions;
Expand Down Expand Up @@ -32,6 +33,7 @@
import java.io.IOException;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.reflect.Field;
import java.net.InetSocketAddress;
import java.time.Duration;
import java.util.ArrayList;
Expand All @@ -47,6 +49,7 @@
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertThrows;

/**
* Tests {@link NettyHttpClientBuilder}.
Expand Down Expand Up @@ -372,6 +375,35 @@ public void getEventLoopGroupToUse(Class<?> expected, EventLoopGroup configuredG
assertInstanceOf(expected, eventLoopGroup);
}

@Test
public void buildNettyClientWithoutConnectionPool() throws NoSuchFieldException, IllegalAccessException {
NettyHttpClient client = (NettyHttpClient) new NettyHttpClientBuilder().connectionPoolSize(0).build();

Field connectionPoolField = NettyHttpClient.class.getDeclaredField("connectionPool");
connectionPoolField.setAccessible(true);
assertNull(connectionPoolField.get(client), "Connection pool should be null when pool size is 0.");
}

@Test
public void testInvalidMaxPendingAcquires() {
NettyHttpClientBuilder builder = new NettyHttpClientBuilder();
assertThrows(IllegalArgumentException.class, () -> builder.maxPendingAcquires(0));
assertThrows(IllegalArgumentException.class, () -> builder.maxPendingAcquires(-1));
}

@Test
public void testMaximumHttpVersion() throws NoSuchFieldException, IllegalAccessException {
NettyHttpClientBuilder builder = new NettyHttpClientBuilder();

NettyHttpClient clientv1 = (NettyHttpClient) builder.maximumHttpVersion(HttpProtocolVersion.HTTP_1_1).build();
Field httpVersionField = NettyHttpClient.class.getDeclaredField("maximumHttpVersion");
httpVersionField.setAccessible(true);
assertEquals(HttpProtocolVersion.HTTP_1_1, httpVersionField.get(clientv1));

NettyHttpClient clientv2 = (NettyHttpClient) builder.maximumHttpVersion(null).build();
assertEquals(HttpProtocolVersion.HTTP_2, httpVersionField.get(clientv2));
}

private static Stream<Arguments> getEventLoopGroupToUseSupplier() throws ReflectiveOperationException {
// Doesn't matter what this is calling, just needs to throw an exception.
// This will as it doesn't accept the arguments that it will be called with.
Expand Down
Loading
Loading