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

2019-07-11 02:28发布

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.

标签: netty
2条回答
爷的心禁止访问
2楼-- · 2019-07-11 03:13

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

查看更多
Evening l夕情丶
3楼-- · 2019-07-11 03:15

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.

查看更多
登录 后发表回答