Skip to content

Commit

Permalink
Merge pull request #222 from trustin/dns_lookup
Browse files Browse the repository at this point in the history
Do not send an AAAA DNS query when -Djava.net.preferIPv4Stack=true (0.21)
  • Loading branch information
kojilin authored Aug 9, 2016
2 parents c8048c9 + 52773a0 commit 3337679
Showing 1 changed file with 30 additions and 2 deletions.
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.Optional;
import java.util.concurrent.ThreadFactory;
import java.util.function.Function;
Expand All @@ -27,6 +28,7 @@
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;
Expand All @@ -35,10 +37,13 @@
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 @@ -89,8 +94,7 @@ private NonDecoratingClientFactory(SessionOptions 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 @@ -148,4 +152,28 @@ public void close() {
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 3337679

Please sign in to comment.