Skip to content

Commit

Permalink
api: Add connection management APIs to ServerBuilder
Browse files Browse the repository at this point in the history
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 #8991
  • Loading branch information
ejona86 committed May 16, 2022
1 parent 78067cb commit ba57a1d
Show file tree
Hide file tree
Showing 4 changed files with 196 additions and 0 deletions.
42 changes: 42 additions & 0 deletions api/src/main/java/io/grpc/ForwardingServerBuilder.java
Expand Up @@ -133,6 +133,48 @@ public T handshakeTimeout(long timeout, TimeUnit unit) {
return thisT();
}

@Override
public T keepAliveTime(long keepAliveTime, TimeUnit timeUnit) {
delegate().keepAliveTime(keepAliveTime, timeUnit);
return thisT();
}

@Override
public T keepAliveTimeout(long keepAliveTimeout, TimeUnit timeUnit) {
delegate().keepAliveTimeout(keepAliveTimeout, timeUnit);
return thisT();
}

@Override
public T maxConnectionIdle(long maxConnectionIdle, TimeUnit timeUnit) {
delegate().maxConnectionIdle(maxConnectionIdle, timeUnit);
return thisT();
}

@Override
public T maxConnectionAge(long maxConnectionAge, TimeUnit timeUnit) {
delegate().maxConnectionAge(maxConnectionAge, timeUnit);
return thisT();
}

@Override
public T maxConnectionAgeGrace(long maxConnectionAgeGrace, TimeUnit timeUnit) {
delegate().maxConnectionAgeGrace(maxConnectionAgeGrace, timeUnit);
return thisT();
}

@Override
public T permitKeepAliveTime(long keepAliveTime, TimeUnit timeUnit) {
delegate().permitKeepAliveTime(keepAliveTime, timeUnit);
return thisT();
}

@Override
public T permitKeepAliveWithoutCalls(boolean permit) {
delegate().permitKeepAliveWithoutCalls(permit);
return thisT();
}

@Override
public T maxInboundMessageSize(int bytes) {
delegate().maxInboundMessageSize(bytes);
Expand Down
105 changes: 105 additions & 0 deletions api/src/main/java/io/grpc/ServerBuilder.java
Expand Up @@ -243,6 +243,111 @@ public T handshakeTimeout(long timeout, TimeUnit unit) {
throw new UnsupportedOperationException();
}

/**
* Sets the time without read activity before sending a keepalive ping. An unreasonably small
* value might be increased, and {@code Long.MAX_VALUE} nano seconds or an unreasonably large
* value will disable keepalive. The typical default is infinite when supported.
*
* @throws UnsupportedOperationException if unsupported
* @since 1.47.0
*/
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/9009")
public T keepAliveTime(long keepAliveTime, TimeUnit timeUnit) {
throw new UnsupportedOperationException();
}

/**
* Sets a time waiting for read activity after sending a keepalive ping. If the time expires
* without any read activity on the connection, the connection is considered dead. An unreasonably
* small value might be increased. Defaults to 20 seconds when supported.
*
* <p>This value should be at least multiple times the RTT to allow for lost packets.
*
* @throws UnsupportedOperationException if unsupported
* @since 1.47.0
*/
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/9009")
public T keepAliveTimeout(long keepAliveTimeout, TimeUnit timeUnit) {
throw new UnsupportedOperationException();
}

/**
* Sets the maximum connection idle time, connections being idle for longer than which will be
* gracefully terminated. Idleness duration is defined since the most recent time the number of
* outstanding RPCs became zero or the connection establishment. An unreasonably small value might
* be increased. {@code Long.MAX_VALUE} nano seconds or an unreasonably large value will disable
* max connection idle.
*
* @throws UnsupportedOperationException if unsupported
* @since 1.47.0
*/
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/9009")
public T maxConnectionIdle(long maxConnectionIdle, TimeUnit timeUnit) {
throw new UnsupportedOperationException();
}

/**
* Sets the maximum connection age, connections lasting longer than which will be gracefully
* terminated. An unreasonably small value might be increased. A random jitter of +/-10% will be
* added to it. {@code Long.MAX_VALUE} nano seconds or an unreasonably large value will disable
* max connection age.
*
* @throws UnsupportedOperationException if unsupported
* @since 1.47.0
*/
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/9009")
public T maxConnectionAge(long maxConnectionAge, TimeUnit timeUnit) {
throw new UnsupportedOperationException();
}

/**
* Sets the grace time for the graceful connection termination. Once the max connection age
* is reached, RPCs have the grace time to complete. RPCs that do not complete in time will be
* cancelled, allowing the connection to terminate. {@code Long.MAX_VALUE} nano seconds or an
* unreasonably large value are considered infinite.
*
* @throws UnsupportedOperationException if unsupported
* @see #maxConnectionAge(long, TimeUnit)
* @since 1.47.0
*/
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/9009")
public T maxConnectionAgeGrace(long maxConnectionAgeGrace, TimeUnit timeUnit) {
throw new UnsupportedOperationException();
}

/**
* Specify the most aggressive keep-alive time clients are permitted to configure. The server will
* try to detect clients exceeding this rate and when detected will forcefully close the
* connection. The typical default is 5 minutes when supported.
*
* <p>Even though a default is defined that allows some keep-alives, clients must not use
* keep-alive without approval from the service owner. Otherwise, they may experience failures in
* the future if the service becomes more restrictive. When unthrottled, keep-alives can cause a
* significant amount of traffic and CPU usage, so clients and servers should be conservative in
* what they use and accept.
*
* @throws UnsupportedOperationException if unsupported
* @see #permitKeepAliveWithoutCalls(boolean)
* @since 1.47.0
*/
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/9009")
public T permitKeepAliveTime(long keepAliveTime, TimeUnit timeUnit) {
throw new UnsupportedOperationException();
}

/**
* Sets whether to allow clients to send keep-alive HTTP/2 PINGs even if there are no outstanding
* RPCs on the connection. Defaults to {@code false} when supported.
*
* @throws UnsupportedOperationException if unsupported
* @see #permitKeepAliveTime(long, TimeUnit)
* @since 1.47.0
*/
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/9009")
public T permitKeepAliveWithoutCalls(boolean permit) {
throw new UnsupportedOperationException();
}

/**
* Sets the maximum message size allowed to be received on the server. If not called,
* defaults to 4 MiB. The default provides protection to servers who haven't considered the
Expand Down
42 changes: 42 additions & 0 deletions core/src/main/java/io/grpc/internal/AbstractServerImplBuilder.java
Expand Up @@ -148,6 +148,48 @@ public T handshakeTimeout(long timeout, TimeUnit unit) {
return thisT();
}

@Override
public T keepAliveTime(long keepAliveTime, TimeUnit timeUnit) {
delegate().keepAliveTime(keepAliveTime, timeUnit);
return thisT();
}

@Override
public T keepAliveTimeout(long keepAliveTimeout, TimeUnit timeUnit) {
delegate().keepAliveTimeout(keepAliveTimeout, timeUnit);
return thisT();
}

@Override
public T maxConnectionIdle(long maxConnectionIdle, TimeUnit timeUnit) {
delegate().maxConnectionIdle(maxConnectionIdle, timeUnit);
return thisT();
}

@Override
public T maxConnectionAge(long maxConnectionAge, TimeUnit timeUnit) {
delegate().maxConnectionAge(maxConnectionAge, timeUnit);
return thisT();
}

@Override
public T maxConnectionAgeGrace(long maxConnectionAgeGrace, TimeUnit timeUnit) {
delegate().maxConnectionAgeGrace(maxConnectionAgeGrace, timeUnit);
return thisT();
}

@Override
public T permitKeepAliveTime(long keepAliveTime, TimeUnit timeUnit) {
delegate().permitKeepAliveTime(keepAliveTime, timeUnit);
return thisT();
}

@Override
public T permitKeepAliveWithoutCalls(boolean permit) {
delegate().permitKeepAliveWithoutCalls(permit);
return thisT();
}

@Override
public T maxInboundMessageSize(int bytes) {
delegate().maxInboundMessageSize(bytes);
Expand Down
7 changes: 7 additions & 0 deletions netty/src/main/java/io/grpc/netty/NettyServerBuilder.java
Expand Up @@ -490,6 +490,7 @@ public NettyServerBuilder maxInboundMetadataSize(int bytes) {
*
* @since 1.3.0
*/
@Override
public NettyServerBuilder keepAliveTime(long keepAliveTime, TimeUnit timeUnit) {
checkArgument(keepAliveTime > 0L, "keepalive time must be positive:%s", keepAliveTime);
keepAliveTimeInNanos = timeUnit.toNanos(keepAliveTime);
Expand All @@ -511,6 +512,7 @@ public NettyServerBuilder keepAliveTime(long keepAliveTime, TimeUnit timeUnit) {
*
* @since 1.3.0
*/
@Override
public NettyServerBuilder keepAliveTimeout(long keepAliveTimeout, TimeUnit timeUnit) {
checkArgument(keepAliveTimeout > 0L, "keepalive timeout must be positive: %s",
keepAliveTimeout);
Expand All @@ -533,6 +535,7 @@ public NettyServerBuilder keepAliveTimeout(long keepAliveTimeout, TimeUnit timeU
*
* @since 1.4.0
*/
@Override
public NettyServerBuilder maxConnectionIdle(long maxConnectionIdle, TimeUnit timeUnit) {
checkArgument(maxConnectionIdle > 0L, "max connection idle must be positive: %s",
maxConnectionIdle);
Expand All @@ -554,6 +557,7 @@ public NettyServerBuilder maxConnectionIdle(long maxConnectionIdle, TimeUnit tim
*
* @since 1.3.0
*/
@Override
public NettyServerBuilder maxConnectionAge(long maxConnectionAge, TimeUnit timeUnit) {
checkArgument(maxConnectionAge > 0L, "max connection age must be positive: %s",
maxConnectionAge);
Expand All @@ -576,6 +580,7 @@ public NettyServerBuilder maxConnectionAge(long maxConnectionAge, TimeUnit timeU
* @see #maxConnectionAge(long, TimeUnit)
* @since 1.3.0
*/
@Override
public NettyServerBuilder maxConnectionAgeGrace(long maxConnectionAgeGrace, TimeUnit timeUnit) {
checkArgument(maxConnectionAgeGrace >= 0L, "max connection age grace must be non-negative: %s",
maxConnectionAgeGrace);
Expand All @@ -600,6 +605,7 @@ public NettyServerBuilder maxConnectionAgeGrace(long maxConnectionAgeGrace, Time
* @see #permitKeepAliveWithoutCalls(boolean)
* @since 1.3.0
*/
@Override
public NettyServerBuilder permitKeepAliveTime(long keepAliveTime, TimeUnit timeUnit) {
checkArgument(keepAliveTime >= 0, "permit keepalive time must be non-negative: %s",
keepAliveTime);
Expand All @@ -614,6 +620,7 @@ public NettyServerBuilder permitKeepAliveTime(long keepAliveTime, TimeUnit timeU
* @see #permitKeepAliveTime(long, TimeUnit)
* @since 1.3.0
*/
@Override
public NettyServerBuilder permitKeepAliveWithoutCalls(boolean permit) {
permitKeepAliveWithoutCalls = permit;
return this;
Expand Down

0 comments on commit ba57a1d

Please sign in to comment.