Dr.Haribo
Dr.Haribo

Reputation: 1778

How to drop congested connections in Netty 4

Using Netty 4, how can I detect that data I write is being clogged up on my side of the connection and close the connection if the remote end can't keep up?

I was checking whether

ChannelHandlerContext.channel.outboundByteBuffer.readableBytes > MaxWriteBuffer

but with the latest Netty 4 version this now gives me an exception:

java.lang.IllegalStateException: nextOutboundByteBuffer() called from outside the eventLoop

Upvotes: 2

Views: 338

Answers (1)

Norman Maurer
Norman Maurer

Reputation: 23567

You need to do this from the EventLoop as otherwise it is not thread-safe.

So something like this:

ChannelHandlerContext ctx = ....
final Channel channel = ctx.channel();
channel.eventLoop().execute(new Runnable() {
    public void run() {
        if (channel.outboundByteBuffer.readableBytes() > MaxWriteBuffer) {
            // do something
        }
    }
});

Upvotes: 2

Related Questions