Search code examples
javanettyniomina

is there a way to cancel a key such that I can still read from it later with java nio


We would like get tcp flow control as a primary component of a new java nio library. It would work like this...

  1. library fires data to Listener.incomingData(DataChunk dataChunk);
  2. library will not fire any more data EVEN if there is any until dataChunk.processed() is called.
  3. Typically, you may call processed() method on the first few dataChunks but on the last one of some message, you write to some remote socket and give it a callback handler.
  4. once the write callback is called, you then invoke the last dataChunk.processed() to relieve the tcp flow control again

BIG NOTE: Step 2 is where tcp flow control automatically kicks in IF you do not read from the nic buffer. This is all automatic (and we tested it with java nio). The issue though is how do we put the key in a state that the poller STOPS releasing and waits for data on all OTHER sockets except this one. I don't mind if it releases when it has new data as we would see that the last dataChunk has not been processed and ignore it, but we don't want the poller thread cycling 100% cpu. Is there a way to achieve this so we can acheive automatic throttling for any server using this potential open source nio framework.


Solution

  • Just cancel it and re-register the channel later, or just de-register it for OP_READ, by changing its interestOps(), and change them back when you are ready to read from it again.