Skip to content

Commit 65e4d9f

Browse files
committed
all: avoid DNS with GRPC_PROXY_EXP
In some environments DNS is not available and is performed by the CONNECT proxy. Nothing "special" should need to be done for these environments, but the previous support took shortcuts which knowingly would not support such environments. This change should fix both OkHttp and Netty. Netty's Bootstrap.connect() resolved the name immediately whereas using ChannelPipeline.connect() waits until the address reaches the end of the pipeline. Netty uses NetUtil.toSocketAddressString() to get the name of the address, which uses InetSocketAddress.getHostString() when available. OkHttp is still using InetSocketAddress.getHostName() which may issue reverse DNS lookups. However, if the reverse DNS lookup fails, it should convert the IP to a textual string like getHostString(). So as long as the reverse DNS maps to the same machine as the IP, there should only be performance concerns, not correctness issues. Since the DnsNameResolver is creating unresolved addresses, the reverse DNS lookups shouldn't occur in the common case.
1 parent 23f5a6f commit 65e4d9f

File tree

3 files changed

+14
-16
lines changed

3 files changed

+14
-16
lines changed

core/src/main/java/io/grpc/internal/DnsNameResolver.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,15 @@ public void run() {
140140
resolving = true;
141141
}
142142
try {
143+
if (System.getenv("GRPC_PROXY_EXP") != null) {
144+
ResolvedServerInfoGroup servers = ResolvedServerInfoGroup.builder()
145+
.add(new ResolvedServerInfo(
146+
InetSocketAddress.createUnresolved(host, port), Attributes.EMPTY))
147+
.build();
148+
savedListener.onUpdate(Collections.singletonList(servers), Attributes.EMPTY);
149+
return;
150+
}
151+
143152
try {
144153
inetAddrs = getAllByName(host);
145154
} catch (UnknownHostException e) {

interop-testing/src/main/java/io/grpc/testing/integration/TestServiceClient.java

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,6 @@
4343
import io.netty.handler.ssl.SslContext;
4444
import java.io.File;
4545
import java.io.FileInputStream;
46-
import java.net.InetAddress;
47-
import java.net.InetSocketAddress;
48-
import java.net.UnknownHostException;
4946
import java.nio.charset.Charset;
5047
import javax.net.ssl.SSLSocketFactory;
5148

@@ -304,16 +301,6 @@ private class Tester extends AbstractInteropTest {
304301
@Override
305302
protected ManagedChannel createChannel() {
306303
if (!useOkHttp) {
307-
InetAddress address;
308-
try {
309-
address = InetAddress.getByName(serverHost);
310-
if (serverHostOverride != null) {
311-
// Force the hostname to match the cert the server uses.
312-
address = InetAddress.getByAddress(serverHostOverride, address.getAddress());
313-
}
314-
} catch (UnknownHostException ex) {
315-
throw new RuntimeException(ex);
316-
}
317304
SslContext sslContext = null;
318305
if (useTestCa) {
319306
try {
@@ -323,7 +310,8 @@ protected ManagedChannel createChannel() {
323310
throw new RuntimeException(ex);
324311
}
325312
}
326-
return NettyChannelBuilder.forAddress(new InetSocketAddress(address, serverPort))
313+
return NettyChannelBuilder.forAddress(serverHost, serverPort)
314+
.overrideAuthority(serverHostOverride)
327315
.flowControlWindow(65 * 1024)
328316
.negotiationType(useTls ? NegotiationType.TLS : NegotiationType.PLAINTEXT)
329317
.sslContext(sslContext)

netty/src/main/java/io/grpc/netty/NettyClientTransport.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,8 +194,9 @@ public Runnable start(Listener transportListener) {
194194
* that it may begin buffering writes.
195195
*/
196196
b.handler(negotiationHandler);
197+
channel = b.register().channel();
197198
// Start the connection operation to the server.
198-
channel = b.connect(address).addListener(new ChannelFutureListener() {
199+
channel.connect(address).addListener(new ChannelFutureListener() {
199200
@Override
200201
public void operationComplete(ChannelFuture future) throws Exception {
201202
if (!future.isSuccess()) {
@@ -209,7 +210,7 @@ public void operationComplete(ChannelFuture future) throws Exception {
209210
future.channel().pipeline().fireExceptionCaught(future.cause());
210211
}
211212
}
212-
}).channel();
213+
});
213214
// Start the write queue as soon as the channel is constructed
214215
handler.startWriteQueue(channel);
215216
// This write will have no effect, yet it will only complete once the negotiationHandler

0 commit comments

Comments
 (0)