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

Netty default maxconnections #26083

Merged
merged 6 commits into from Dec 20, 2021
Merged
Show file tree
Hide file tree
Changes from 3 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
2 changes: 2 additions & 0 deletions sdk/core/azure-core-http-netty/CHANGELOG.md
Expand Up @@ -7,6 +7,8 @@
### Breaking Changes

### Bugs Fixed
- Set default `maxConnections` value to match the default used in `reactor-netty` when `HttpClientOptions` is set but
`maximumConnectionPoolSize` is not specified.

### Other Changes

Expand Down
Expand Up @@ -12,6 +12,7 @@
* An {@link HttpClientProvider} that provides an implementation of HttpClient based on Netty.
*/
public final class NettyAsyncHttpClientProvider implements HttpClientProvider {
private static final int DEFAULT_MAX_CONNECTIONS = 500;

@Override
public HttpClient createInstance() {
Expand All @@ -36,6 +37,19 @@ public HttpClient createInstance(HttpClientOptions clientOptions) {
Integer maximumConnectionPoolSize = clientOptions.getMaximumConnectionPoolSize();
if (maximumConnectionPoolSize != null && maximumConnectionPoolSize > 0) {
connectionProviderBuilder.maxConnections(maximumConnectionPoolSize);
} else {
// reactor-netty (as of version 1.0.13) uses different default values for maxConnections when creating
// HttpClient with and without a ConnectionProvider.
// https://github.com/reactor/reactor-netty/blob/v1.0.13/reactor-netty-core/src/main/java/reactor/netty/tcp/TcpResources.java#L398

// HttpClient.create() uses 500 connections (called when HttpClientOptions is not set)
// HttpClient.create(ConnectionProvider) uses maxAvailableProcessors * 2 (minimum of 16) (called
// when HttpClientOptions is set). This number can be very small depending on the host on which
// applications run and can lead to issues like this - https://github.com/Azure/azure-sdk-for-java/issues/26027

// So, we need to unfortunately hardcode the maxConnections to 500 (when user doesn't set it) to have
// consistent configuration whether or not HttpClientOptions is set.
connectionProviderBuilder.maxConnections(DEFAULT_MAX_CONNECTIONS);
}

builder = builder.connectionProvider(connectionProviderBuilder.build());
Expand Down
Expand Up @@ -6,6 +6,7 @@
import com.azure.core.http.ProxyOptions;
import com.azure.core.util.Configuration;
import com.azure.core.util.HttpClientOptions;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import reactor.netty.transport.ProxyProvider;

Expand Down Expand Up @@ -82,4 +83,20 @@ public void optionsWithTimeouts() {
assertEquals(expectedTimeout, httpClient.responseTimeout);
assertEquals(expectedTimeout, httpClient.readTimeout);
}

@Test
@Disabled("Due to a bug in reactor-netty that doesn't read maxConnections value from implementation")
public void testDefaultMaxConnections() {
NettyAsyncHttpClient httpClient = (NettyAsyncHttpClient) new NettyAsyncHttpClientProvider()
.createInstance(null);
int actualMaxConnections = httpClient.nettyClient.configuration().connectionProvider().maxConnections();
// There's a bug in reactor-netty that doesn't read the `maxConnections from the implementation of
// ConnectionProvider. It reads from the default implementation in the interface which always returns -1.
// assertEquals(500, actualMaxConnections);

httpClient = (NettyAsyncHttpClient) new NettyAsyncHttpClientProvider()
.createInstance(new HttpClientOptions());
actualMaxConnections = httpClient.nettyClient.configuration().connectionProvider().maxConnections();
// assertEquals(500, actualMaxConnections);
}
}