Skip to content

Commit

Permalink
DnsNameResolver for case grpc#1 and grpc#2
Browse files Browse the repository at this point in the history
  • Loading branch information
zpencer committed Jun 1, 2017
1 parent c48b191 commit f52ceaa
Showing 1 changed file with 28 additions and 17 deletions.
45 changes: 28 additions & 17 deletions core/src/main/java/io/grpc/internal/DnsNameResolver.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.URI;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
Expand Down Expand Up @@ -165,36 +166,32 @@ public void run() {
}
try {
InetSocketAddress destination = InetSocketAddress.createUnresolved(host, port);
ProxyParameters maybeNullParams = proxyDetector.proxyFor(destination);
ProxyParameters proxyParams = proxyDetector.proxyFor(destination);
Attributes attrs = Attributes.newBuilder()
.set(ProxyDetector.ATTR_KEY_PROXY_PARAMS, maybeNullParams)
.set(ProxyDetector.ATTR_KEY_PROXY_PARAMS, proxyParams)
.build();

ResolutionResults resolvedInetAddrs;
ResolutionResults resolvedInetAddrs = null;
try {
resolvedInetAddrs = delegateResolver.resolve(host);
} catch (Exception e) {
synchronized (DnsNameResolver.this) {
if (shutdown) {
return;
}
// Because timerService is the single-threaded GrpcUtil.TIMER_SERVICE in production,
// we need to delegate the blocking work to the executor
resolutionTask =
timerService.schedule(new LogExceptionRunnable(resolutionRunnableOnExecutor),
1, TimeUnit.MINUTES);
} catch (UnknownHostException e) {
// It is OK for a host to be unresolvable, assuming the proxy can resolve it
if (proxyParams == null) {
handleError(savedListener, e);
return;
}
savedListener.onError(Status.UNAVAILABLE.withCause(e));
} catch (Exception e) {
handleError(savedListener, e);
return;
}
ArrayList<EquivalentAddressGroup> servers = new ArrayList<EquivalentAddressGroup>();
if (resolvedInetAddrs.addresses.isEmpty()) {
servers.add(new EquivalentAddressGroup(destination));
} else {
if (resolvedInetAddrs != null) {
// Each address forms an EAG
for (InetAddress inetAddr : resolvedInetAddrs.addresses) {
servers.add(new EquivalentAddressGroup(new InetSocketAddress(inetAddr, port), attrs));
}
} else {
servers.add(new EquivalentAddressGroup(destination, attrs));
}
savedListener.onAddresses(servers, Attributes.EMPTY);
} finally {
Expand All @@ -205,6 +202,20 @@ public void run() {
}
};

private void handleError(Listener listener, Exception e) {
synchronized (DnsNameResolver.this) {
if (shutdown) {
return;
}
// Because timerService is the single-threaded GrpcUtil.TIMER_SERVICE in production,
// we need to delegate the blocking work to the executor
resolutionTask =
timerService.schedule(new LogExceptionRunnable(resolutionRunnableOnExecutor),
1, TimeUnit.MINUTES);
}
listener.onError(Status.UNAVAILABLE.withCause(e));
}

private final Runnable resolutionRunnableOnExecutor = new Runnable() {
@Override
public void run() {
Expand Down

0 comments on commit f52ceaa

Please sign in to comment.