编程知识 cdmana.com

How does netty write data?

1 The core issue of writing data

Express scene ( The parcel ) Netty Writing data ( data )
It's in the warehouse write: Write to a buffer
From the warehouse flush: hold buffer Send out the data in
Receive the goods from the warehouse and deliver them immediately ( urgent item ) writeAndFlush: writes buffer, Send it immediately
There's a buffer warehouse between collection and delivery Write and Flush There's a ChannelOutboundBuffer

1.1 It's fried

  • When the other party's warehouse burst , When you can't give it away , Will stop sending , Negotiate and wait for a call to let you know when it's ready , Send again .
    Netty Writing data , When I can't write it in , Will stop writing , Then register a OP_WRITE event , To inform you when you can write it in .

1.2 I can write it

  • When sending express delivery , The other party's warehouse will accept it directly , This time, when we send the express , You can try sending more express delivery. Try , It works better .
    Netty When you write data in bulk , If everything you try to write goes in , I'll try to write more ( adjustment maxBytesPerGatheringWrite)

1.3 I can also write

  • When sending express delivery , There are a lot of express delivery to some place , We will continue to send , But express cars are limited after all , And think about other places

  • Netty As long as there is data to write , And can write out , I've been trying to , Until it can't be written or full 16 Time (writeSpinCount)

Write 16 I haven't finished yet , Just directly schedule One task To continue to write , Instead of using the register write event to trigger , More concise and powerful .

1.4 I can't write it

  • Take too much , It didn't come in time , Burst warehouse , There will be a notice board at this time : I can't take it anymore , It's better to live 2 I'll send it back in the next day .
  • Netty Too much data to write , Beyond a certain water level (writeBufferWaterMark.high()) , Will change the writable flag bit to false, Let the app decide whether to send data or not ( Write ) 了 ( It's real , Put the blame on the user ).

2 The core processes

  • Write - Write data to the buffer :
    ChannelOutboundBuffer#addMessage

  • Flush - send out buffer The data in it :
    AbstractChannel.AbstractUnsafe#flush

    • Prepare the data : ChannelOutboundBuffer#addFlush
  • Finished updating status

    • send out : NioSocketChannel#doWrite

3 The essence of writing data

  • Single write
    sun.nio.ch.SocketChannelmpl#write(java.nio.ByteBuffer)
  • gathering write( Batch write )
    sun.nio.ch.SocketChannelmpl#write(java.nio.ByteBuffer[], int, int)

When you can't write data in , Will stop writing , Sign up for a OP_WRITE event , To inform you when you can put it in .
OP_WRITE It doesn't mean there's data to write , It can be written in , So don't register it normally , Otherwise, it will trigger all the time .

  • channelHandlerContext.channel().write()
    from TailContext Start execution
  • channelHandlerContext.write()
    From the present Context Start

版权声明
本文为[The official account -JavaEdge]所创,转载请带上原文链接,感谢
https://cdmana.com/2020/12/20201224153141237r.html

Scroll to Top