Skip to content

Commit 888dc0c

Browse files
committed
Merge branch 'master' into pr/548
2 parents 5457a57 + ae68aa9 commit 888dc0c

File tree

5 files changed

+91
-8
lines changed

5 files changed

+91
-8
lines changed

grpc-client-spring-boot-autoconfigure/src/main/java/net/devh/boot/grpc/client/config/GrpcChannelProperties.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
import io.grpc.NameResolverProvider;
3838
import io.grpc.internal.GrpcUtil;
3939
import io.grpc.netty.shaded.io.grpc.netty.NettyChannelBuilder;
40-
import io.grpc.netty.shaded.io.grpc.netty.NettyServerBuilder;
4140
import io.grpc.netty.shaded.io.netty.handler.ssl.SslContextBuilder;
4241
import lombok.EqualsAndHashCode;
4342
import lombok.ToString;
@@ -182,7 +181,7 @@ public void setEnableKeepAlive(final Boolean enableKeepAlive) {
182181

183182
@DurationUnit(ChronoUnit.SECONDS)
184183
private Duration keepAliveTime;
185-
private static final Duration DEFAULT_KEEP_ALIVE_TIME = Duration.of(60, ChronoUnit.SECONDS);
184+
private static final Duration DEFAULT_KEEP_ALIVE_TIME = Duration.of(5, ChronoUnit.MINUTES);
186185

187186
/**
188187
* Gets the default delay before we send a keepAlive.
@@ -196,13 +195,14 @@ public Duration getKeepAliveTime() {
196195
}
197196

198197
/**
199-
* The default delay before we send a keepAlives. Defaults to {@code 60s}. Default unit {@link ChronoUnit#SECONDS
200-
* SECONDS}. Please note that shorter intervals increase the network burden for the server.
198+
* The default delay before we send a keepAlives. Defaults to {@code 5min}. Default unit {@link ChronoUnit#SECONDS
199+
* SECONDS}. Please note that shorter intervals increase the network burden for the server. Cannot be lower than
200+
* permitKeepAliveTime on server (default 5min).
201201
*
202202
* @param keepAliveTime The new default delay before sending keepAlives, or null to use the fallback.
203203
*
204204
* @see #setEnableKeepAlive(Boolean)
205-
* @see NettyServerBuilder#keepAliveTime(long, TimeUnit)
205+
* @see NettyChannelBuilder#keepAliveTime(long, TimeUnit)
206206
*/
207207
public void setKeepAliveTime(final Duration keepAliveTime) {
208208
this.keepAliveTime = keepAliveTime;
@@ -232,7 +232,7 @@ public Duration getKeepAliveTimeout() {
232232
* @param keepAliveTimeout The default timeout for a keepAlives ping request.
233233
*
234234
* @see #setEnableKeepAlive(Boolean)
235-
* @see NettyServerBuilder#keepAliveTimeout(long, TimeUnit)
235+
* @see NettyChannelBuilder#keepAliveTimeout(long, TimeUnit)
236236
*/
237237
public void setKeepAliveTimeout(final Duration keepAliveTimeout) {
238238
this.keepAliveTimeout = keepAliveTimeout;

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

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ public class GrpcServerProperties {
118118
private boolean enableKeepAlive = false;
119119

120120
/**
121-
* The default delay before we send a keepAlives. Defaults to {@code 60s}. Default unit {@link ChronoUnit#SECONDS
121+
* The default delay before we send a keepAlives. Defaults to {@code 2h}. Default unit {@link ChronoUnit#SECONDS
122122
* SECONDS}.
123123
*
124124
* @see #setEnableKeepAlive(boolean)
@@ -128,7 +128,7 @@ public class GrpcServerProperties {
128128
* @return The default delay before sending keepAlives.
129129
*/
130130
@DurationUnit(ChronoUnit.SECONDS)
131-
private Duration keepAliveTime = Duration.of(60, ChronoUnit.SECONDS);
131+
private Duration keepAliveTime = Duration.of(2, ChronoUnit.HOURS);
132132

133133
/**
134134
* The default timeout for a keepAlives ping request. Defaults to {@code 20s}. Default unit
@@ -167,6 +167,40 @@ public class GrpcServerProperties {
167167
@DurationUnit(ChronoUnit.SECONDS)
168168
private boolean permitKeepAliveWithoutCalls = false;
169169

170+
/**
171+
* Specify a max connection idle time. Defaults to disabled. Default unit {@link ChronoUnit#SECONDS SECONDS}.
172+
*
173+
* @see NettyServerBuilder#maxConnectionIdle(long, TimeUnit)
174+
*
175+
* @param maxConnectionIdle The max connection idle time.
176+
* @return The max connection idle time.
177+
*/
178+
@DurationUnit(ChronoUnit.SECONDS)
179+
private Duration maxConnectionIdle = null;
180+
181+
/**
182+
* Specify a max connection age. Defaults to disabled. Default unit {@link ChronoUnit#SECONDS SECONDS}.
183+
*
184+
* @see NettyServerBuilder#maxConnectionAge(long, TimeUnit)
185+
*
186+
* @param maxConnectionAge The max connection age.
187+
* @return The max connection age.
188+
*/
189+
@DurationUnit(ChronoUnit.SECONDS)
190+
private Duration maxConnectionAge = null;
191+
192+
/**
193+
* Specify a grace time for the graceful max connection age termination. Defaults to disabled. Default unit
194+
* {@link ChronoUnit#SECONDS SECONDS}.
195+
*
196+
* @see NettyServerBuilder#maxConnectionAgeGrace(long, TimeUnit)
197+
*
198+
* @param maxConnectionAgeGrace The max connection age grace time.
199+
* @return The max connection age grace time.
200+
*/
201+
@DurationUnit(ChronoUnit.SECONDS)
202+
private Duration maxConnectionAgeGrace = null;
203+
170204
/**
171205
* The maximum message size allowed to be received by the server. If not set ({@code null}) then
172206
* {@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: 21 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,26 @@ 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(
133+
"MaxConnectionIdle is set but this implementation does not support maxConnectionIdle!");
134+
}
135+
if (this.properties.getMaxConnectionAge() != null) {
136+
throw new IllegalStateException(
137+
"MaxConnectionAge is set but this implementation does not support maxConnectionAge!");
138+
}
139+
if (this.properties.getMaxConnectionAgeGrace() != null) {
140+
throw new IllegalStateException(
141+
"MaxConnectionAgeGrace is set but this implementation does not support maxConnectionAgeGrace!");
142+
}
143+
}
144+
124145
/**
125146
* Configures the security options that should be used by the server.
126147
*

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
@@ -88,6 +88,20 @@ protected void configureKeepAlive(final NettyServerBuilder builder) {
8888
.permitKeepAliveWithoutCalls(this.properties.isPermitKeepAliveWithoutCalls());
8989
}
9090

91+
@Override
92+
// Keep this in sync with ShadedNettyGrpcServerFactory#configureConnectionLimits
93+
protected void configureConnectionLimits(final NettyServerBuilder builder) {
94+
if (this.properties.getMaxConnectionIdle() != null) {
95+
builder.maxConnectionIdle(this.properties.getMaxConnectionIdle().toNanos(), TimeUnit.NANOSECONDS);
96+
}
97+
if (this.properties.getMaxConnectionAge() != null) {
98+
builder.maxConnectionAge(this.properties.getMaxConnectionAge().toNanos(), TimeUnit.NANOSECONDS);
99+
}
100+
if (this.properties.getMaxConnectionAgeGrace() != null) {
101+
builder.maxConnectionAgeGrace(this.properties.getMaxConnectionAgeGrace().toNanos(), TimeUnit.NANOSECONDS);
102+
}
103+
}
104+
91105
@Override
92106
// Keep this in sync with ShadedNettyGrpcServerFactory#configureSecurity
93107
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
@@ -78,6 +78,20 @@ protected NettyServerBuilder newServerBuilder() {
7878
}
7979
}
8080

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

0 commit comments

Comments
 (0)