Skip to content

Commit 00e54cb

Browse files
author
Ivan Lorca
committed
Bind to configured IP address on netty servers
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 <[email protected]> [resolves #319]
1 parent dc23afd commit 00e54cb

File tree

4 files changed

+65
-2
lines changed

4 files changed

+65
-2
lines changed

spring-grpc-core/src/main/java/org/springframework/grpc/server/DefaultGrpcServerFactory.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package org.springframework.grpc.server;
1818

19+
import java.net.InetSocketAddress;
20+
import java.net.SocketAddress;
1921
import java.util.LinkedHashSet;
2022
import java.util.LinkedList;
2123
import java.util.List;
@@ -31,6 +33,7 @@
3133

3234
import org.springframework.grpc.internal.GrpcUtils;
3335
import org.springframework.grpc.server.service.ServerInterceptorFilter;
36+
import org.springframework.util.StringUtils;
3437

3538
import io.grpc.Grpc;
3639
import io.grpc.InsecureServerCredentials;
@@ -190,4 +193,26 @@ protected void configureServices(T builder, List<ServerServiceDefinition> servic
190193
});
191194
}
192195

196+
protected String hostName() {
197+
return address().split(":")[0].trim();
198+
}
199+
200+
protected SocketAddress socketAddress() {
201+
String address = address();
202+
if (address.startsWith("unix:")) {
203+
throw new UnsupportedOperationException("Unix socket addresses not supported");
204+
}
205+
if (address.startsWith("in-process:")) {
206+
throw new UnsupportedOperationException("In-Process addresses not supported");
207+
}
208+
209+
var host = hostName();
210+
if (StringUtils.hasText(host) && !Objects.equals(host, "*")) {
211+
return new InetSocketAddress(host, port());
212+
}
213+
else {
214+
return new InetSocketAddress(port());
215+
}
216+
}
217+
193218
}

spring-grpc-core/src/main/java/org/springframework/grpc/server/NettyGrpcServerFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ protected NettyServerBuilder newServerBuilder() {
5656
.bossEventLoopGroup(new MultiThreadIoEventLoopGroup(1, EpollIoHandler.newFactory()))
5757
.workerEventLoopGroup(new MultiThreadIoEventLoopGroup(EpollIoHandler.newFactory()));
5858
}
59-
return super.newServerBuilder();
59+
return NettyServerBuilder.forAddress(socketAddress(), credentials());
6060
}
6161

6262
}

spring-grpc-core/src/main/java/org/springframework/grpc/server/ShadedNettyGrpcServerFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ protected NettyServerBuilder newServerBuilder() {
5555
.bossEventLoopGroup(new EpollEventLoopGroup(1))
5656
.workerEventLoopGroup(new EpollEventLoopGroup());
5757
}
58-
return super.newServerBuilder();
58+
return NettyServerBuilder.forAddress(socketAddress(), credentials());
5959
}
6060

6161
}

spring-grpc-core/src/test/java/org/springframework/grpc/server/DefaultGrpcServerFactoryTests.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,18 @@
1717
package org.springframework.grpc.server;
1818

1919
import static org.assertj.core.api.Assertions.assertThat;
20+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
2021
import static org.mockito.Mockito.mock;
2122

23+
import java.net.InetSocketAddress;
24+
import java.net.SocketAddress;
2225
import java.util.List;
2326

2427
import org.assertj.core.api.InstanceOfAssertFactories;
28+
import org.junit.jupiter.api.DynamicTest;
2529
import org.junit.jupiter.api.Nested;
2630
import org.junit.jupiter.api.Test;
31+
import org.junit.jupiter.api.TestFactory;
2732

2833
import io.grpc.ServerServiceDefinition;
2934

@@ -83,4 +88,37 @@ void whenFilterAllowsOneThenOneServiceAdded() {
8388

8489
}
8590

91+
@Nested
92+
class AddressParser {
93+
94+
@TestFactory
95+
List<DynamicTest> ipAddress() {
96+
return List.of(testIpAddress(":9999", new InetSocketAddress(9999)),
97+
testIpAddress("localhost:9999", new InetSocketAddress("localhost", 9999)),
98+
testIpAddress("localhost", new InetSocketAddress("localhost", 9090)),
99+
testIpAddress("*", new InetSocketAddress(9090)),
100+
testIpAddress("*:8888", new InetSocketAddress(8888)),
101+
testIpAddress("", new InetSocketAddress(9090)));
102+
}
103+
104+
private DynamicTest testIpAddress(String address, SocketAddress expected) {
105+
return DynamicTest.dynamicTest("Socket address: " + address, () -> {
106+
var factory = new DefaultGrpcServerFactory<>(address, List.of());
107+
assertThat(factory.socketAddress()).isEqualTo(expected);
108+
});
109+
}
110+
111+
@TestFactory
112+
List<DynamicTest> unsupportedAddress() {
113+
return List.of(testThrows("unix:dummy"), testThrows("in-process:"));
114+
}
115+
116+
private DynamicTest testThrows(String address) {
117+
return DynamicTest.dynamicTest("Socket address: " + address,
118+
() -> assertThatExceptionOfType(UnsupportedOperationException.class)
119+
.isThrownBy(() -> new DefaultGrpcServerFactory<>(address, List.of()).socketAddress()));
120+
}
121+
122+
}
123+
86124
}

0 commit comments

Comments
 (0)