您的位置:首页 > 其它

netty源码学习-课堂5

2012-11-22 21:53 363 查看
要学的东西,还很多,例如:

1)详细写过程write

2)bytebuffer设计

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

4)各种协议,大架子

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

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


"low"watermarkindicatedthesafelevel,thatwhentheamountofbytesqueuedtobesentwentunderthisvaluewriting
couldresume(isWriteable()=true).



"high"watermarkwouldindicatethedangerlevel,thanwhentheamountofbytesqueuedtobesentwentoverthisvaluethenwritingshouldstop(isWriteable()=false).Thedocumentationsaystheinverseofthis.


>1.Ifwehadpreviouslyexceededthehighwatermark(counter>0)and
>wearestillabovethelowwatermark,writeremainsdisabled.
>2.Ifwehadpreviouslyexceededthehighwatermark(counter>0)and
>wearenowbelowthelowwatermark,writeisenabled.
>3.Ifwewerebeneaththehighwatermark(counter<=0)andweare
>stillbelowthehighwatermark,writeremainsenabled.
>4.Ifwewerebeneaththehighwatermark(counter<=0)andwearenow
>abovethehighwatermark,writeisdisabled.


也就是说,
【安全】low水位以下,isWriteable=true可以往外写
【不安全】high水位以上,isWritable=false只能排队

[b][b]1.【不安全】如果危险系数比较低(counter<=0),[b]且目前写任务的个数>高水位,那么只能排队;容易理解[/b][/b][/b]
2.【安全】如果危险系数比较高(counter>0),[b]且目前写任务的个数<低水位,那么可以往外[/b];容易理解

3.【不安全】如果危险系数比较高(counter>0),且目前写任务的个数>低水位,那么只能[b]排队; 容易理解[/b]
4.【安全】如果危险系数比较低(counter<=0),[b]且目前写任务的个数<高水位,那么可以往外写;容易理解
[/b]

那详细的写过程,又是什么样子的呢?nioworker怎么就把数据写到socket里了?遭遇超超大数据会怎样?我们一起来看。
.........................................TOBECONTINUED超超大数据基于流来写?

附录:
channel的interestops,究竟应该如何理解呢?


InterestOps

A
Channel
hasapropertycalled
interestOps
whichissimilartothatof
NIOSelectionKey
.Itisrepresentedasabitfieldwhichiscomposedofthetwoflags.

OP_READ
-Ifset,amessagesentbyaremotepeerwillbereadimmediately.Ifunset,themessagefromtheremotepeerwillnotbereaduntilthe
OP_READ
flagissetagain(i.e.readsuspension).

OP_WRITE
-Ifset,awriterequestwillnotbesenttoaremotepeeruntilthe
OP_WRITE
flagisclearedandthewriterequestwillbependinginaqueue.Ifunset,thewriterequestwillbeflushedoutassoonaspossiblefromthequeue.

OP_READ_WRITE
-Thisisacombinationof
OP_READ
and
OP_WRITE
,whichmeansonlywriterequestsaresuspended.

OP_NONE
-Thisisacombinationof(NOT
OP_READ
)and(NOT
OP_WRITE
),whichmeansonlyreadoperationissuspended.

Youcansetorclearthe
OP_READ
flagtosuspendandresumereadoperationvia
setReadable(boolean)
.

Pleasenotethatyoucannotsuspendorresumewriteoperationjustlikeyoucansetorclear
OP_READ
.The
OP_WRITE
flagisreadonlyandprovidedsimplyasameantotellyouifthesizeofpendingwriterequestsexceededacertainthresholdornotsothatyoudon'tissuetoomanypendingwritesthatleadtoan
OutOfMemoryError
.Forexample,theNIOsockettransportusesthe
writeBufferLowWaterMark
and
writeBufferHighWaterMark
propertiesin
NioSocketChannelConfig
todeterminewhentosetorclearthe
OP_WRITE
flag.

http://wenku.baidu.com/view/5d3d6dfd910ef12d2af9e7c6.html
http://wenku.baidu.com/view/7765bc2db4daa58da0114a4c.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐
章节导航