Netty: Should I close the Channel if it's a 'keep-alive' connection?

woodings picture woodings · Nov 28, 2012 · Viewed 8.4k times · Source

I'm writing an HTTP server with Netty. I set the keep-alive option when I create server bootstrap. bootstrap.setOption("child.keepAlive", true); Each time I write an HTTP response, I set the keep-alive header and close the channel after writing response.

rep.setHeader("Connection", "keep-alive");
channel.write(rep).addListener(ChannelFutureListener.CLOSE);

I'm not sure if I'm supposed to close the Channel.

Answer

forty-two picture forty-two · Nov 28, 2012

Assuming that you are writing an HTTP 1.1 server, you should per default keep the connection open after sending the response. If, for some reason, you decide to close it anyway , you should include

Connection: close

in the response.

Note that

bootstrap.setOption("child.keepAlive", true);

turns on the keepalive option on the socket and has nothing to do with HTTP; rather, it is a kind of watchdog mechanim in order to detect broken connections in the absence of "real" traffic.