[Design Pattern] Reactor mode

Source: Internet
Author: User

References

• Wikipedia:Https://en.wikipedia.org/wiki/Reactor_pattern

Event-driven mode

       we already know that for a host, the CPU processing speed is much higher than the IO processing speed. If for an IO operation (such as charging a piece of data from a socket), the application blocking waits for the completion of the IO operation is not cost-effective, which wastes the CPU time allocated to the process. A good practice is to allocate multiple processes or multiple threads for IO processing, but this can lead to the overhead of process switching or thread switching. In this case, a process reading a piece of data will take 500ms, during which time it switches to the process 3 times, but at this point nothing can be processed and wasted CPU. The event-driven mode , or the proposed event callback method , is an effective solution to this problem. In this way, the application-layer service registers an event callback service with an "intermediary", and when IO is ready (readable or writable), the "middleman" throws an event , and notifies the appropriate event callback function for processing. The event callback method is also the "Hollywood Principle" (Hollywood principle : Don ' t call us, we'll call you) in the software development of a embodiment.

In fact, the event-driven mode can easily find the corresponding scene in real life, take the food service as an example: Every customer comes to a meal will be issued a service request, and then browse the menu, and finally order a meal. This process requires our service personnel to handle these meal requests. In multithreading mode, this is the process: a customer opens a meal, needs a waiter to the service, the customer browses the menu, then a la carte, and finally the service staff will submit the menu to the chef. When 5 people are seated at the same time, 5 attendants are required to go to the service. This is the idea of multithreading, when a business request arrives, it needs a dedicated thread for processing. This approach gets better user experience when people are less, and every customer gets the service in time. In this service attitude, the reputation of this shop is recognized, so come to eat more customers, so the same time will come to 10 customers dining. Restaurant owner is very happy, but at this time only 5 waiter, in order not to reduce the quality of service, the boss hired 5 waiter. More and more people are satisfied with this restaurant, the customer gradually more up, at the same time come to dinner people arrived at 20, when the boss worried, if hired 10 waiter, will inevitably affect the profit of the restaurant revenue; If you do not hire a waiter, and some are receiving waiter service customers ordering is very slow, This will inevitably reduce the quality of service for other customers. The boss later observed that most guests order the process is relatively slow, the waiter's time is idle waiting for the guests to order the process, so the boss finally found a new method: When Guests browse the menu, the waiter can go to greet other guests, and so guests browse the menu, greet a "waiter", There will be a waiter coming to the service right away. The guest greets the waiter is initiates an event, the waiter goes to the service is this event callback service (event Handler). Eventually the boss left a decent number of waiters based on the amount of business concurrency.

Introduction to Reactor mode

reactor mode , the operating system is only responsible for notifying IO readiness , while the specific IO operation is still handled by the business process, the proactor mode is further based on this, the operating system completes the IO operation, and the event processing process is only responsible for processing the business logic, so that IO and program processing asynchronous execution. So we generally call reactor mode synchronous io mode , and proactor mode is asynchronous IO mode .

The General I/O multiplexing mechanism relies on an event demultiplexer. The event multiplexer object separates the IO events (events) from the event source and distributes them to the corresponding IO event handlers (event Handler). Developers need to pre-register events and their event handlers (or callback functions ). Two design patterns related to the event multiplexer are the reactor and proactor modes mentioned above.

The reactor mode goal is to separate one or more User Service requests (Demultiplex) and Dispatch (Dispatch) to the application, based on the event-driven mechanism, Enables applications to handle multiple service requests simultaneously and in an orderly manner.

reactor mode UML Structure diagram

• Structure description

In reactor mode, there are 5 key players.

? Event Source- Resource Handle

? Synchronous event Splitter- synchronous event Demultiplexer

[Design Pattern] Reactor mode

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.