aboutsummaryrefslogtreecommitdiff
path: root/core
diff options
context:
space:
mode:
authorzpencer <spencerfang@google.com>2018-09-28 16:10:14 -0700
committerGitHub <noreply@github.com>2018-09-28 16:10:14 -0700
commit2fae9a3a97115a98ec9b29daa9489c2f8ea5752f (patch)
treee853b6b7415b6819d384885559a1f9e945f711a2 /core
parent8b16899bc1f4ffc22b899cea7724cf2eebd348f8 (diff)
downloadgrpc-grpc-java-2fae9a3a97115a98ec9b29daa9489c2f8ea5752f.tar.gz
core: permanently store authority at channel creation (#4886)
Getting the authority must not rely on the name resolver being non-null, because that can trivially happen if the channel is shut down.
Diffstat (limited to 'core')
-rw-r--r--core/src/main/java/io/grpc/internal/ManagedChannelImpl.java13
-rw-r--r--core/src/test/java/io/grpc/internal/ManagedChannelImplTest.java8
2 files changed, 18 insertions, 3 deletions
diff --git a/core/src/main/java/io/grpc/internal/ManagedChannelImpl.java b/core/src/main/java/io/grpc/internal/ManagedChannelImpl.java
index dc582b9d5..0278ce42a 100644
--- a/core/src/main/java/io/grpc/internal/ManagedChannelImpl.java
+++ b/core/src/main/java/io/grpc/internal/ManagedChannelImpl.java
@@ -576,7 +576,7 @@ final class ManagedChannelImpl extends ManagedChannel implements
this.retryEnabled = builder.retryEnabled && !builder.temporarilyDisableRetry;
serviceConfigInterceptor = new ServiceConfigInterceptor(
retryEnabled, builder.maxRetryAttempts, builder.maxHedgedAttempts);
- Channel channel = new RealChannel();
+ Channel channel = new RealChannel(nameResolver.getServiceAuthority());
channel = ClientInterceptors.intercept(channel, serviceConfigInterceptor);
if (builder.binlog != null) {
channel = builder.binlog.wrapChannel(channel);
@@ -810,6 +810,14 @@ final class ManagedChannelImpl extends ManagedChannel implements
}
private class RealChannel extends Channel {
+ // Set when the NameResolver is initially created. When we create a new NameResolver for the
+ // same target, the new instance must have the same value.
+ private final String authority;
+
+ private RealChannel(String authority) {
+ this.authority = checkNotNull(authority, "authority");
+ }
+
@Override
public <ReqT, RespT> ClientCall<ReqT, RespT> newCall(MethodDescriptor<ReqT, RespT> method,
CallOptions callOptions) {
@@ -828,8 +836,7 @@ final class ManagedChannelImpl extends ManagedChannel implements
@Override
public String authority() {
- String authority = nameResolver.getServiceAuthority();
- return checkNotNull(authority, "authority");
+ return authority;
}
}
diff --git a/core/src/test/java/io/grpc/internal/ManagedChannelImplTest.java b/core/src/test/java/io/grpc/internal/ManagedChannelImplTest.java
index 74e95c88e..248eb66f0 100644
--- a/core/src/test/java/io/grpc/internal/ManagedChannelImplTest.java
+++ b/core/src/test/java/io/grpc/internal/ManagedChannelImplTest.java
@@ -2801,6 +2801,14 @@ public class ManagedChannelImplTest {
mychannel.shutdownNow();
}
+ @Test
+ public void getAuthorityAfterShutdown() throws Exception {
+ createChannel();
+ assertEquals(SERVICE_NAME, channel.authority());
+ channel.shutdownNow().awaitTermination(1, TimeUnit.SECONDS);
+ assertEquals(SERVICE_NAME, channel.authority());
+ }
+
private static final class ChannelBuilder
extends AbstractManagedChannelImplBuilder<ChannelBuilder> {