How to deal with netty's unpacking and sticking in production

concept

TCP processes data in a stream, so it will cause sticky/unpacked packets.

  • Unpacking: A complete packet may be split into multiple packets for transmission by TCP.

  • Sticky packet: It is also possible to encapsulate a small packet into a large packet and send it.

 the reason

  • The byte size written by the application program is larger than the size of the socket send buffer, and unpacking will occur . The data written by the application is smaller than the socket buffer size, and the network card sends the data written by the application multiple times to the network, which will cause packet sticking .

  • If the data to be sent is larger than MSS (maximum message length), TCP will unpack before transmission .

  • The payload of the Ethernet frame is larger than the MTU (the default is 1500 bytes) for IP fragmentation and unpacking .

  • If the application layer of the receiving data side fails to read the data in the receiving buffer in time, sticky packets will occur .

solve

In Netty, multiple Decoder parsing classes are provided, as follows:

  • ① FixedLengthFrameDecoder, based on fixed-length messages for sticking and unpacking processing.

  • ② LengthFieldBasedFrameDecoder, based on the message header specified message length for sticking and unpacking processing.

  • ③ LineBasedFrameDecoder, which performs message sticking and unpacking processing based on line breaks .

  • ④ DelimiterBasedFrameDecoder, based on the specified message boundary method for sticking and unpacking processing.

In fact, the above four FrameDecoder implementations can be regularized:

  • ① is a special case of ②, and the fixed length is a form of specifying the message length in the message header .

  • ③ is a special case of ④, and line break is a form of specifying message boundary .

In production, it is processed through a custom protocol. There is a startTag field at the beginning of the protocol and an endTag field at the end.

Guess you like

Origin blog.csdn.net/madongyu1259892936/article/details/109668901