Skip to content

Commit

Permalink
Merge pull request #804 from commercetools/configure-http-version
Browse files Browse the repository at this point in the history
[DEVX-513] configurable protocol version for HTTP clients
  • Loading branch information
kodiakhq[bot] authored Jan 22, 2025
2 parents 1775bc8 + 40b2fa0 commit 01bd9a8
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,13 @@ public static HttpAsyncClientBuilder createClientBuilder(AsyncClientConnectionMa

public static PoolingAsyncClientConnectionManagerBuilder createConnectionManager(final int maxConnTotal,
final int maxConnPerRoute) {
return createConnectionManager(HttpVersionPolicy.NEGOTIATE, maxConnTotal, maxConnPerRoute);
}

public static PoolingAsyncClientConnectionManagerBuilder createConnectionManager(
final HttpVersionPolicy httpVersionPolicy, final int maxConnTotal, final int maxConnPerRoute) {
final TlsStrategy tlsStrategy = ClientTlsStrategyBuilder.create().useSystemProperties().build();
final TlsConfig tlsConfig = TlsConfig.copy(TlsConfig.DEFAULT)
.setVersionPolicy(HttpVersionPolicy.NEGOTIATE)
.build();
final TlsConfig tlsConfig = TlsConfig.copy(TlsConfig.DEFAULT).setVersionPolicy(httpVersionPolicy).build();
return PoolingAsyncClientConnectionManagerBuilder.create()
.setMaxConnPerRoute(maxConnPerRoute)
.setMaxConnTotal(maxConnTotal)
Expand All @@ -74,12 +77,27 @@ public CtApacheHttpClient() {
init();
}

public CtApacheHttpClient(final HttpVersionPolicy versionPolicy) {
super();
apacheHttpClient = createClientBuilder(
createConnectionManager(versionPolicy, MAX_REQUESTS, MAX_REQUESTS).build()).build();
init();
}

public CtApacheHttpClient(final int maxConnTotal, final int maxConnPerRoute) {
super();
apacheHttpClient = createClientBuilder(createConnectionManager(maxConnTotal, maxConnPerRoute).build()).build();
init();
}

public CtApacheHttpClient(final HttpVersionPolicy versionPolicy, final int maxConnTotal,
final int maxConnPerRoute) {
super();
apacheHttpClient = createClientBuilder(
createConnectionManager(versionPolicy, maxConnTotal, maxConnPerRoute).build()).build();
init();
}

public CtApacheHttpClient(final BuilderOptions options) {
super();
apacheHttpClient = options.plus(clientBuilder.get()).build();
Expand All @@ -94,6 +112,16 @@ public CtApacheHttpClient(final int maxConnTotal, final int maxConnPerRoute, fin
init();
}

public CtApacheHttpClient(final HttpVersionPolicy versionPolicy, final int maxConnTotal, final int maxConnPerRoute,
final BuilderOptions options) {
super();
apacheHttpClient = options
.plus(
createClientBuilder(createConnectionManager(versionPolicy, maxConnTotal, maxConnPerRoute).build()))
.build();
init();
}

public CtApacheHttpClient(final Supplier<HttpAsyncClientBuilder> builderSupplier) {
super();
apacheHttpClient = builderSupplier.get().build();
Expand All @@ -112,6 +140,14 @@ public CtApacheHttpClient(final ExecutorService executor, final int maxConnTotal
init();
}

public CtApacheHttpClient(final ExecutorService executor, final HttpVersionPolicy versionPolicy,
final int maxConnTotal, final int maxConnPerRoute) {
super(executor);
apacheHttpClient = createClientBuilder(
createConnectionManager(versionPolicy, maxConnTotal, maxConnPerRoute).build()).build();
init();
}

public CtApacheHttpClient(final ExecutorService executor, final BuilderOptions options) {
super(executor);
apacheHttpClient = options.plus(clientBuilder.get()).build();
Expand All @@ -127,6 +163,16 @@ public CtApacheHttpClient(final ExecutorService executor, final int maxConnTotal
init();
}

public CtApacheHttpClient(final ExecutorService executor, final HttpVersionPolicy versionPolicy,
final int maxConnTotal, final int maxConnPerRoute, final BuilderOptions options) {
super(executor);
apacheHttpClient = options
.plus(
createClientBuilder(createConnectionManager(versionPolicy, maxConnTotal, maxConnPerRoute).build()))
.build();
init();
}

public CtApacheHttpClient(final ExecutorService executor, final Supplier<HttpAsyncClientBuilder> builderSupplier) {
super(executor);
apacheHttpClient = builderSupplier.get().build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
package com.commercetools.http.asynchttp;

import io.vrap.rmf.base.client.SolutionInfo;
import io.vrap.rmf.base.client.VersionLoader;

import org.asynchttpclient.config.AsyncHttpClientConfigDefaults;

public class AsyncHttpClientSolutionInfo extends SolutionInfo {

public AsyncHttpClientSolutionInfo() {
setName("AsyncHttpAsyncClient");
setVersion(VersionLoader.loadVersionInfo("org.asynchttpclient", "async-http-client", "pom.properties",
"version", this.getClass().getClassLoader()));
setVersion(AsyncHttpClientConfigDefaults.AHC_VERSION);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ public CtJavaNetHttpClient() {
javaHttpClient = clientBuilder.get().build();
}

public CtJavaNetHttpClient(HttpClient.Version version) {
super();
javaHttpClient = clientBuilder.get().version(version).build();
}

public CtJavaNetHttpClient(final BuilderOptions options) {
super();
javaHttpClient = options.plus(clientBuilder.get()).build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import reactor.core.publisher.Mono;
import reactor.netty.ByteBufMono;
import reactor.netty.NettyOutbound;
import reactor.netty.http.HttpProtocol;
import reactor.netty.http.client.HttpClient;
import reactor.netty.http.client.HttpClientRequest;
import reactor.netty.http.client.HttpClientResponse;
Expand All @@ -41,12 +42,22 @@ public CtNettyHttpClient() {
this(MAX_REQUESTS);
}

public CtNettyHttpClient(int maxConnections) {
public CtNettyHttpClient(HttpProtocol protocol) {
this(protocol, MAX_REQUESTS);
}

public CtNettyHttpClient(final int maxConnections) {
super();
this.nettyClient = createDefaultClient(maxConnections);
this.nettyClient.warmup();
}

public CtNettyHttpClient(final HttpProtocol protocol, final int maxConnections) {
super();
this.nettyClient = createDefaultClient(protocol, maxConnections);
this.nettyClient.warmup();
}

public CtNettyHttpClient(final BuilderOptions options) {
this(MAX_REQUESTS, options);
}
Expand All @@ -73,6 +84,12 @@ public CtNettyHttpClient(final ExecutorService executor, final int maxConnection
this.nettyClient.warmup();
}

public CtNettyHttpClient(final ExecutorService executor, final HttpProtocol protocol, final int maxConnections) {
super(executor);
this.nettyClient = createDefaultClient(protocol, maxConnections);
this.nettyClient.warmup();
}

public CtNettyHttpClient(final ExecutorService executor, final BuilderOptions options) {
this(executor, MAX_REQUESTS, options);
}
Expand All @@ -83,6 +100,13 @@ public CtNettyHttpClient(final ExecutorService executor, final int maxConnection
this.nettyClient.warmup();
}

public CtNettyHttpClient(final ExecutorService executor, final HttpProtocol protocol, final int maxConnections,
final BuilderOptions options) {
super(executor);
this.nettyClient = options.plus(createDefaultClient(protocol, maxConnections));
this.nettyClient.warmup();
}

public CtNettyHttpClient(final ExecutorService executor, final Supplier<HttpClient> httpClientSupplier) {
super(executor);
this.nettyClient = httpClientSupplier.get();
Expand All @@ -97,6 +121,10 @@ public static HttpClient createDefaultClient(final int maxConnections) {
.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 10000);
}

public static HttpClient createDefaultClient(final HttpProtocol httpProtocol, final int maxConnections) {
return createDefaultClient(maxConnections).protocol(httpProtocol);
}

@Override
public CompletableFuture<ApiHttpResponse<byte[]>> execute(ApiHttpRequest request) {
final HttpMethod method = HttpMethod.valueOf(request.getMethod().name());
Expand Down
2 changes: 2 additions & 0 deletions commercetools/internal-docs/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ dependencies {
implementation project(":commercetools:commercetools-http-client")
implementation project(":commercetools:commercetools-okhttp-client4")
implementation project(":commercetools:commercetools-apachehttp-client")
implementation project(":commercetools:commercetools-reactornetty-client")
implementation project(":commercetools:commercetools-sdk-compat-v1")
implementation project(":commercetools:commercetools-graphql-api")
implementation project(":commercetools:commercetools-monitoring-newrelic")
implementation project(":commercetools:commercetools-monitoring-datadog")

testImplementation ctsdkv1.client version ctsdkv1.version
implementation ctsdkv1.models version ctsdkv1.version
testImplementation project(":commercetools:commercetools-javanet-client")

testImplementation commons.io version commons.io_version

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,14 @@
* <p>The third option is to use the timeout functionality of the futures</p>
*
* {@include.example example.ExamplesTest#timeoutFuture()}
*
* <h3 id=http-version>HTTP protocol version</h3>
*
* <p>In case it's needed so specify a specific HTTP protocol version this can be done through the HTTP client package itself.</p>
*
* {@include.example example.ExamplesTest#httpVersion()}
*
* <p>The AsyncHttpClient and OkHttp packages use HTTP1.1 by default.</p>
*/
public class ClientTuning {
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.http.HttpClient;
import java.time.Duration;
import java.util.Arrays;
import java.util.Collections;
Expand Down Expand Up @@ -35,6 +36,8 @@
import com.commercetools.api.models.project.Project;
import com.commercetools.api.models.tax_category.TaxCategoryPagedQueryResponse;
import com.commercetools.http.apachehttp.CtApacheHttpClient;
import com.commercetools.http.javanet.CtJavaNetHttpClient;
import com.commercetools.http.netty.CtNettyHttpClient;
import com.commercetools.http.okhttp4.CtOkHttp4Client;

import io.vrap.rmf.base.client.*;
Expand All @@ -43,13 +46,15 @@

import org.apache.commons.lang3.tuple.Pair;
import org.apache.hc.client5.http.config.RequestConfig;
import org.apache.hc.core5.http2.HttpVersionPolicy;
import org.apache.hc.core5.util.Timeout;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;
import org.slf4j.event.Level;

import dev.failsafe.Failsafe;
import dev.failsafe.FailsafeExecutor;
import reactor.netty.http.HttpProtocol;

public class ExamplesTest {

Expand Down Expand Up @@ -623,4 +628,10 @@ public void mdcProjectApiRoot() {
ProjectApiRoot globalRoot = ProjectApiRoot.of("test");
ProjectApiRoot apiRoot = ProjectApiRoot.withContext(globalRoot, new MDCContext());
}

public void httpVersion() {
CtApacheHttpClient apacheHttpClient = new CtApacheHttpClient(HttpVersionPolicy.FORCE_HTTP_1);
CtNettyHttpClient nettyHttpClient = new CtNettyHttpClient(HttpProtocol.HTTP11);
CtJavaNetHttpClient javaNetHttpClient = new CtJavaNetHttpClient(HttpClient.Version.HTTP_1_1);
}
}

0 comments on commit 01bd9a8

Please sign in to comment.