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

Find HttpConnectionProvider in HttpClientConfig (#2468) #2468

Closed
Closed
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 @@ -83,7 +83,7 @@ class HttpClientConnect extends HttpClient {

HttpClientConnect(ConnectionProvider provider) {
this.config = new HttpClientConfig(
provider,
findHttpConnectionProvider(provider),
Collections.singletonMap(ChannelOption.AUTO_READ, false),
() -> AddressUtils.createUnresolved(NetUtil.LOCALHOST.getHostAddress(), DEFAULT_PORT));
}
Expand Down Expand Up @@ -195,6 +195,26 @@ static HttpClient applyTcpClientConfig(TcpClientConfig config) {
return httpClient;
}

private ConnectionProvider findHttpConnectionProvider(ConnectionProvider provider) {
if (!(provider instanceof HttpConnectionProvider)) {
return provider;
}

HttpConnectionProvider httpConnectionProvider = (HttpConnectionProvider) provider;

if (httpConnectionProvider.http1ConnectionProvider != null &&
httpConnectionProvider.h2ConnectionProvider.get() == null) {
return httpConnectionProvider.http1ConnectionProvider;
}

if (httpConnectionProvider.http1ConnectionProvider == null &&
httpConnectionProvider.h2ConnectionProvider.get() != null) {
return httpConnectionProvider.h2ConnectionProvider.get();
}

return provider;
}

static final class MonoHttpConnect extends Mono<Connection> {

final HttpClientConfig config;
Expand Down
Expand Up @@ -76,7 +76,7 @@ public Map<SocketAddress, Integer> maxConnectionsPerHost() {
final AtomicReference<ConnectionProvider> h2ConnectionProvider = new AtomicReference<>();

HttpConnectionProvider() {
this(null);
this(ConnectionProvider.create("default", 1));
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made new ConnectionProvider.
Because If http1ConnectionProvider and h2ConnectionProvider.get() values are all null,
It's impossible to get original ConnectionProvider

}

HttpConnectionProvider(@Nullable ConnectionProvider http1ConnectionProvider) {
Expand Down
Expand Up @@ -92,4 +92,38 @@ void maxConnectionsPerHostDefaultConnectionProvider() {
.isEqualTo(HttpResources.get().getOrCreateHttp2ConnectionProvider(HTTP2_CONNECTION_PROVIDER_FACTORY)
.maxConnectionsPerHost());
}

@Test
void returnOriginalConnectionProvider() {
ConnectionProvider provider = HttpClient.create().configuration().connectionProvider();
try {
assertThat(provider.mutate()).isNotNull();
}
finally {
provider.disposeLater()
.block(Duration.ofSeconds(5));
}
}

@Test
void returnOriginalConnectionProviderUsingBuilder() {
ConnectionProvider provider = ConnectionProvider
.builder("provider")
.maxConnections(1)
.disposeTimeout(Duration.ofSeconds(1L))
.pendingAcquireTimeout(Duration.ofSeconds(1L))
.maxIdleTime(Duration.ofSeconds(1L))
.maxLifeTime(Duration.ofSeconds(10L))
.lifo()
.build();

try {
HttpClient httpClient = HttpClient.create(provider);
assertThat(httpClient.configuration().connectionProvider().mutate()).isNotNull();
}
finally {
provider.disposeLater()
.block(Duration.ofSeconds(5));
}
}
}