Non-blocking Ioserver models

Source: Internet
Author: User

Let's consider a scene where you and millions of gamers are loyal fans of World of Warcraft. Time trip to play every weekend boss.

Whenever weekend game server in Alexandria, because at least hundreds of thousands of users online at the same time.

Suppose our multi--threaded Jam server works as a game server? This game first analyzes what the server features:

① Online games are not like web pages. Open to close the connection once the download is complete.

Online games must have a persistent, stateful connection, and each client needs to have a persistent connection with the server. So that messages can be sent at high speed. As the number of concurrent users is added, multithreaded blocking server cannot allocate one thread per client.

② Unlike the general application server, CS-structured network games generally put complex logic processing to the client. On the game server side, it only handles simpler logic, or even just passing messages. Simple logic like this, we actually assign a thread to each request. Is this a serious departure from reality?

③ online game is about fast response, timely exchange of information, and can be two-way communication. That must require frequent requests and responses, if we have used a long-term connection. But not every time the server has new data, do not need to send to the client, then we also accounted for a thread, is not too wasteful?

From the above points analysis. Like online games such occasions, our traditional multi-threaded server has obviously been powerless. The thread pool can mitigate the resource consumption of frequent IO calls to a certain extent, but the pool is limited in size. In the face of thousands of client requests for large concurrency scenarios. It is not always the best solution. Is it possible to maintain very long-lasting connections with one or a small number of threads? A new server model-the non-clogging server model-is described below.

One of the most important features of a non-clogging server model is that it returns immediately after invoking an interface without blocking the wait. 2-6-2-1, when more than one client requests to the server. The server side saves a list of socket connections, and then a dedicated thread polls the list.

Suppose a socket has data that is readable. The corresponding read operation of the socket is called. Conversely, if the socket is found to be writable, the corresponding write operation of the socket is called, and if a socket is found to be interrupted, the socket close operation is called. In order to have better performance. It can also be combined with a thread pool to start another thread to handle once it detects a socket that needs to be processed (read data, write data, close).

watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvd2fuz3lhbmd6agl6ag91/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma== /dissolve/70/gravity/southeast ">

Figure 2-6-2-1 Non-clogging server model

In this way, no matter how many socket connections can be managed by a thread. A thread is responsible for traversing these socket lists, handing them over to the thread pool, making good use of the blocking time and improving processing power.

But such a model involves traversing all of the socket lists, at the same time processing the data stitching. Spare also consumes more CPU resources and is still not suitable for large concurrency scenarios. A little bit more improvement--event-driven model. Its core is event-driven, and thread traversal is not a list of sockets. Instead, it's a test case. Respond to the detected events individually. Greatly improves the detection efficiency. Natural processing power is also stronger.



Like to study Java classmate can make a friend, the following is my number:


Copyright notice: This article Bo Master original articles, blogs, without consent may not be reproduced.

Non-blocking Ioserver models

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.