Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Propagate back-pressure between two netty channels

Tags:

java

netty

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.

like image 862
Michael Deardeuff Avatar asked Nov 02 '13 22:11

Michael Deardeuff


2 Answers

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.

like image 162
johnstlr Avatar answered Nov 15 '22 08:11

johnstlr


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.

like image 43
Igor Alelekov Avatar answered Nov 15 '22 10:11

Igor Alelekov