diff --git a/netty/src/main/java/io/grpc/netty/NettyServerHandler.java b/netty/src/main/java/io/grpc/netty/NettyServerHandler.java index d7a9aec2e2f..41a630eac4c 100644 --- a/netty/src/main/java/io/grpc/netty/NettyServerHandler.java +++ b/netty/src/main/java/io/grpc/netty/NettyServerHandler.java @@ -19,6 +19,7 @@ import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Preconditions.checkNotNull; import static io.grpc.internal.GrpcUtil.SERVER_KEEPALIVE_TIME_NANOS_DISABLED; +import static io.grpc.netty.NettyServerBuilder.MAX_CONNECTION_AGE_GRACE_NANOS_INFINITE; import static io.grpc.netty.NettyServerBuilder.MAX_CONNECTION_AGE_NANOS_DISABLED; import static io.grpc.netty.NettyServerBuilder.MAX_CONNECTION_IDLE_NANOS_DISABLED; import static io.grpc.netty.Utils.CONTENT_TYPE_HEADER; @@ -910,12 +911,9 @@ void secondGoAwayAndClose(ChannelHandlerContext ctx) { // gracefully shutdown with specified grace time long savedGracefulShutdownTimeMillis = gracefulShutdownTimeoutMillis(); - long gracefulShutdownTimeoutMillis = savedGracefulShutdownTimeMillis; - if (graceTimeInNanos != null) { - gracefulShutdownTimeoutMillis = TimeUnit.NANOSECONDS.toMillis(graceTimeInNanos); - } + long overriddenGraceTime = graceTimeOverrideMillis(savedGracefulShutdownTimeMillis); try { - gracefulShutdownTimeoutMillis(gracefulShutdownTimeoutMillis); + gracefulShutdownTimeoutMillis(overriddenGraceTime); NettyServerHandler.super.close(ctx, ctx.newPromise()); } catch (Exception e) { onError(ctx, /* outbound= */ true, e); @@ -923,6 +921,17 @@ void secondGoAwayAndClose(ChannelHandlerContext ctx) { gracefulShutdownTimeoutMillis(savedGracefulShutdownTimeMillis); } } + + private long graceTimeOverrideMillis(long originalMillis) { + if (graceTimeInNanos == null) { + return originalMillis; + } + if (graceTimeInNanos == MAX_CONNECTION_AGE_GRACE_NANOS_INFINITE) { + // netty treats -1 as "no timeout" + return -1L; + } + return TimeUnit.NANOSECONDS.toMillis(graceTimeInNanos); + } } // Use a frame writer so that we know when frames are through flow control and actually being