I want to write a system using netty-4 where a server receives a request from one channel (cleverly dubbed in), does a transform and potentially puts the result onto another channel to another backend-system (dubbed out).
But I would like to propagate back-pressure from the out channel to the in channel? Specifically—because the netty APIs are asynchronous—how do I get notified when out is no longer blocked up; and tell in to pause/resume?
Partial Solutions
I believe can tell when out channel is ready to write with channel.isWritable()
.
An answer to this question mentions disabling AUTO_READ, but then I believe I'd have to be in charge of polling the channel for reads. I'm not to happy about that—unless I can receive a message when it's ready to read. At this point, the utility of using netty is mostly just the no-copy buffers and the ByteToMessageDecoder superclass.
To add to Igor's answer - you have a couple of options for reactivating reads. The first one, shown here means capturing the channelWritabilityChanged event on the destination channel. You can check the status of the channel and enable auto read on the source channel.
Alternatively the proxy example shows setting a write completion listener to write and flush. This technique also works but does not take advantage of Netty's high/low water mark queuing system as you'll only be told the write is complete once the write has been flushed to the network buffers.
Right, you can disable AUTO_READ on the IN channel when the OUT is not writable and enable AUTO_READ again when the OUT will be ready. No polling is required here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With