Skip to content

Commit ba57a1d

Browse files
committed
api: Add connection management APIs to ServerBuilder
These APIs were added to NettyServerBuilder for gRFC A8 and A9. They are important enough that they shouldn't require using the perma-unstable transport API to access. This change also allows using these methods with grpc-netty-shaded. Fixes grpc#8991
1 parent 78067cb commit ba57a1d

File tree

4 files changed

+196
-0
lines changed

4 files changed

+196
-0
lines changed

api/src/main/java/io/grpc/ForwardingServerBuilder.java

+42
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,48 @@ public T handshakeTimeout(long timeout, TimeUnit unit) {
133133
return thisT();
134134
}
135135

136+
@Override
137+
public T keepAliveTime(long keepAliveTime, TimeUnit timeUnit) {
138+
delegate().keepAliveTime(keepAliveTime, timeUnit);
139+
return thisT();
140+
}
141+
142+
@Override
143+
public T keepAliveTimeout(long keepAliveTimeout, TimeUnit timeUnit) {
144+
delegate().keepAliveTimeout(keepAliveTimeout, timeUnit);
145+
return thisT();
146+
}
147+
148+
@Override
149+
public T maxConnectionIdle(long maxConnectionIdle, TimeUnit timeUnit) {
150+
delegate().maxConnectionIdle(maxConnectionIdle, timeUnit);
151+
return thisT();
152+
}
153+
154+
@Override
155+
public T maxConnectionAge(long maxConnectionAge, TimeUnit timeUnit) {
156+
delegate().maxConnectionAge(maxConnectionAge, timeUnit);
157+
return thisT();
158+
}
159+
160+
@Override
161+
public T maxConnectionAgeGrace(long maxConnectionAgeGrace, TimeUnit timeUnit) {
162+
delegate().maxConnectionAgeGrace(maxConnectionAgeGrace, timeUnit);
163+
return thisT();
164+
}
165+
166+
@Override
167+
public T permitKeepAliveTime(long keepAliveTime, TimeUnit timeUnit) {
168+
delegate().permitKeepAliveTime(keepAliveTime, timeUnit);
169+
return thisT();
170+
}
171+
172+
@Override
173+
public T permitKeepAliveWithoutCalls(boolean permit) {
174+
delegate().permitKeepAliveWithoutCalls(permit);
175+
return thisT();
176+
}
177+
136178
@Override
137179
public T maxInboundMessageSize(int bytes) {
138180
delegate().maxInboundMessageSize(bytes);

api/src/main/java/io/grpc/ServerBuilder.java

+105
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,111 @@ public T handshakeTimeout(long timeout, TimeUnit unit) {
243243
throw new UnsupportedOperationException();
244244
}
245245

246+
/**
247+
* Sets the time without read activity before sending a keepalive ping. An unreasonably small
248+
* value might be increased, and {@code Long.MAX_VALUE} nano seconds or an unreasonably large
249+
* value will disable keepalive. The typical default is infinite when supported.
250+
*
251+
* @throws UnsupportedOperationException if unsupported
252+
* @since 1.47.0
253+
*/
254+
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/9009")
255+
public T keepAliveTime(long keepAliveTime, TimeUnit timeUnit) {
256+
throw new UnsupportedOperationException();
257+
}
258+
259+
/**
260+
* Sets a time waiting for read activity after sending a keepalive ping. If the time expires
261+
* without any read activity on the connection, the connection is considered dead. An unreasonably
262+
* small value might be increased. Defaults to 20 seconds when supported.
263+
*
264+
* <p>This value should be at least multiple times the RTT to allow for lost packets.
265+
*
266+
* @throws UnsupportedOperationException if unsupported
267+
* @since 1.47.0
268+
*/
269+
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/9009")
270+
public T keepAliveTimeout(long keepAliveTimeout, TimeUnit timeUnit) {
271+
throw new UnsupportedOperationException();
272+
}
273+
274+
/**
275+
* Sets the maximum connection idle time, connections being idle for longer than which will be
276+
* gracefully terminated. Idleness duration is defined since the most recent time the number of
277+
* outstanding RPCs became zero or the connection establishment. An unreasonably small value might
278+
* be increased. {@code Long.MAX_VALUE} nano seconds or an unreasonably large value will disable
279+
* max connection idle.
280+
*
281+
* @throws UnsupportedOperationException if unsupported
282+
* @since 1.47.0
283+
*/
284+
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/9009")
285+
public T maxConnectionIdle(long maxConnectionIdle, TimeUnit timeUnit) {
286+
throw new UnsupportedOperationException();
287+
}
288+
289+
/**
290+
* Sets the maximum connection age, connections lasting longer than which will be gracefully
291+
* terminated. An unreasonably small value might be increased. A random jitter of +/-10% will be
292+
* added to it. {@code Long.MAX_VALUE} nano seconds or an unreasonably large value will disable
293+
* max connection age.
294+
*
295+
* @throws UnsupportedOperationException if unsupported
296+
* @since 1.47.0
297+
*/
298+
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/9009")
299+
public T maxConnectionAge(long maxConnectionAge, TimeUnit timeUnit) {
300+
throw new UnsupportedOperationException();
301+
}
302+
303+
/**
304+
* Sets the grace time for the graceful connection termination. Once the max connection age
305+
* is reached, RPCs have the grace time to complete. RPCs that do not complete in time will be
306+
* cancelled, allowing the connection to terminate. {@code Long.MAX_VALUE} nano seconds or an
307+
* unreasonably large value are considered infinite.
308+
*
309+
* @throws UnsupportedOperationException if unsupported
310+
* @see #maxConnectionAge(long, TimeUnit)
311+
* @since 1.47.0
312+
*/
313+
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/9009")
314+
public T maxConnectionAgeGrace(long maxConnectionAgeGrace, TimeUnit timeUnit) {
315+
throw new UnsupportedOperationException();
316+
}
317+
318+
/**
319+
* Specify the most aggressive keep-alive time clients are permitted to configure. The server will
320+
* try to detect clients exceeding this rate and when detected will forcefully close the
321+
* connection. The typical default is 5 minutes when supported.
322+
*
323+
* <p>Even though a default is defined that allows some keep-alives, clients must not use
324+
* keep-alive without approval from the service owner. Otherwise, they may experience failures in
325+
* the future if the service becomes more restrictive. When unthrottled, keep-alives can cause a
326+
* significant amount of traffic and CPU usage, so clients and servers should be conservative in
327+
* what they use and accept.
328+
*
329+
* @throws UnsupportedOperationException if unsupported
330+
* @see #permitKeepAliveWithoutCalls(boolean)
331+
* @since 1.47.0
332+
*/
333+
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/9009")
334+
public T permitKeepAliveTime(long keepAliveTime, TimeUnit timeUnit) {
335+
throw new UnsupportedOperationException();
336+
}
337+
338+
/**
339+
* Sets whether to allow clients to send keep-alive HTTP/2 PINGs even if there are no outstanding
340+
* RPCs on the connection. Defaults to {@code false} when supported.
341+
*
342+
* @throws UnsupportedOperationException if unsupported
343+
* @see #permitKeepAliveTime(long, TimeUnit)
344+
* @since 1.47.0
345+
*/
346+
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/9009")
347+
public T permitKeepAliveWithoutCalls(boolean permit) {
348+
throw new UnsupportedOperationException();
349+
}
350+
246351
/**
247352
* Sets the maximum message size allowed to be received on the server. If not called,
248353
* defaults to 4 MiB. The default provides protection to servers who haven't considered the

core/src/main/java/io/grpc/internal/AbstractServerImplBuilder.java

+42
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,48 @@ public T handshakeTimeout(long timeout, TimeUnit unit) {
148148
return thisT();
149149
}
150150

151+
@Override
152+
public T keepAliveTime(long keepAliveTime, TimeUnit timeUnit) {
153+
delegate().keepAliveTime(keepAliveTime, timeUnit);
154+
return thisT();
155+
}
156+
157+
@Override
158+
public T keepAliveTimeout(long keepAliveTimeout, TimeUnit timeUnit) {
159+
delegate().keepAliveTimeout(keepAliveTimeout, timeUnit);
160+
return thisT();
161+
}
162+
163+
@Override
164+
public T maxConnectionIdle(long maxConnectionIdle, TimeUnit timeUnit) {
165+
delegate().maxConnectionIdle(maxConnectionIdle, timeUnit);
166+
return thisT();
167+
}
168+
169+
@Override
170+
public T maxConnectionAge(long maxConnectionAge, TimeUnit timeUnit) {
171+
delegate().maxConnectionAge(maxConnectionAge, timeUnit);
172+
return thisT();
173+
}
174+
175+
@Override
176+
public T maxConnectionAgeGrace(long maxConnectionAgeGrace, TimeUnit timeUnit) {
177+
delegate().maxConnectionAgeGrace(maxConnectionAgeGrace, timeUnit);
178+
return thisT();
179+
}
180+
181+
@Override
182+
public T permitKeepAliveTime(long keepAliveTime, TimeUnit timeUnit) {
183+
delegate().permitKeepAliveTime(keepAliveTime, timeUnit);
184+
return thisT();
185+
}
186+
187+
@Override
188+
public T permitKeepAliveWithoutCalls(boolean permit) {
189+
delegate().permitKeepAliveWithoutCalls(permit);
190+
return thisT();
191+
}
192+
151193
@Override
152194
public T maxInboundMessageSize(int bytes) {
153195
delegate().maxInboundMessageSize(bytes);

netty/src/main/java/io/grpc/netty/NettyServerBuilder.java

+7
Original file line numberDiff line numberDiff line change
@@ -490,6 +490,7 @@ public NettyServerBuilder maxInboundMetadataSize(int bytes) {
490490
*
491491
* @since 1.3.0
492492
*/
493+
@Override
493494
public NettyServerBuilder keepAliveTime(long keepAliveTime, TimeUnit timeUnit) {
494495
checkArgument(keepAliveTime > 0L, "keepalive time must be positive:%s", keepAliveTime);
495496
keepAliveTimeInNanos = timeUnit.toNanos(keepAliveTime);
@@ -511,6 +512,7 @@ public NettyServerBuilder keepAliveTime(long keepAliveTime, TimeUnit timeUnit) {
511512
*
512513
* @since 1.3.0
513514
*/
515+
@Override
514516
public NettyServerBuilder keepAliveTimeout(long keepAliveTimeout, TimeUnit timeUnit) {
515517
checkArgument(keepAliveTimeout > 0L, "keepalive timeout must be positive: %s",
516518
keepAliveTimeout);
@@ -533,6 +535,7 @@ public NettyServerBuilder keepAliveTimeout(long keepAliveTimeout, TimeUnit timeU
533535
*
534536
* @since 1.4.0
535537
*/
538+
@Override
536539
public NettyServerBuilder maxConnectionIdle(long maxConnectionIdle, TimeUnit timeUnit) {
537540
checkArgument(maxConnectionIdle > 0L, "max connection idle must be positive: %s",
538541
maxConnectionIdle);
@@ -554,6 +557,7 @@ public NettyServerBuilder maxConnectionIdle(long maxConnectionIdle, TimeUnit tim
554557
*
555558
* @since 1.3.0
556559
*/
560+
@Override
557561
public NettyServerBuilder maxConnectionAge(long maxConnectionAge, TimeUnit timeUnit) {
558562
checkArgument(maxConnectionAge > 0L, "max connection age must be positive: %s",
559563
maxConnectionAge);
@@ -576,6 +580,7 @@ public NettyServerBuilder maxConnectionAge(long maxConnectionAge, TimeUnit timeU
576580
* @see #maxConnectionAge(long, TimeUnit)
577581
* @since 1.3.0
578582
*/
583+
@Override
579584
public NettyServerBuilder maxConnectionAgeGrace(long maxConnectionAgeGrace, TimeUnit timeUnit) {
580585
checkArgument(maxConnectionAgeGrace >= 0L, "max connection age grace must be non-negative: %s",
581586
maxConnectionAgeGrace);
@@ -600,6 +605,7 @@ public NettyServerBuilder maxConnectionAgeGrace(long maxConnectionAgeGrace, Time
600605
* @see #permitKeepAliveWithoutCalls(boolean)
601606
* @since 1.3.0
602607
*/
608+
@Override
603609
public NettyServerBuilder permitKeepAliveTime(long keepAliveTime, TimeUnit timeUnit) {
604610
checkArgument(keepAliveTime >= 0, "permit keepalive time must be non-negative: %s",
605611
keepAliveTime);
@@ -614,6 +620,7 @@ public NettyServerBuilder permitKeepAliveTime(long keepAliveTime, TimeUnit timeU
614620
* @see #permitKeepAliveTime(long, TimeUnit)
615621
* @since 1.3.0
616622
*/
623+
@Override
617624
public NettyServerBuilder permitKeepAliveWithoutCalls(boolean permit) {
618625
permitKeepAliveWithoutCalls = permit;
619626
return this;

0 commit comments

Comments
 (0)