Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ public GrpcCallContext getEmptyCallContext() {
/** The channel in use. */
abstract ManagedChannel getManagedChannel();

public abstract boolean isDirectPath();

public Channel getChannel() {
return getManagedChannel();
}
Expand Down Expand Up @@ -100,7 +102,7 @@ public void close() {
}

public static Builder newBuilder() {
return new AutoValue_GrpcTransportChannel.Builder();
return new AutoValue_GrpcTransportChannel.Builder().setDirectPath(false);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add a test case for the default value here?

}

public static GrpcTransportChannel create(ManagedChannel channel) {
Expand All @@ -111,6 +113,8 @@ public static GrpcTransportChannel create(ManagedChannel channel) {
public abstract static class Builder {
public abstract Builder setManagedChannel(ManagedChannel value);

abstract Builder setDirectPath(boolean value);

public abstract GrpcTransportChannel build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -258,9 +258,12 @@ public TransportChannel getTransportChannel() throws IOException {
}

private TransportChannel createChannel() throws IOException {
return GrpcTransportChannel.create(
ChannelPool.create(
channelPoolSettings, InstantiatingGrpcChannelProvider.this::createSingleChannel));
return GrpcTransportChannel.newBuilder()
.setManagedChannel(
ChannelPool.create(
channelPoolSettings, InstantiatingGrpcChannelProvider.this::createSingleChannel))
.setDirectPath(this.canUseDirectPath())
.build();
}

private boolean isDirectPathEnabled() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -706,7 +706,7 @@ void testLogDirectPathMisconfigNotOnGCE() throws Exception {
}

@Test
public void canUseDirectPath_happyPath() {
public void canUseDirectPath_happyPath() throws IOException {
System.setProperty("os.name", "Linux");
EnvironmentProvider envProvider = Mockito.mock(EnvironmentProvider.class);
Mockito.when(
Expand All @@ -718,14 +718,20 @@ public void canUseDirectPath_happyPath() {
.setAttemptDirectPath(true)
.setCredentials(computeEngineCredentials)
.setEndpoint(DEFAULT_ENDPOINT)
.setEnvProvider(envProvider);
.setEnvProvider(envProvider)
.setHeaderProvider(Mockito.mock(HeaderProvider.class));
InstantiatingGrpcChannelProvider provider =
new InstantiatingGrpcChannelProvider(builder, GCE_PRODUCTION_NAME_AFTER_2016);
Truth.assertThat(provider.canUseDirectPath()).isTrue();

// verify this info is passed correctly to transport channel
TransportChannel transportChannel = provider.getTransportChannel();
Truth.assertThat(((GrpcTransportChannel) transportChannel).isDirectPath()).isTrue();
transportChannel.shutdownNow();
}

@Test
public void canUseDirectPath_directPathEnvVarDisabled() {
public void canUseDirectPath_directPathEnvVarDisabled() throws IOException {
System.setProperty("os.name", "Linux");
EnvironmentProvider envProvider = Mockito.mock(EnvironmentProvider.class);
Mockito.when(
Expand All @@ -737,10 +743,16 @@ public void canUseDirectPath_directPathEnvVarDisabled() {
.setAttemptDirectPath(true)
.setCredentials(computeEngineCredentials)
.setEndpoint(DEFAULT_ENDPOINT)
.setEnvProvider(envProvider);
.setEnvProvider(envProvider)
.setHeaderProvider(Mockito.mock(HeaderProvider.class));
InstantiatingGrpcChannelProvider provider =
new InstantiatingGrpcChannelProvider(builder, GCE_PRODUCTION_NAME_AFTER_2016);
Truth.assertThat(provider.canUseDirectPath()).isFalse();

// verify this info is passed correctly to transport channel
TransportChannel transportChannel = provider.getTransportChannel();
Truth.assertThat(((GrpcTransportChannel) transportChannel).isDirectPath()).isFalse();
transportChannel.shutdownNow();
}

@Test
Expand Down