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.
This commit is contained in:
zpencer 2018-09-28 16:10:14 -07:00 committed by GitHub
parent 8b16899bc1
commit 2fae9a3a97
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 3 deletions

View File

@ -576,7 +576,7 @@ final class ManagedChannelImpl extends ManagedChannel implements
this.retryEnabled = builder.retryEnabled && !builder.temporarilyDisableRetry; this.retryEnabled = builder.retryEnabled && !builder.temporarilyDisableRetry;
serviceConfigInterceptor = new ServiceConfigInterceptor( serviceConfigInterceptor = new ServiceConfigInterceptor(
retryEnabled, builder.maxRetryAttempts, builder.maxHedgedAttempts); retryEnabled, builder.maxRetryAttempts, builder.maxHedgedAttempts);
Channel channel = new RealChannel(); Channel channel = new RealChannel(nameResolver.getServiceAuthority());
channel = ClientInterceptors.intercept(channel, serviceConfigInterceptor); channel = ClientInterceptors.intercept(channel, serviceConfigInterceptor);
if (builder.binlog != null) { if (builder.binlog != null) {
channel = builder.binlog.wrapChannel(channel); channel = builder.binlog.wrapChannel(channel);
@ -810,6 +810,14 @@ final class ManagedChannelImpl extends ManagedChannel implements
} }
private class RealChannel extends Channel { 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 @Override
public <ReqT, RespT> ClientCall<ReqT, RespT> newCall(MethodDescriptor<ReqT, RespT> method, public <ReqT, RespT> ClientCall<ReqT, RespT> newCall(MethodDescriptor<ReqT, RespT> method,
CallOptions callOptions) { CallOptions callOptions) {
@ -828,8 +836,7 @@ final class ManagedChannelImpl extends ManagedChannel implements
@Override @Override
public String authority() { public String authority() {
String authority = nameResolver.getServiceAuthority(); return authority;
return checkNotNull(authority, "authority");
} }
} }

View File

@ -2801,6 +2801,14 @@ public class ManagedChannelImplTest {
mychannel.shutdownNow(); 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 private static final class ChannelBuilder
extends AbstractManagedChannelImplBuilder<ChannelBuilder> { extends AbstractManagedChannelImplBuilder<ChannelBuilder> {