Skip to content

Conversation

@speevy
Copy link

@speevy speevy commented Dec 5, 2025

Replace call to super on the NettyGrpcServer and ShaddedNettyGrpcServer newServerBuilder() method, with a call to the NettyServerBuilder.forAddress that allows defining the IP and port to be bound to using a SocketAddress.

The glue logic for getting the SocketAddress from the configured address string is in the DefaultGrpcServerFactory as is common to both inheritor classes.

Signed-off-by: Ivan Lorca ivan.lorca@spacewell.com

[resolves #319]

Replace call to super on the `NettyGrpcServer` and `ShaddedNettyGrpcServer`
`newServerBuilder()` method, with a call to the `NettyServerBuilder.forAddress`
that allows defining the IP and port to be bound to using a `SocketAddress`.

The glue logic for getting the `SocketAddress` from the configured address string
is in the `DefaultGrpcServerFactory` as is common to both inheritor classes.

Signed-off-by: Ivan Lorca <ivan.lorca@spacewell.com>

[resolves spring-projects#319]
Copy link
Contributor

@onobc onobc left a comment

Choose a reason for hiding this comment

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

Thanks for the bug find and contribution/fix @speevy !

I have a couple of comments / suggestions. I look forward to hearing back.

Thanks,
Chris

testIpAddress("", new InetSocketAddress(9090)));
}

private DynamicTest testIpAddress(String address, SocketAddress expected) {
Copy link
Contributor

Choose a reason for hiding this comment

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

🎉 nice use of DynamicTest!

.workerEventLoopGroup(new EpollEventLoopGroup());
}
return super.newServerBuilder();
return NettyServerBuilder.forAddress(socketAddress(), credentials());
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this replacement makes sense and is "safe" as the previous code called:

Grpc.newServerBuilderForPort(port(), credentials());

which ultimately called:

ProtocolNegotiators.FromServerCredentialsResult result = ProtocolNegotiators.from(creds);
if (result.error != null) {
    return NewServerBuilderResult.error(result.error);
}
return NewServerBuilderResult.serverBuilder(
        new NettyServerBuilder(new InetSocketAddress(port), result.negotiator));

and the new code NettyServerBuilder.forAddress(socketAddress(), credentials()); does the same effectively but passes in the hostname not just the port.

Previously we were simply disregarding any host info (as noted in your issue).

return address().split(":")[0].trim();
}

protected SocketAddress socketAddress() {
Copy link
Contributor

Choose a reason for hiding this comment

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

I would prefer moving this to GrpcUtils like we do for port. Then rather than super.hostname the subs could call GrpcUtils.hostname.

if (address.startsWith("unix:")) {
throw new UnsupportedOperationException("Unix socket addresses not supported");
}
if (address.startsWith("in-process:")) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think in-process: string every makes it to the server side and is soley used on the client/channel side (i.e. its stripped by the time it gets here).

}

protected String hostName() {
return address().split(":")[0].trim();
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think this will handle all cases as users could pass in arbitrary addresses (e.g. 12::34::....). I think we need to use a regex to cover all cases. Wdyt?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

When using the netty server, it always binds to all interfaces

2 participants