Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

4.x - Custom DNS resolvers for Nima #4876

Merged
merged 5 commits into from
Nov 11, 2022
Merged
Show file tree
Hide file tree
Changes from 4 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
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,13 @@ 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.authority(),
Verdent marked this conversation as resolved.
Show resolved Hide resolved
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,39 @@
/*
* 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 authority uri address authority
* @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 authority,
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,36 @@
/*
* 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.webclient.spi.DnsResolver;
import io.helidon.nima.webclient.spi.DnsResolverProvider;

/**
* Provider of the {@link DefaultDnsResolver} instance.
*/
public class DefaultDnsResolverProvider implements DnsResolverProvider {
Verdent marked this conversation as resolved.
Show resolved Hide resolved

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

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