In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article will explain in detail the number of clients that Netty reads each time. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.
When many clients connect to the server at the same time, how many connections does Netty read by default? Then we will combine the source code and actual combat analysis.
We mentioned earlier that NioEventLoop will loop indefinitely after the trilogy (create-start-execute). The execution process is divided into three steps (polling IO events-handling IO events-executing queue tasks). So when polling for IO events, that is to say, when there are events for client connections, the next step is to deal with IO events, that is, in the process of processing IO events, read client connections, so how many at a time? To note, the version of Netty we use is as follows. Because different versions (especially the latest version) will be different. Io.netty netty-all 4.1.5.Final
When the server has been looping through the following code
Io.netty.channel.nio.NioEventLoop#run it listens for client requests. If we connect to 19 clients of the server through telnet 127.0.0.1 8080 at this time.
When the server listens for ACCEPT events, it handles them.
Io.netty.channel.nio.NioEventLoop#processSelectedKeys then calls the following main code
Io.netty.channel.nio.AbstractNioMessageChannel.NioMessageUnsafe#read there will be a loop do {int localRead = doReadMessages (readBuf); if (localRead = = 0) {break;} if (localRead) in this method
< 0) { closed = true; break; } allocHandle.incMessagesRead(localRead);} while (allocHandle.continueReading());我们通过debug方式调试. 为了模拟实现并发同时有19个客户端到达, 我们先在轮询IO事件地方打个断点, 让服务端线程'停止'. 当我们通过telnet执行了19个命令后再'放行'服务端线程.After the breakpoint was hit, we connected to the server through telnet for a total of 19 times. After connecting, we release the server thread. Let's make a breakpoint in the code mentioned above.
The client connection has not been read at this time, currently size = 0
When we continue to 'let go'
You can see that size = 16
Let's continue with the 'release' first, and finally analyze
After processing the last wave, the server polls the IO event again, and then polls the ACCEPT event and continues to process the IO event. Size = 3 this time
The first size = 16 and the second size = 3 add up to exactly the number of our client connections. So we guessed that the number of client connections read by the server was 16. 5 per read. Although the client has 19 clients connected to the server at the same time, the server also listens for client connection requests, so the server only reads 16 clients in the first cycle. The remaining 3 clients wait until the second poll to read. Let's go to allocHandle.continueReading () to inquire.
Io.netty.channel.DefaultMaxMessagesRecvByteBufAllocator.MaxMessageHandle#continueReadingpublic boolean continueReading () {return config.isAutoRead () & & attemptedBytesRead = = lastBytesRead & & totalMessages < maxMessagePerRead & & totalBytesRead < Integer.MAX_VALUE;} We can see that there is a maxMessagePerRead attribute that controls the number of clients per read. We track this value through debug. Let's make a breakpoint in the following code: .io.netty.channel.DefaultMaxMessagesRecvByteBufAllocator # maxMessagesPerRead (int)
Restart the server
We found that it was finally assigned a value of 16, and we can trace its source through the stack.
Finally, we found that when creating a NioServerSocketChannel, we also created a config (one Config for each Channel). Each Config corresponds to a RecvByteBufAllocator. The maxMessagesPerRead property belongs to the RecvByteBufAllocator class. And where is that specific 16?
Every Channel has a meta.
As you can see in the source code, this 16 is assigned to the maxMessagesPerRead attribute of RecvByteBufAllocator. Conclusion: the server reads 16 client requests each time the article about "how many clients are read by Netty" is shared here. I hope the above content can be helpful to you so that you can learn more knowledge. if you think the article is good, please share it for more people to see.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.