Skip to content

Commit

Permalink
Merge pull request #223 from trustin/dns_lookup_0.20
Browse files Browse the repository at this point in the history
Do not send an AAAA DNS query when -Djava.net.preferIPv4Stack=true (0.20)
  • Loading branch information
kojilin authored Aug 9, 2016
2 parents 5fe2589 + 4066c77 commit 686bbeb
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 7 deletions.
9 changes: 4 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@
<url>${project.url}</url>
<connection>scm:git:https://github.com/line/armeria.git</connection>
<developerConnection>scm:git:ssh://git@github.com/line/armeria.git</developerConnection>
<tag>HEAD</tag>
</scm>

<developers>
Expand Down Expand Up @@ -81,11 +80,11 @@
<maven.compiler.showWarnings>true</maven.compiler.showWarnings>

<!-- Dependency versions -->
<jackson.version>2.7.5</jackson.version>
<jetty.version>9.3.10.v20160621</jetty.version>
<jackson.version>2.7.6</jackson.version>
<jetty.version>9.3.11.v20160721</jetty.version>
<logback.version>1.1.7</logback.version>
<metrics.version>3.1.2</metrics.version>
<netty.version>4.1.3.Final</netty.version>
<netty.version>4.1.4.Final</netty.version>
<slf4j.version>1.7.21</slf4j.version>
<tomcat.version>8.5.4</tomcat.version>
<jetty.alpnAgent.version>2.0.3</jetty.alpnAgent.version>
Expand All @@ -99,7 +98,7 @@
<thrift.source.test>${project.basedir}/src/test/thrift</thrift.source.test>
<thrift.generated.main>${project.build.directory}/generated-sources</thrift.generated.main>
<thrift.generated.test>${project.build.directory}/generated-test-sources</thrift.generated.test>
<brave.version>3.9.0</brave.version>
<brave.version>3.9.1</brave.version>
<guava.version>19.0</guava.version>
<reflections.version>0.9.10</reflections.version>
</properties>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import static java.util.Objects.requireNonNull;

import java.net.InetAddress;
import java.util.Collections;
import java.util.EnumMap;
import java.util.Map;
Expand All @@ -32,17 +33,22 @@
import com.linecorp.armeria.common.util.NativeLibraries;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFactory;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoop;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.epoll.EpollDatagramChannel;
import io.netty.channel.epoll.EpollEventLoopGroup;
import io.netty.channel.epoll.EpollSocketChannel;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.DatagramChannel;
import io.netty.channel.socket.InternetProtocolFamily;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioDatagramChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.resolver.NameResolver;
import io.netty.resolver.dns.DnsAddressResolverGroup;
import io.netty.resolver.dns.DnsNameResolverBuilder;
import io.netty.resolver.dns.DnsServerAddresses;
import io.netty.util.concurrent.DefaultThreadFactory;

Expand Down Expand Up @@ -139,8 +145,7 @@ private RemoteInvokerFactory(RemoteInvokerOptions options,
baseBootstrap.channel(channelType());
baseBootstrap.resolver(
options.addressResolverGroup()
.orElseGet(() -> new DnsAddressResolverGroup(
datagramChannelType(), DnsServerAddresses.defaultAddresses())));
.orElseGet(DnsAddressResolverGroup5657::new));

baseBootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS,
ConvertUtils.safeLongToInt(options.connectTimeoutMillis()));
Expand Down Expand Up @@ -219,4 +224,28 @@ private void close0() {
eventLoopGroup.shutdownGracefully().syncUninterruptibly();
}
}

private static class DnsAddressResolverGroup5657 extends DnsAddressResolverGroup {

DnsAddressResolverGroup5657() {
super(datagramChannelType(), DnsServerAddresses.defaultAddresses());
}

@Override
protected NameResolver<InetAddress> newNameResolver(
EventLoop eventLoop, ChannelFactory<? extends DatagramChannel> channelFactory,
DnsServerAddresses nameServerAddresses) throws Exception {

final DnsNameResolverBuilder builder = new DnsNameResolverBuilder(eventLoop);
builder.channelFactory(channelFactory);
builder.nameServerAddresses(nameServerAddresses);
if (Boolean.getBoolean("java.net.preferIPv4Stack")) {
// Resolve IPv4 addresses only when -Djava.net.preferIPv4Stack is enabled,
// because JDK will fail or refuse connecting to an IPv6 address at all.
// See: https://github.com/netty/netty/issues/5657
builder.resolvedAddressTypes(InternetProtocolFamily.IPv4);
}
return builder.build();
}
}
}

0 comments on commit 686bbeb

Please sign in to comment.