From 5af7884f2b65c03652bb15d4866c120547c78aad Mon Sep 17 00:00:00 2001 From: Alan Hazelton <148586068+ahazeltonNF@users.noreply.github.com> Date: Mon, 11 Mar 2024 12:49:10 -0400 Subject: [PATCH] Fixed #557 with the response timeout using the incorrect units (#558) Added convenience constructor to ZitiConnectionSocketFactory for referencing an existing ZitiContext --- .../config/ZitiHttpClientConfiguration.java | 21 +++++++------------ .../AbstractZitiConnectionSocketFactory.java | 4 ++++ .../ZitiConnectionSocketFactory.java | 5 +++++ .../ZitiSSLConnectionSocketFactory.java | 5 +++++ 4 files changed, 21 insertions(+), 14 deletions(-) diff --git a/ziti-springboot-client/src/main/java/org/openziti/springboot/client/web/config/ZitiHttpClientConfiguration.java b/ziti-springboot-client/src/main/java/org/openziti/springboot/client/web/config/ZitiHttpClientConfiguration.java index 71e66fbd..1f5cbfdf 100644 --- a/ziti-springboot-client/src/main/java/org/openziti/springboot/client/web/config/ZitiHttpClientConfiguration.java +++ b/ziti-springboot-client/src/main/java/org/openziti/springboot/client/web/config/ZitiHttpClientConfiguration.java @@ -19,34 +19,29 @@ import java.io.IOException; import java.net.InetAddress; import java.net.UnknownHostException; -import java.util.Arrays; import java.util.Iterator; +import java.util.List; import java.util.Optional; import java.util.concurrent.TimeUnit; -import javax.net.ssl.SSLSocketFactory; import jakarta.annotation.PreDestroy; import org.apache.hc.client5.http.ConnectionKeepAliveStrategy; import org.apache.hc.client5.http.DnsResolver; import org.apache.hc.client5.http.classic.HttpClient; import org.apache.hc.client5.http.config.RequestConfig; -import org.apache.hc.client5.http.impl.InMemoryDnsResolver; import org.apache.hc.client5.http.impl.classic.CloseableHttpClient; import org.apache.hc.client5.http.impl.classic.HttpClients; -import org.apache.hc.client5.http.impl.io.BasicHttpClientConnectionManager; import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager; -import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManagerBuilder; import org.apache.hc.client5.http.socket.ConnectionSocketFactory; -import org.apache.hc.client5.http.socket.LayeredConnectionSocketFactory; -import org.apache.hc.client5.http.ssl.SSLConnectionSocketFactory; import org.apache.hc.core5.http.HeaderElement; import org.apache.hc.core5.http.HeaderElements; import org.apache.hc.core5.http.URIScheme; import org.apache.hc.core5.http.config.Registry; import org.apache.hc.core5.http.config.RegistryBuilder; -import org.apache.hc.core5.http.io.SocketConfig; -import org.apache.hc.core5.http.message.BasicHeaderElementIterator; import org.apache.hc.core5.http.message.MessageSupport; +import org.apache.hc.core5.pool.PoolConcurrencyPolicy; +import org.apache.hc.core5.pool.PoolReusePolicy; import org.apache.hc.core5.util.TimeValue; +import org.openziti.Ziti; import org.openziti.springboot.client.web.httpclient.ZitiConnectionSocketFactory; import org.openziti.springboot.client.web.httpclient.ZitiSSLConnectionSocketFactory; import org.springframework.beans.factory.BeanCreationException; @@ -64,9 +59,6 @@ @Configuration public class ZitiHttpClientConfiguration { - // The default timeout in milliseconds until a connection is established. - private static final int DEFAULT_CONNECT_TIMEOUT = 30000; - // The default timeout when requesting a connection from the connection manager. private static final long DEFAULT_CONNECTION_REQUEST_TIMEOUT = 30000; @@ -153,7 +145,8 @@ public PoolingHttpClientConnectionManager poolingConnectionManager( final DnsResolver dnsResolver = getDnsResolver(); final PoolingHttpClientConnectionManager poolingConnectionManager = - new PoolingHttpClientConnectionManager(socketFactoryRegistry, null, null, null, null, dnsResolver, null); + new PoolingHttpClientConnectionManager(socketFactoryRegistry, PoolConcurrencyPolicy.STRICT, PoolReusePolicy.LIFO, + TimeValue.NEG_ONE_MILLISECOND, null, dnsResolver, null); Optional.ofNullable(maxTotal).ifPresent(poolingConnectionManager::setMaxTotal); Optional.ofNullable(maxPerRoute).ifPresent(poolingConnectionManager::setDefaultMaxPerRoute); @@ -188,7 +181,7 @@ public CloseableHttpClient httpClient( return HttpClients.custom() .setDefaultRequestConfig(RequestConfig.custom() .setConnectionRequestTimeout(Optional.ofNullable(connectionRequestTimeout).orElse(DEFAULT_CONNECTION_REQUEST_TIMEOUT), TimeUnit.MILLISECONDS) - .setResponseTimeout(Optional.ofNullable(responseTimeout).orElse(DEFAULT_RESPONSE_TIMEOUT), TimeUnit.MICROSECONDS) + .setResponseTimeout(Optional.ofNullable(responseTimeout).orElse(DEFAULT_RESPONSE_TIMEOUT), TimeUnit.MILLISECONDS) .build()) .setConnectionManager(poolingHttpClientConnectionManager) .setKeepAliveStrategy(connectionKeepAliveStrategy) diff --git a/ziti-springboot-client/src/main/java/org/openziti/springboot/client/web/httpclient/AbstractZitiConnectionSocketFactory.java b/ziti-springboot-client/src/main/java/org/openziti/springboot/client/web/httpclient/AbstractZitiConnectionSocketFactory.java index 6fce1816..00670824 100644 --- a/ziti-springboot-client/src/main/java/org/openziti/springboot/client/web/httpclient/AbstractZitiConnectionSocketFactory.java +++ b/ziti-springboot-client/src/main/java/org/openziti/springboot/client/web/httpclient/AbstractZitiConnectionSocketFactory.java @@ -104,6 +104,10 @@ public AbstractZitiConnectionSocketFactory(String fileName, char[] pwd, long wai } } + public AbstractZitiConnectionSocketFactory(ZitiContext ctx) { + this.ctx = ctx; + } + public void shutdown() { Optional.ofNullable(getCtx()).ifPresent(ZitiContext::destroy); } diff --git a/ziti-springboot-client/src/main/java/org/openziti/springboot/client/web/httpclient/ZitiConnectionSocketFactory.java b/ziti-springboot-client/src/main/java/org/openziti/springboot/client/web/httpclient/ZitiConnectionSocketFactory.java index 88aeebb6..13aaf960 100644 --- a/ziti-springboot-client/src/main/java/org/openziti/springboot/client/web/httpclient/ZitiConnectionSocketFactory.java +++ b/ziti-springboot-client/src/main/java/org/openziti/springboot/client/web/httpclient/ZitiConnectionSocketFactory.java @@ -26,6 +26,7 @@ import org.apache.hc.core5.http.protocol.HttpContext; import org.apache.hc.core5.util.TimeValue; import org.openziti.Ziti; +import org.openziti.ZitiContext; import lombok.extern.slf4j.Slf4j; @Slf4j @@ -75,6 +76,10 @@ public ZitiConnectionSocketFactory(String fileName, char[] pwd, long waitTime) { super(fileName, pwd, waitTime); } + public ZitiConnectionSocketFactory(ZitiContext ctx) { + super(ctx); + } + @Override public Socket createSocket(HttpContext context) throws IOException { if (socketFactory == null) { diff --git a/ziti-springboot-client/src/main/java/org/openziti/springboot/client/web/httpclient/ZitiSSLConnectionSocketFactory.java b/ziti-springboot-client/src/main/java/org/openziti/springboot/client/web/httpclient/ZitiSSLConnectionSocketFactory.java index c60fb8e0..29bb3ad4 100644 --- a/ziti-springboot-client/src/main/java/org/openziti/springboot/client/web/httpclient/ZitiSSLConnectionSocketFactory.java +++ b/ziti-springboot-client/src/main/java/org/openziti/springboot/client/web/httpclient/ZitiSSLConnectionSocketFactory.java @@ -32,6 +32,7 @@ import org.apache.hc.core5.ssl.SSLContextBuilder; import org.apache.hc.core5.util.TimeValue; import org.openziti.Ziti; +import org.openziti.ZitiContext; import lombok.extern.slf4j.Slf4j; @Slf4j @@ -81,6 +82,10 @@ public ZitiSSLConnectionSocketFactory(String fileName, char[] pwd, long waitTime super(fileName, pwd, waitTime); } + public ZitiSSLConnectionSocketFactory(ZitiContext ctx) { + super(ctx); + } + @Override public Socket createSocket(HttpContext context) { return new Socket();