Currently in my pipeline I have a simple handler to reject connections when my server gets overloaded:
public class RequestFilter extends SimpleChannelHandler {
@Override
public void channelConnected(final ChannelHandlerContext ctx, final ChannelStateEvent e) throws Exception {
requestLimiter(ctx, e);
super.channelConnected(ctx, e);
}
}
private void requestLimiter(final ChannelHandlerContext ctx, final ChannelStateEvent e) {
if(threshold < counter) {
ctx.getChannel().close();
}
}
When the counter exceeds the threshold, the channel is closed, that all seems to work fine.
Now I’d like to enhance this by first sending an HTTP 503 response prior to closing the channel. What i’ve tried so far is this method below, instead of closing the channel immediatly I try to write a response to the channel and then handle closing it with a channelfuture listener so it’s closed when the write is complete. However whats happening is I get a ton of exceptions about “already sent a response, can’t send more than 1” followed by stack overflow.
protected void sendResponse(Channel channel, HttpResponse response) {
if (channel.isConnected()) {
channel.write(response).addListener(ChannelFutureListener.CLOSE);
log.trace("response sent");
} else if (!channel.isConnected()) {
log.trace("attempted to send response, but the channel was closed");
} else {
log.trace("Not sure why this would happen");
}
}
Any thoughts or examples I could look at? thanks
Edit: stacktrace
java.lang.IllegalStateException: cannot send more responses than requests
at org.jboss.netty.handler.codec.http.HttpContentEncoder.writeRequested(HttpContentEncoder.java:104)
at org.jboss.netty.handler.timeout.WriteTimeoutHandler.writeRequested(WriteTimeoutHandler.java:152)
at org.jboss.netty.handler.stream.ChunkedWriteHandler.flush(ChunkedWriteHandler.java:262)
at org.jboss.netty.handler.stream.ChunkedWriteHandler.handleDownstream(ChunkedWriteHandler.java:119)
at org.jboss.netty.handler.execution.ExecutionHandler.handleDownstream(ExecutionHandler.java:165)
at org.jboss.netty.channel.Channels.write(Channels.java:605)
at org.jboss.netty.channel.Channels.write(Channels.java:572)
at org.jboss.netty.channel.AbstractChannel.write(AbstractChannel.java:245)
at com.test.RequestFilter.sendResponse(RequestFilter.java:98)
I don’t think it is sending multiple responses. I think that it’s [trying] to send one, which is one more than the number of requests which is zero because the the event is being triggered by the connect channel event and the pipeline has yet to see any http requests.
I would change your code to not do this on connect, but rather, trigger the 503 response on the first request. If the channel is then closed, adios client, but if the client’s first request sneaks in under the threshold, then remove the bouncer from the pipeline (assuming that once a client is in, they’re in for good).
Make sense ?