Fixes to allow non-test inprocess channels

- Fix @LocalGrpcPort to ignore the inprocess factory port
- Don't set load balancer policy for inprocess channel factory

Signed-off-by: Chris Bono <chris.bono@gmail.com>
This commit is contained in:
Chris Bono
2025-06-07 14:48:12 -05:00
committed by Dave Syer
parent a087eac776
commit 171f2556df
4 changed files with 30 additions and 1 deletions

View File

@@ -338,6 +338,23 @@ class GrpcServerIntegrationTests {
}
@Nested
@SpringBootTest(properties = { "spring.grpc.server.inprocess.name=foo", "spring.grpc.server.host=0.0.0.0",
"spring.grpc.server.port=0" })
class ServerWithRegularAndInProcessChannelsAndFactories {
@Test
void servesResponseToNonInProcessClient(@Autowired GrpcChannelFactory channels, @LocalGrpcPort int port) {
assertThatResponseIsServedToChannel(channels.createChannel("0.0.0.0:" + port));
}
@Test
void servesResponseToInProcessClient(@Autowired GrpcChannelFactory channels) {
assertThatResponseIsServedToChannel(channels.createChannel("in-process:foo"));
}
}
private void assertThatResponseIsServedToChannel(ManagedChannel clientChannel) {
SimpleGrpc.SimpleBlockingStub client = SimpleGrpc.newBlockingStub(clientChannel);
HelloReply response = client.sayHello(HelloRequest.newBuilder().setName("Alien").build());

View File

@@ -107,6 +107,14 @@ public class GrpcServerLifecycle implements SmartLifecycle {
return this.server == null ? 0 : this.server.getPort();
}
/**
* Gets the server factory used to create the server.
* @return the server factory to create the server
*/
public GrpcServerFactory getFactory() {
return this.factory;
}
/**
* Creates and starts the grpc server.
* @throws IOException If the server is unable to bind the port.

View File

@@ -49,7 +49,7 @@ class ClientPropertiesChannelBuilderCustomizer<T extends ManagedChannelBuilder<T
ChannelConfig channel = this.properties.getChannel(authority);
PropertyMapper mapper = PropertyMapper.get().alwaysApplyingWhenNonNull();
mapper.from(channel.getUserAgent()).to(builder::userAgent);
if (!authority.startsWith("unix:")) {
if (!authority.startsWith("unix:") && !authority.startsWith("in-process:")) {
mapper.from(channel.getDefaultLoadBalancingPolicy()).to(builder::defaultLoadBalancingPolicy);
}
mapper.from(channel.getMaxInboundMessageSize()).asInt(DataSize::toBytes).to(builder::maxInboundMessageSize);

View File

@@ -26,6 +26,7 @@ import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MapPropertySource;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.PropertySource;
import org.springframework.grpc.server.InProcessGrpcServerFactory;
import org.springframework.grpc.server.lifecycle.GrpcServerStartedEvent;
public class ServerPortInfoApplicationContextInitializer implements
@@ -43,6 +44,9 @@ public class ServerPortInfoApplicationContextInitializer implements
@Override
public void onApplicationEvent(GrpcServerStartedEvent event) {
if (event.getSource().getFactory() instanceof InProcessGrpcServerFactory) {
return;
}
String propertyName = "local.grpc.port";
setPortProperty(applicationContext, propertyName, event.getPort());
}