Skip to content

Commit 24ce20b

Browse files
committed
Support max connection idle and age limits
1 parent ed6a0e5 commit 24ce20b

File tree

4 files changed

+80
-0
lines changed

4 files changed

+80
-0
lines changed

grpc-server-spring-boot-autoconfigure/src/main/java/net/devh/boot/grpc/server/config/GrpcServerProperties.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,40 @@ public class GrpcServerProperties {
162162
@DurationUnit(ChronoUnit.SECONDS)
163163
private boolean permitKeepAliveWithoutCalls = false;
164164

165+
/**
166+
* Specify a max connection idle time. Defaults to disabled. Default unit {@link ChronoUnit#SECONDS SECONDS}.
167+
*
168+
* @see NettyServerBuilder#maxConnectionIdle(long, TimeUnit)
169+
*
170+
* @param maxConnectionIdle The max connection idle time.
171+
* @return The max connection idle time.
172+
*/
173+
@DurationUnit(ChronoUnit.SECONDS)
174+
private Duration maxConnectionIdle = null;
175+
176+
/**
177+
* Specify a max connection age. Defaults to disabled. Default unit {@link ChronoUnit#SECONDS SECONDS}.
178+
*
179+
* @see NettyServerBuilder#maxConnectionAge(long, TimeUnit)
180+
*
181+
* @param maxConnectionAge The max connection age.
182+
* @return The max connection age.
183+
*/
184+
@DurationUnit(ChronoUnit.SECONDS)
185+
private Duration maxConnectionAge = null;
186+
187+
/**
188+
* Specify a grace time for the graceful max connection age termination. Defaults to disabled. Default unit
189+
* {@link ChronoUnit#SECONDS SECONDS}.
190+
*
191+
* @see NettyServerBuilder#maxConnectionAgeGrace(long, TimeUnit)
192+
*
193+
* @param maxConnectionAgeGrace The max connection age grace time.
194+
* @return The max connection age grace time.
195+
*/
196+
@DurationUnit(ChronoUnit.SECONDS)
197+
private Duration maxConnectionAgeGrace = null;
198+
165199
/**
166200
* The maximum message size allowed to be received by the server. If not set ({@code null}) then
167201
* {@link GrpcUtil#DEFAULT_MAX_MESSAGE_SIZE gRPC's default} should be used.

grpc-server-spring-boot-autoconfigure/src/main/java/net/devh/boot/grpc/server/serverfactory/AbstractGrpcServerFactory.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ public Server createServer() {
8484
protected void configure(final T builder) {
8585
configureServices(builder);
8686
configureKeepAlive(builder);
87+
configureConnectionLimits(builder);
8788
configureSecurity(builder);
8889
configureLimits(builder);
8990
for (final GrpcServerConfigurer serverConfigurer : this.serverConfigurers) {
@@ -121,6 +122,23 @@ protected void configureKeepAlive(final T builder) {
121122
}
122123
}
123124

125+
/**
126+
* Configures the keep alive options that should be used by the server.
127+
*
128+
* @param builder The server builder to configure.
129+
*/
130+
protected void configureConnectionLimits(final T builder) {
131+
if (this.properties.getMaxConnectionIdle() != null) {
132+
throw new IllegalStateException("MaxConnectionIdle is set but this implementation does not support maxConnectionIdle!");
133+
}
134+
if (this.properties.getMaxConnectionAge() != null) {
135+
throw new IllegalStateException("MaxConnectionAge is set but this implementation does not support maxConnectionAge!");
136+
}
137+
if (this.properties.getMaxConnectionAgeGrace() != null) {
138+
throw new IllegalStateException("MaxConnectionAgeGrace is set but this implementation does not support maxConnectionAgeGrace!");
139+
}
140+
}
141+
124142
/**
125143
* Configures the security options that should be used by the server.
126144
*

grpc-server-spring-boot-autoconfigure/src/main/java/net/devh/boot/grpc/server/serverfactory/NettyGrpcServerFactory.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,20 @@ protected void configureKeepAlive(final NettyServerBuilder builder) {
7979
.permitKeepAliveWithoutCalls(this.properties.isPermitKeepAliveWithoutCalls());
8080
}
8181

82+
@Override
83+
// Keep this in sync with ShadedNettyGrpcServerFactory#configureConnectionLimits
84+
protected void configureConnectionLimits(final NettyServerBuilder builder) {
85+
if (this.properties.getMaxConnectionIdle() != null) {
86+
builder.maxConnectionIdle(this.properties.getMaxConnectionIdle().toNanos(), TimeUnit.NANOSECONDS);
87+
}
88+
if (this.properties.getMaxConnectionAge() != null) {
89+
builder.maxConnectionAge(this.properties.getMaxConnectionAge().toNanos(), TimeUnit.NANOSECONDS);
90+
}
91+
if (this.properties.getMaxConnectionAgeGrace() != null) {
92+
builder.maxConnectionAgeGrace(this.properties.getMaxConnectionAgeGrace().toNanos(), TimeUnit.NANOSECONDS);
93+
}
94+
}
95+
8296
@Override
8397
// Keep this in sync with ShadedNettyGrpcServerFactory#configureSecurity
8498
protected void configureSecurity(final NettyServerBuilder builder) {

grpc-server-spring-boot-autoconfigure/src/main/java/net/devh/boot/grpc/server/serverfactory/ShadedNettyGrpcServerFactory.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,20 @@ protected NettyServerBuilder newServerBuilder() {
6969
}
7070
}
7171

72+
@Override
73+
// Keep this in sync with NettyGrpcServerFactory#configureConnectionLimits
74+
protected void configureConnectionLimits(final NettyServerBuilder builder) {
75+
if (this.properties.getMaxConnectionIdle() != null) {
76+
builder.maxConnectionIdle(this.properties.getMaxConnectionIdle().toNanos(), TimeUnit.NANOSECONDS);
77+
}
78+
if (this.properties.getMaxConnectionAge() != null) {
79+
builder.maxConnectionAge(this.properties.getMaxConnectionAge().toNanos(), TimeUnit.NANOSECONDS);
80+
}
81+
if (this.properties.getMaxConnectionAgeGrace() != null) {
82+
builder.maxConnectionAgeGrace(this.properties.getMaxConnectionAgeGrace().toNanos(), TimeUnit.NANOSECONDS);
83+
}
84+
}
85+
7286
@Override
7387
// Keep this in sync with NettyGrpcServerFactory#configureKeepAlive
7488
protected void configureKeepAlive(final NettyServerBuilder builder) {

0 commit comments

Comments
 (0)