Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the causes of sticky package and semi-package in Netty

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the causes of sticky package and semi-package in Netty, which has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.

Define

In TCP transmission, when the client sends data, it actually writes the data to the TCP cache, and sticky packets and semi-packets will be generated at this time.

The client sends two messages, ABC and DEF, to the server. How many kinds of reception will there be on the server side? It is possible to receive all the messages ABCDEF at once, or to receive three messages: AB, CD, and EF.

The above mentioned one-time receipt of all the messages ABCDEF, similar to sticky packets. If the size of the packet sent by the client is smaller than the cache capacity of TCP, and the TCP cache can store multiple packets, then multiple packets may be delivered in a communication between the client and the server. At this time, the server may read multiple packets from the TCP cache, which is called sticky packet.

The latter one mentioned above received three messages AB, CD, and EF, which is similar to a half-packet. If the size of the packet sent by the client is larger than the cache capacity of TCP, the packet will be divided into multiple packets and sent to the server multiple times through Socket. The data obtained by the server from the accepted cache for the first time is actually part of the whole packet. At this time, a half packet is generated (not only half of the whole packet, but part of the whole packet is received).

Cause

In fact, from the above definition, we can roughly know the cause.

The main reasons for sticking bags are:

Each time the sender writes data

< 套接字(Socket)缓冲区大小 接收方读取套接字(Socket)缓冲区数据不够及时 半包的主要原因: 发送方每次写入数据 >

Socket (Socket) buffer size

The data sent is larger than the MTU (Maximum Transmission Unit, maximum transmission unit) of the protocol, so the packets must be unpacked.

In fact, we can look at the problem from a different perspective:

From a transceiver point of view, one transmission may be received multiple times, and multiple transmissions may be received at one time.

From a transmission point of view, one transmission may occupy multiple transmission packets, and multiple transmissions may share one transmission packet.

The root cause, in fact,

TCP is a streaming protocol and messages have no boundaries.

(PS: although UDP can transfer multiple packets or one packet at a time, each message has a boundary, so there is no sticky packet or semi-packet problem.)

Solution method

As mentioned above, the main reason why UDP does not have sticky packet and half-package problems is that messages have boundaries, so we can take a similar approach.

Change to a short connection

Change the TCP connection to a short connection, one request for a short connection. In this way, the message that establishes the connection to the release connection is the transmitted information, and the message creates a boundary.

This method is very simple and does not need to make too many changes in our application. But the disadvantage is obvious, inefficient, TCP connection and disconnection will involve three-way handshake and four-way handshake, each message will involve these processes, which is a waste of performance.

Therefore, this approach is not recommended.

Encapsulated into a frame

Encapsulated into frames (Framing), that is, the original unit of sending messages is the buffer size, but now it has been replaced by frames so that we can customize the boundaries. There are generally four ways:

Fixed length

In this way, the message boundary is a fixed length.

The advantage is that the implementation is very simple, the disadvantage is that there is a great waste of space, if most of the messages delivered are relatively short, so there will be a lot of space to be wasted.

Therefore, this approach is generally not recommended.

Separator

In this way, the message boundary is the delimiter itself.

The advantage is that the space is no longer wasted and the implementation is relatively simple. The disadvantage is that the content itself needs to be escaped when there is a separator, so whether it is sent or accepted, the entire content needs to be scanned.

Therefore, this approach is not very efficient, but you can try to use it.

Special length field

In this way, it is a bit like Content-Length in a Http request, with a special field to store the length of the message. As a server, when accepting a message, first parse the fixed-length field (length field) to get the total length of the message, and then read the subsequent content.

The advantage is that the user data is accurately located and the content does not need to be escaped. The disadvantage is that the length is theoretically limited, and it is necessary to limit the maximum possible length in advance to define the number of bytes occupied by the length.

Therefore, it is highly recommended to use this way.

Other ways

Other ways are different, such as whether JSON can be seen as using {} in pairs. These advantages and disadvantages need to be measured in their respective scenarios.

Implementation in Netty

Netty supports the first three methods of encapsulation into frames (Framing) mentioned above, which are briefly introduced:

Way to decode and encode fixed-length FixedLengthFrameDecoder simple separator DelimiterBasedFrameDecoder simple special length field LengthFieldBasedFrameDecoderLengthFieldPrepender Thank you for reading this article carefully. I hope the article "what are the causes of sticky package and half-package in Netty" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report