netty源码学习课堂5

要学的东西,还很多,例如:

1)详细写过程write

2)bytebuffer设计

3)upstream/downstream各种设计与handler维护

4)各种协议,大架子

喘口气,我们不可能一口气吃下个胖子哈,那么好,我们书接上回,谈谈这详细的写过程。

在写的时候,channel的getInterestOps(),会根据目前的“写情况”来自动更新OP_WRITE相关的interestops,换句话说,OP_WRITE完全由netty自己get/set,业务层无法显式set的。前面刚提到,根据目前的“写情况”,那究竟如何标识写情况呢,我们不得不拎出这两个变量来打量一番。关于highwatermark和lowwatermark这两个变量:

"low" water mark indicated the safe level, that when the amount of bytes queued to be sent went under this value writing
 could resume (isWriteable() = true). 
 
"high" water mark would indicate the danger level, than when the amount of bytes queued to be sent went over this value then writing should stop (isWriteable() = false). The documentation says the inverse of this.
> 1. If we had previously exceeded the high water mark (counter > 0) and
> we are still above the low water mark, write remains disabled.
> 2. If we had previously exceeded the high water mark (counter > 0) and
> we are now below the low water mark, write is enabled.
> 3. If we were beneath the high water mark (counter <= 0) and we are
> still below the high water mark, write remains enabled.
> 4. If we were beneath the high water mark (counter <= 0) and we are now
> above the high water mark, write is disabled.
也就是说,
【安全】low水位以下,isWriteable=true 可以往外写
【不安全】high水位以上,isWritable=false 只能排队
 
1. 【不安全】如果危险系数比较低(counter<=0)且目前写任务的个数>高水位,那么只能排队; 容易理解
2. 【安全】如果危险系数比较高(counter>0)且目前写任务的个数<低水位,那么可以往外;    容易理解
3. 【不安全】如果危险系数比较高(counter>0),且目前写任务的个数>低水位,那么只能排队;  容易理解
4. 【安全】如果危险系数比较低(counter<=0)且目前写任务的个数<高水位,那么可以往外写;  容易理解
 
那详细的写过程,又是什么样子的呢?nioworker怎么就把数据写到socket里了?遭遇超超大数据会怎样?我们一起来看。
.........................................TO BE CONTINUED 超超大数据基于流来写?
 
 
附录:
channel的interestops,究竟应该如何理解呢?

InterestOps

Channel has a property called interestOps which is similar to that of NIO SelectionKey. It is represented as a bit field which is composed of the two flags.

  • OP_READ - If set, a message sent by a remote peer will be read immediately. If unset, the message from the remote peer will not be read until the OP_READ flag is set again (i.e. read suspension).
  • OP_WRITE - If set, a write request will not be sent to a remote peer until the OP_WRITE flag is cleared and the write request will be pending in a queue. If unset, the write request will be flushed out as soon as possible from the queue.
  • OP_READ_WRITE - This is a combination of OP_READ and OP_WRITE, which means only write requests are suspended.
  • OP_NONE - This is a combination of (NOT OP_READ) and (NOT OP_WRITE), which means only read operation is suspended.

You can set or clear the OP_READ flag to suspend and resume read operation via setReadable(boolean).

Please note that you cannot suspend or resume write operation just like you can set or clear OP_READ. The OP_WRITE flag is read only and provided simply as a mean to tell you if the size of pending write requests exceeded a certain threshold or not so that you don't issue too many pending writes that lead to an OutOfMemoryError. For example, the NIO socket transport uses the writeBufferLowWaterMark and writeBufferHighWaterMark properties in NioSocketChannelConfig to determine when to set or clear the OP_WRITE flag.
 
原文地址:https://www.cnblogs.com/alipayhutu/p/2783377.html