Skip to content

Commit

Permalink
4.x - Custom DNS resolvers for Nima (#4876)
Browse files Browse the repository at this point in the history
* Custom DNS resolving

Signed-off-by: David Kral <david.k.kral@oracle.com>
  • Loading branch information
Verdent authored Nov 11, 2022
1 parent ddb75e3 commit 1769d9e
Show file tree
Hide file tree
Showing 20 changed files with 806 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import io.helidon.nima.http2.Http2Headers;
import io.helidon.nima.webclient.ClientConnection;
import io.helidon.nima.webclient.ClientRequest;
import io.helidon.nima.webclient.ConnectionKey;
import io.helidon.nima.webclient.UriHelper;

class ClientRequestImpl implements Http2ClientRequest {
Expand Down Expand Up @@ -217,7 +218,12 @@ private Http2Headers prepareHeaders(WritableHeaders<?> headers) {

private Http2ClientStream reserveStream() {
if (explicitConnection == null) {
ConnectionKey connectionKey = new ConnectionKey(uri.scheme(), uri.authority(), uri.host(), uri.port(), tls);
ConnectionKey connectionKey = new ConnectionKey(uri.scheme(),
uri.host(),
uri.port(),
tls,
client.dnsResolver(),
client.dnsAddressLookup());

// this statement locks all threads - must not do anything complicated (just create a new instance)
return CHANNEL_CACHE.computeIfAbsent(connectionKey,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.UncheckedIOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.security.cert.Certificate;
Expand All @@ -37,6 +38,8 @@
import io.helidon.common.socket.SocketWriter;
import io.helidon.common.socket.TlsSocket;
import io.helidon.nima.http2.Http2ConnectionWriter;
import io.helidon.nima.webclient.ConnectionKey;
import io.helidon.nima.webclient.spi.DnsResolver;

import static java.lang.System.Logger.Level.DEBUG;
import static java.lang.System.Logger.Level.TRACE;
Expand Down Expand Up @@ -109,9 +112,14 @@ private void doConnect() throws IOException {
socket = sslSocket == null ? new Socket() : sslSocket;

socketOptions.configureSocket(socket);
socket.connect(new InetSocketAddress(connectionKey.host(),
connectionKey.port()),
(int) socketOptions.connectTimeout().toMillis());
DnsResolver dnsResolver = connectionKey.dnsResolver();
if (dnsResolver.useDefaultJavaResolver()) {
socket.connect(new InetSocketAddress(connectionKey.host(), connectionKey.port()),
(int) socketOptions.connectTimeout().toMillis());
} else {
InetAddress address = dnsResolver.resolveAddress(connectionKey.host(), connectionKey.dnsAddressLookup());
socket.connect(new InetSocketAddress(address, connectionKey.port()), (int) socketOptions.connectTimeout().toMillis());
}
channelId = "0x" + HexFormat.of().toHexDigits(System.identityHashCode(socket));

helidonSocket = sslSocket == null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.util.concurrent.atomic.AtomicReference;

import io.helidon.common.socket.SocketOptions;
import io.helidon.nima.webclient.ConnectionKey;

// a representation of a single remote endpoint
// this may use one or more connections (depending on parallel streams)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright (c) 2022 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.helidon.nima.webclient;

import io.helidon.nima.common.tls.Tls;
import io.helidon.nima.webclient.spi.DnsResolver;

/**
* Connection key instance contains all needed connection related information.
*
* @param scheme uri address scheme
* @param host uri address host
* @param port uri address port
* @param tls TLS to be used in connection
* @param dnsResolver DNS resolver to be used
* @param dnsAddressLookup DNS address lookup strategy
*/
public record ConnectionKey(String scheme,
String host,
int port,
Tls tls,
DnsResolver dnsResolver,
DnsAddressLookup dnsAddressLookup) { }
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/*
* Copyright (c) 2022 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.helidon.nima.webclient;

import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;

import io.helidon.common.LazyValue;

/**
* Heavily inspired by Netty.
*/
final class DefaultDnsAddressLookupFinder {

private static final System.Logger LOGGER = System.getLogger(DefaultDnsAddressLookupFinder.class.getName());

/**
* {@code true} if IPv4 should be used even if the system supports both IPv4 and IPv6.
*/
private static final LazyValue<Boolean> IPV4_PREFERRED = LazyValue.create(() -> {
return Boolean.getBoolean("java.net.preferIPv4Stack");
});

/**
* {@code true} if an IPv6 address should be preferred when a host has both an IPv4 address and an IPv6 address.
*/
private static final LazyValue<Boolean> IPV6_PREFERRED = LazyValue.create(() -> {
return Boolean.getBoolean("java.net.preferIPv6Addresses");
});

private static final LazyValue<DnsAddressLookup> DEFAULT_IP_VERSION = LazyValue.create(() -> {
if (IPV4_PREFERRED.get() || !anyInterfaceSupportsIpV6()) {
return DnsAddressLookup.IPV4;
} else {
if (IPV6_PREFERRED.get()) {
return DnsAddressLookup.IPV6_PREFERRED;
} else {
return DnsAddressLookup.IPV4_PREFERRED;
}
}
});

private DefaultDnsAddressLookupFinder() {
throw new IllegalStateException("This class should not be instantiated");
}

static DnsAddressLookup defaultDnsAddressLookup() {
return DEFAULT_IP_VERSION.get();
}

/**
* Returns {@code true} if any {@link NetworkInterface} supports {@code IPv6}, {@code false} otherwise.
*/
private static boolean anyInterfaceSupportsIpV6() {
try {
Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
while (interfaces.hasMoreElements()) {
NetworkInterface networkInterface = interfaces.nextElement();
Enumeration<InetAddress> addresses = networkInterface.getInetAddresses();
while (addresses.hasMoreElements()) {
InetAddress inetAddress = addresses.nextElement();
if (inetAddress instanceof Inet6Address
&& !inetAddress.isAnyLocalAddress()
&& !inetAddress.isLoopbackAddress()
&& !inetAddress.isLinkLocalAddress()) {
return true;
}
}
}
} catch (SocketException ignore) {
if (LOGGER.isLoggable(System.Logger.Level.INFO)) {
LOGGER.log(System.Logger.Level.INFO,
"Unable to detect if any interface supports IPv6, assuming IPv4-only",
ignore);
}
}
return false;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright (c) 2022 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.helidon.nima.webclient;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.Map;

import io.helidon.nima.webclient.spi.DnsResolver;

final class DefaultDnsResolver implements DnsResolver {

private final Map<String, InetAddress> hostnameAddresses = new HashMap<>();

@Override
public InetAddress resolveAddress(String hostname, DnsAddressLookup dnsAddressLookup) {
return hostnameAddresses.computeIfAbsent(hostname, host -> {
try {
InetAddress[] processed = dnsAddressLookup.filter(InetAddress.getAllByName(hostname));
if (processed.length == 0) {
throw new RuntimeUnknownHostException("No IP version " + dnsAddressLookup.name() + " found for host " + host);
}
return processed[0];
} catch (UnknownHostException e) {
throw new RuntimeUnknownHostException(e);
}
});
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright (c) 2022 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.helidon.nima.webclient;

import io.helidon.common.Weight;
import io.helidon.common.Weighted;
import io.helidon.nima.webclient.spi.DnsResolver;
import io.helidon.nima.webclient.spi.DnsResolverProvider;

/**
* Provider of the {@link DefaultDnsResolver} instance.
*/
@Weight(Weighted.DEFAULT_WEIGHT)
public class DefaultDnsResolverProvider implements DnsResolverProvider {

/**
* Create new instance of the {@link DefaultDnsResolverProvider}.
* This should be used only for purposes of SPI.
*/
public DefaultDnsResolverProvider() {
}

@Override
public String resolverName() {
return "default";
}

@Override
public DnsResolver createDnsResolver() {
return new DefaultDnsResolver();
}
}
Loading

0 comments on commit 1769d9e

Please sign in to comment.