Skip to content

Commit

Permalink
Fixed #557 with the response timeout using the incorrect units (#558)
Browse files Browse the repository at this point in the history
Added convenience constructor to ZitiConnectionSocketFactory for referencing an existing ZitiContext
  • Loading branch information
ahazeltonNF authored Mar 11, 2024
1 parent 066066a commit 5af7884
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 5af7884

Please sign in to comment.