Skip to content

Commit 2ac1eea

Browse files
committed
PR comments
- PairSocketAddress - use correct DnsNameResolver error handling - let ProxyDetector be passed in as getNameResolverParams
1 parent 5005b1d commit 2ac1eea

File tree

7 files changed

+71
-38
lines changed

7 files changed

+71
-38
lines changed

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@
2525
import io.grpc.EquivalentAddressGroup;
2626
import io.grpc.NameResolver;
2727
import io.grpc.Status;
28-
import io.grpc.internal.ProxyDetector.ProxiedInetSocketAddress;
2928
import io.grpc.internal.SharedResourceHolder.Resource;
29+
import java.io.IOException;
3030
import java.net.InetAddress;
3131
import java.net.InetSocketAddress;
3232
import java.net.SocketAddress;
@@ -149,14 +149,20 @@ public void run() {
149149
ProxyParameters proxy;
150150
try {
151151
proxy = proxyDetector.proxyFor(destination);
152-
} catch (Exception e) {
152+
} catch (IOException e) {
153153
savedListener.onError(
154154
Status.UNAVAILABLE.withDescription("Unable to resolve host " + host).withCause(e));
155155
return;
156156
}
157157
if (proxy != null) {
158158
EquivalentAddressGroup server =
159-
new EquivalentAddressGroup(new ProxiedInetSocketAddress(destination, proxy));
159+
new EquivalentAddressGroup(
160+
new PairSocketAddress(
161+
destination,
162+
Attributes
163+
.newBuilder()
164+
.set(ProxyDetector.PROXY_PARAMS_KEY, proxy)
165+
.build()));
160166
savedListener.onAddresses(Collections.singletonList(server), Attributes.EMPTY);
161167
return;
162168
}

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@
3737
* </ul>
3838
*/
3939
public final class DnsNameResolverProvider extends NameResolverProvider {
40+
public static final Attributes.Key<ProxyDetector> PROXY_DETECTOR_KEY
41+
= Attributes.Key.of("proxy-detector-key");
4042

4143
private static final String SCHEME = "dns";
4244

@@ -47,12 +49,17 @@ public DnsNameResolver newNameResolver(URI targetUri, Attributes params) {
4749
Preconditions.checkArgument(targetPath.startsWith("/"),
4850
"the path component (%s) of the target (%s) must start with '/'", targetPath, targetUri);
4951
String name = targetPath.substring(1);
52+
// We want to eventually let the ProxyDetector be passed in from ManagedChannel
53+
ProxyDetector proxyDetector = params.get(PROXY_DETECTOR_KEY);
54+
if (proxyDetector == null) {
55+
proxyDetector = GrpcUtil.getDefaultProxyDetector();
56+
}
5057
return new DnsNameResolver(
5158
targetUri.getAuthority(),
5259
name,
5360
params,
5461
GrpcUtil.SHARED_CHANNEL_EXECUTOR,
55-
GrpcUtil.getProxyDetector());
62+
proxyDetector);
5663
} else {
5764
return null;
5865
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ public ProxyParameters proxyFor(SocketAddress targetServerAddress) {
249249
/**
250250
* Returns a proxy detector appropriate for the current environment.
251251
*/
252-
public static ProxyDetector getProxyDetector() {
252+
public static ProxyDetector getDefaultProxyDetector() {
253253
if (IS_RESTRICTED_APPENGINE) {
254254
return NOOP_PROXY_DETECTOR;
255255
} else {

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
import io.grpc.MethodDescriptor;
3838
import io.grpc.Status;
3939
import io.grpc.internal.Channelz.ChannelStats;
40-
import io.grpc.internal.ProxyDetector.ProxiedInetSocketAddress;
4140
import java.net.SocketAddress;
4241
import java.util.ArrayList;
4342
import java.util.Collection;
@@ -212,9 +211,9 @@ private void startNewTransport() {
212211
SocketAddress address = addrs.get(addressIndex);
213212

214213
ProxyParameters proxy = null;
215-
if (address instanceof ProxiedInetSocketAddress) {
216-
proxy = ((ProxiedInetSocketAddress) address).getProxyParameters();
217-
address = ((ProxiedInetSocketAddress) address).getDestination();
214+
if (address instanceof PairSocketAddress) {
215+
proxy = ((PairSocketAddress) address).getAttributes().get(ProxyDetector.PROXY_PARAMS_KEY);
216+
address = ((PairSocketAddress) address).getAddress();
218217
}
219218

220219
ConnectionClientTransport transport =
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright 2018, gRPC Authors All rights reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.grpc.internal;
18+
19+
import com.google.common.annotations.VisibleForTesting;
20+
import io.grpc.Attributes;
21+
import java.net.SocketAddress;
22+
23+
/**
24+
* A data structure to associate a {@link SocketAddress} with {@link Attributes}.
25+
*/
26+
final class PairSocketAddress extends SocketAddress {
27+
private static final long serialVersionUID = -6854992294603212793L;
28+
29+
private final SocketAddress address;
30+
private final Attributes attributes;
31+
32+
@VisibleForTesting
33+
PairSocketAddress(SocketAddress address, Attributes attributes) {
34+
this.address = address;
35+
this.attributes = attributes;
36+
}
37+
38+
public Attributes getAttributes() {
39+
return attributes;
40+
}
41+
42+
public SocketAddress getAddress() {
43+
return address;
44+
}
45+
}

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

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,8 @@
1616

1717
package io.grpc.internal;
1818

19-
import com.google.common.annotations.VisibleForTesting;
19+
import io.grpc.Attributes;
2020
import java.io.IOException;
21-
import java.net.InetSocketAddress;
2221
import java.net.SocketAddress;
2322
import javax.annotation.Nullable;
2423

@@ -29,34 +28,12 @@
2928
* {@link io.grpc.NameResolver}.
3029
*/
3130
public interface ProxyDetector {
31+
Attributes.Key<ProxyParameters> PROXY_PARAMS_KEY = Attributes.Key.of("proxy-params-key");
3232
/**
3333
* Given a target address, returns which proxy address should be used. If no proxy should be
3434
* used, then return value will be null. The address of the {@link ProxyParameters} is always
3535
* resolved. This throws if the proxy address cannot be resolved.
3636
*/
3737
@Nullable
3838
ProxyParameters proxyFor(SocketAddress targetServerAddress) throws IOException;
39-
40-
// This class does not extend from InetSocketAddress because there is no public constructor
41-
// that allows subclass instances be unresolved.
42-
final class ProxiedInetSocketAddress extends SocketAddress {
43-
private static final long serialVersionUID = -6854992294603212793L;
44-
45-
private final InetSocketAddress destination;
46-
private final ProxyParameters proxyParams;
47-
48-
@VisibleForTesting
49-
public ProxiedInetSocketAddress(InetSocketAddress destination, ProxyParameters proxyParams) {
50-
this.destination = destination;
51-
this.proxyParams = proxyParams;
52-
}
53-
54-
public ProxyParameters getProxyParameters() {
55-
return proxyParams;
56-
}
57-
58-
public InetSocketAddress getDestination() {
59-
return destination;
60-
}
61-
}
6239
}

core/src/test/java/io/grpc/internal/DnsNameResolverTest.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
import io.grpc.NameResolver;
3737
import io.grpc.internal.DnsNameResolver.DelegateResolver;
3838
import io.grpc.internal.DnsNameResolver.ResolutionResults;
39-
import io.grpc.internal.ProxyDetector.ProxiedInetSocketAddress;
4039
import io.grpc.internal.SharedResourceHolder.Resource;
4140
import java.net.InetAddress;
4241
import java.net.InetSocketAddress;
@@ -266,9 +265,9 @@ public void doNotResolveWhenProxyDetected() throws Exception {
266265
EquivalentAddressGroup eag = result.get(0);
267266
assertThat(eag.getAddresses()).hasSize(1);
268267

269-
ProxiedInetSocketAddress socketAddress = (ProxiedInetSocketAddress) eag.getAddresses().get(0);
270-
assertSame(proxyParameters, socketAddress.getProxyParameters());
271-
assertTrue(socketAddress.getDestination().isUnresolved());
268+
PairSocketAddress socketAddress = (PairSocketAddress) eag.getAddresses().get(0);
269+
assertSame(proxyParameters, socketAddress.getAttributes().get(ProxyDetector.PROXY_PARAMS_KEY));
270+
assertTrue(((InetSocketAddress) socketAddress.getAddress()).isUnresolved());
272271
}
273272

274273
private void testInvalidUri(URI uri) {

0 commit comments

Comments
 (0)