Do messages sent via Netty's Channel.write() p

2019-07-11 02:55发布

问题:

I am doing some sort of encryption which depends of the order of messages sent via a Netty channel. What I am expecting is that if I write message A before message B to a channel then they should be sent to the remote socket in the same order I have written them to the channel. So is the case I need already supported by Netty?

On the other hand Netty's Channel accepts concurrent write calls and I am in doubt my requirement can already met. I had to synchronize on a statefull (a per channel one) encoder today due to currupt state of a data structure. However I am still not sure whether the in-filter synchronization will help my need.

回答1:

Yes, the messages will get transfered in the same order as you call Channel.write(..). There is nothing you need to worry about here.



回答2:

Answer picked up from this issue. (Thanks Scottmitch@github).

Netty can provide "ordering" in the following situations:

  1. You are doing all writes from the EventLoop thread; OR
  2. You are doing no writes from the EventLoop thread (i.e. all writes are being done in other thread(s)).

It is subtle but important thing to remember as it can cause painful bugs. If some channel.write() calls are coming from application threads and some are coming from EventLoop then the writes can seem out of order from application's perspective. See more details on the linked issue.

This question seems to be the top search result for Netty write ordering, so wanted to make sure it mentions this important caveat.



标签: netty