Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 17 additions & 26 deletions java/src/main/java/com/genexus/internet/HttpClientJavaLib.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,45 +66,35 @@

public class HttpClientJavaLib extends GXHttpClient {

private static class FirstIpDnsResolver implements DnsResolver {
private final DnsResolver defaultDnsResolver = new SystemDefaultDnsResolver();

@Override
public InetAddress[] resolve(final String host) throws UnknownHostException {
InetAddress[] allIps = defaultDnsResolver.resolve(host);
if (allIps != null && allIps.length > 0) {
return new InetAddress[]{allIps[0]};
}
return allIps;
private static final DnsResolver FIRST_IP_DNS_RESOLVER = host -> {
InetAddress[] allIps = SystemDefaultDnsResolver.INSTANCE.resolve(host);
if (allIps != null && allIps.length > 0) {
return new InetAddress[]{allIps[0]};
}
}
return allIps;
};

private static String getGxIpResolverConfig() {
private static boolean isFirstIpDnsEnabled() {
String name = "GX_USE_FIRST_IP_DNS";
String gxDns = System.getProperty(name);
if (gxDns == null || gxDns.trim().isEmpty()) {
gxDns = System.getenv(name);
}
if (gxDns != null && gxDns.trim().equalsIgnoreCase("true")) {
return gxDns.trim();
} else {
return null;
}
return gxDns != null && gxDns.trim().equalsIgnoreCase("true");
}


public HttpClientJavaLib() {
getPoolInstance();
ConnectionKeepAliveStrategy myStrategy = generateKeepAliveStrategy();
HttpClientBuilder builder = HttpClients.custom()
.setConnectionManager(connManager)
.setConnectionManagerShared(true)
.setKeepAliveStrategy(myStrategy);
if (getGxIpResolverConfig() != null) {
builder.setDnsResolver(new FirstIpDnsResolver());
if (isFirstIpDnsEnabled()) {
builder.setDnsResolver(FIRST_IP_DNS_RESOLVER);
}
httpClientBuilder = builder;
cookies = new BasicCookieStore();
cookies = new BasicCookieStore();
streamsToClose = new Vector<>();
}

Expand All @@ -114,10 +104,11 @@ private static void getPoolInstance() {
RegistryBuilder.<ConnectionSocketFactory>create()
.register("http", PlainConnectionSocketFactory.INSTANCE).register("https", getSSLSecureInstance())
.build();
boolean useCustomDnsResolver = getGxIpResolverConfig() != null;
PoolingHttpClientConnectionManager connManager = useCustomDnsResolver
? new PoolingHttpClientConnectionManager(socketFactoryRegistry, new FirstIpDnsResolver())
: new PoolingHttpClientConnectionManager(socketFactoryRegistry);
if (isFirstIpDnsEnabled()) {
connManager = new PoolingHttpClientConnectionManager(socketFactoryRegistry, FIRST_IP_DNS_RESOLVER);
} else {
connManager = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
}
connManager.setMaxTotal((int) CommonUtil.val(clientCfg.getProperty("Client", "HTTPCLIENT_MAX_SIZE", "1000")));
connManager.setDefaultMaxPerRoute((int) CommonUtil.val(clientCfg.getProperty("Client", "HTTPCLIENT_MAX_PER_ROUTE", "1000")));

Expand Down Expand Up @@ -675,7 +666,7 @@ public void execute(String method, String url) {
resetStateAdapted();
}
}

private synchronized void displayHTTPConnections(){
Iterator<HttpRoute> iterator = storedRoutes.iterator();
while (iterator.hasNext()) {
Expand Down
Loading