Skip to content

Commit f52ceaa

Browse files
committed
DnsNameResolver for case grpc#1 and grpc#2
1 parent c48b191 commit f52ceaa

File tree

1 file changed

+28
-17
lines changed

1 file changed

+28
-17
lines changed

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

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import java.net.InetAddress;
4545
import java.net.InetSocketAddress;
4646
import java.net.URI;
47+
import java.net.UnknownHostException;
4748
import java.util.ArrayList;
4849
import java.util.Arrays;
4950
import java.util.Collections;
@@ -165,36 +166,32 @@ public void run() {
165166
}
166167
try {
167168
InetSocketAddress destination = InetSocketAddress.createUnresolved(host, port);
168-
ProxyParameters maybeNullParams = proxyDetector.proxyFor(destination);
169+
ProxyParameters proxyParams = proxyDetector.proxyFor(destination);
169170
Attributes attrs = Attributes.newBuilder()
170-
.set(ProxyDetector.ATTR_KEY_PROXY_PARAMS, maybeNullParams)
171+
.set(ProxyDetector.ATTR_KEY_PROXY_PARAMS, proxyParams)
171172
.build();
172173

173-
ResolutionResults resolvedInetAddrs;
174+
ResolutionResults resolvedInetAddrs = null;
174175
try {
175176
resolvedInetAddrs = delegateResolver.resolve(host);
176-
} catch (Exception e) {
177-
synchronized (DnsNameResolver.this) {
178-
if (shutdown) {
179-
return;
180-
}
181-
// Because timerService is the single-threaded GrpcUtil.TIMER_SERVICE in production,
182-
// we need to delegate the blocking work to the executor
183-
resolutionTask =
184-
timerService.schedule(new LogExceptionRunnable(resolutionRunnableOnExecutor),
185-
1, TimeUnit.MINUTES);
177+
} catch (UnknownHostException e) {
178+
// It is OK for a host to be unresolvable, assuming the proxy can resolve it
179+
if (proxyParams == null) {
180+
handleError(savedListener, e);
181+
return;
186182
}
187-
savedListener.onError(Status.UNAVAILABLE.withCause(e));
183+
} catch (Exception e) {
184+
handleError(savedListener, e);
188185
return;
189186
}
190187
ArrayList<EquivalentAddressGroup> servers = new ArrayList<EquivalentAddressGroup>();
191-
if (resolvedInetAddrs.addresses.isEmpty()) {
192-
servers.add(new EquivalentAddressGroup(destination));
193-
} else {
188+
if (resolvedInetAddrs != null) {
194189
// Each address forms an EAG
195190
for (InetAddress inetAddr : resolvedInetAddrs.addresses) {
196191
servers.add(new EquivalentAddressGroup(new InetSocketAddress(inetAddr, port), attrs));
197192
}
193+
} else {
194+
servers.add(new EquivalentAddressGroup(destination, attrs));
198195
}
199196
savedListener.onAddresses(servers, Attributes.EMPTY);
200197
} finally {
@@ -205,6 +202,20 @@ public void run() {
205202
}
206203
};
207204

205+
private void handleError(Listener listener, Exception e) {
206+
synchronized (DnsNameResolver.this) {
207+
if (shutdown) {
208+
return;
209+
}
210+
// Because timerService is the single-threaded GrpcUtil.TIMER_SERVICE in production,
211+
// we need to delegate the blocking work to the executor
212+
resolutionTask =
213+
timerService.schedule(new LogExceptionRunnable(resolutionRunnableOnExecutor),
214+
1, TimeUnit.MINUTES);
215+
}
216+
listener.onError(Status.UNAVAILABLE.withCause(e));
217+
}
218+
208219
private final Runnable resolutionRunnableOnExecutor = new Runnable() {
209220
@Override
210221
public void run() {

0 commit comments

Comments
 (0)