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;
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;
}
}

View File

@ -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> {