1
votes

I have a ServerBootstrap configured with a fairly standard Http-Codec ChannelInitializer.

On shutdown my server waits for a grace period where it can still handle incoming requests. My server supports keep-alive, but on shutdown I want to make sure every HttpResponse sent closes the connection with HTTP header "Connection: close" and that the channel is closed after the write. This is only necessary on server shutdown.

I have a ChannelHandler to support that:

@ChannelHandler.Sharable
public class CloseConnectionHandler extends ChannelOutboundHandlerAdapter {

@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws   Exception {
    HttpResponse response = (HttpResponse) msg;

    if (isKeepAlive(response)) {
        setKeepAlive(response, false);
        promise.addListener(ChannelFutureListener.CLOSE);
    }
    ctx.write(msg, promise);
}

I keep a track of all connected clients using a ChannelGroup, so I can dynamically modify the pipeline of each client at the point of shutdown to include my CloseConnectionHandler, this works no problem.

However, new connections in the grace period have their pipeline configuration provided by the original ServerBootstrap ChannelInitializer, and I can't see a way of dynamically re-configuring that?

As a work-around I can have the CloseConnectionHandler configured in the standard pipeline and turned off with a boolean, only activating it on shutdown. But I'd rather avoid that if possible, seems a bit unnecessary.

1

1 Answers

2
votes

there is currently no way to "replace" the initializer at run-time. So using a flag etc would be the best bet.