Skip to content

Commit

Permalink
[http-client] enable to configure http version (1.1 or 2), redirect p…
Browse files Browse the repository at this point in the history
…olicy and connect timeout
  • Loading branch information
rmannibucau committed Jul 11, 2023
1 parent 818c13f commit f8d46c9
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import javax.enterprise.inject.Produces;
import javax.inject.Inject;
import java.net.http.HttpClient;
import java.util.concurrent.Executor;
import java.time.Duration;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ForkJoinPool;
Expand All @@ -40,6 +40,21 @@ public class HttpClientProducer implements ConfigHolder {
@ConfigProperty(name = "bundlebee.httpclient.threads", defaultValue = "-1")
private int threads;

@Inject
@Description("The HTTP client version, `none` mean the JVM default (v2), `HTTP_1_1` v1.1 and `HTTP_2` v2.0.")
@ConfigProperty(name = "bundlebee.httpclient.forcedHttpVersion", defaultValue = "none")
private String forcedHttpVersion;

@Inject
@Description("The HTTP client connect timeout (in java Duration format), `none` can be used to ignore this setting.")
@ConfigProperty(name = "bundlebee.httpclient.connectTimeout", defaultValue = "none")
private String connectTimeout;

@Inject
@Description("The HTTP client redirect policy. Default to `NORMAL`, can be set to `ALWAYS` or `NEVER`.")
@ConfigProperty(name = "bundlebee.httpclient.followRedirects", defaultValue = "NORMAL")
private String followRedirects;

@Produces
@BundleBee
@ApplicationScoped
Expand All @@ -56,9 +71,17 @@ public Thread newThread(final Runnable r) {
return thread;
}
});
return HttpClient.newBuilder()
.executor(executor)
.build();
final var builder = HttpClient.newBuilder().executor(executor);
if (forcedHttpVersion != null && !forcedHttpVersion.isBlank() && !"none".equals(forcedHttpVersion)) {
builder.version(HttpClient.Version.valueOf(forcedHttpVersion));
}
if (connectTimeout != null && !connectTimeout.isBlank() && !"none".equals(connectTimeout)) {
builder.connectTimeout(Duration.parse(connectTimeout));
}
if (followRedirects != null && !followRedirects.isBlank()) {
builder.followRedirects(HttpClient.Redirect.valueOf(followRedirects));
}
return builder.build();
}

private boolean isMaven() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,13 @@ public class HttpKubeClient implements ConfigHolder {

@PostConstruct
private void init() {
client = new DelegatingClient(doConfigure(HttpClient.newBuilder()
.executor(dontUseAtRuntime.executor().orElseGet(ForkJoinPool::commonPool)))
.build()) {
final var builder = HttpClient.newBuilder();
dontUseAtRuntime.connectTimeout().ifPresent(builder::connectTimeout);
builder.followRedirects(dontUseAtRuntime.followRedirects());
builder.version(dontUseAtRuntime.version());
builder.executor(dontUseAtRuntime.executor().orElseGet(ForkJoinPool::commonPool));

client = new DelegatingClient(doConfigure(builder).build()) {
@Override
public <T> CompletableFuture<HttpResponse<T>> sendAsync(final HttpRequest request,
final HttpResponse.BodyHandler<T> responseBodyHandler) {
Expand Down Expand Up @@ -388,7 +392,8 @@ private HttpClient.Builder doConfigureFromLoadedKubeConfig(final String location
keyManagerFactory.init(keyStore, new char[0]);

keyManagers = keyManagerFactory.getKeyManagers();
} catch (final NoSuchAlgorithmException | CertificateException | UnrecoverableKeyException | KeyStoreException | IOException e) {
} catch (final NoSuchAlgorithmException | CertificateException | UnrecoverableKeyException |
KeyStoreException | IOException e) {
throw new IllegalStateException(e);
}
} catch (final RuntimeException re) {
Expand Down

0 comments on commit f8d46c9

Please sign in to comment.