protected void gracefully(ChannelHandlerContext ctx, ChannelPromise promise) {
        Channel channel = ctx.channel();
        if (channel.isActive()) {
            String channelId = channel.id().asShortText();

            // In gracefulCloseDelay secs time, go ahead and close the connection if it hasn't already been.
            int gracefulCloseDelay = ConnectionCloseChannelAttributes.gracefulCloseDelay(channel);
            ctx.executor()
                    .schedule(
                            () -> {

                                // Check that the client hasn't already closed the connection.
                                if (channel.isActive()) {

                                    // If there is still an inflight request, then don't close the conn now. Instead
                                    // assume that it will be closed
                                    // either after the response finally gets written (due to us having set the
                                    // CLOSE_AFTER_RESPONSE flag), or when the IdleTimeout
                                    // for this conn fires.
                                    if (requestInflight.get()) {
                                        LOG.debug(
                                                "gracefully: firing graceful_shutdown event to close connection, but"
                                                        + " request still inflight, so leaving. channel={}",
                                                channelId);
                                    } else {
                                        LOG.debug(
                                                "gracefully: firing graceful_shutdown event to close connection."
                                                        + " channel={}",
                                                channelId);
                                        ctx.close(promise);
                                    }
                                } else {
                                    LOG.debug("gracefully: connection already closed. channel={}", channelId);
                                    promise.setSuccess();
                                }
                            },
                            gracefulCloseDelay,
                            TimeUnit.SECONDS);
        } else {
            promise.setSuccess();
        }
    }
User: michaels@netflix.com
Date: 2/8/17
Time: 2:03 PM
zuul-core/src/main/java/com/netflix/netty/common/Http1ConnectionCloseHandler.java