IO design mode: Reactor vs. Proactor

Source: Internet
Author: User
Tags epoll

IO design mode: Reactor vs. Proactor

Open source products such as Redis, ACE, and event model are used in the reactor mode, while the same event handling proactor, due to operating system reasons, the relevant open source products are also few; here to learn its model structure, the focus of comparison between the two similarities and differences;

Reactor Reactorreactor pattern Structure

The reactor contains the following roles:

    • Handle handle; Used to identify a socket connection or open a file;
    • Synchronous event Demultiplexer: Synchronous events Multiplexer: A function implemented by the operating system kernel, used to block one or more events that are waiting to occur on the handle collection, such as Select/epoll;
    • Event Handler: Events Processing Interface
    • Concrete Event Handlera: Implements the specific incident handling logic provided by the application;
    • Reactor: A reactor that defines an interface for the following functions:
      1) for application registration and deletion of concerned event handlers;
      2) Run the event loop;
      3) When a ready event arrives, the distribution event is processed on the previously registered callback function;

The origin of "reaction" in the name of "reaction":
"Reaction" is "inverted", "control reversal", the specific event handler does not call the reactor, but the reactor is assigned a specific event handler, the specific event handler for a specified event to respond; this reversal of control is also known as the "Hollywood Rule" (Don't call me, let me call you)

Business process and timing diagram

    1. The application launches and registers the concerned event handle with the reactor;
    2. Call reactor, enter the infinite event loop, wait for the event to register to arrive;
    3. Event arrives, select returns, reactor the event to the previously registered callback function;
Proactorproactor mode structure of the active device

The Proactor Active mode contains the following roles

    • Handle handle; Used to identify a socket connection or open a file;
    • Asynchronous Operation Processor: asynchronous operation processor, responsible for performing asynchronous operations, generally implemented by the operating system kernel;
    • Asynchronous Operation: Asynchronous operation
    • Completion event queue: Completion of events queues, results of asynchronous operations completed in queue for subsequent use
    • Proactor: An active device, providing an event loop for an application process, and extracting the results of an asynchronous operation from the completion event queue, distributing calls to the corresponding subsequent processing logic;
    • Completion Handler: Completes the event interface, usually consists of a callback function interface;
    • Concrete completion Handler: Completes the event processing logic, implements the interface definition specific application processing logic;
Business process and timing diagram

    1. The application starts by invoking the asynchronous operation interface function provided by the asynchronous operation processor, and the application and asynchronous operations are run independently after the call, and the application can invoke the new asynchronous operation, while other operations can be performed concurrently;
    2. The application launches the Proactor, carries out the infinite event loop, waits for the completion event to arrive;
    3. The asynchronous Operation processor performs an asynchronous operation that, when completed, puts the result into the completion event queue;
    4. The actuator extracts the result from the completion event queue and distributes it to the corresponding completion event callback function processing logic;
Compare the difference between active and passive

Take active writing as an example:
Reactor the handle to select (), waits for the write to be ready, and then calls write () writes the data, and finishes processing the subsequent logic;
Proactor call Aoi_write immediately after the return, the kernel is responsible for the write operation, after writing, call the corresponding callback function to process the subsequent logic;

It can be seen that the reactor passively waits to indicate the arrival of the event and reacts; it has a waiting process, which is to be put into the listener event collection to wait for handler to be available;
Proactor directly calls the asynchronous read and write operation, returns immediately after the call is finished;

Realize

Reactor realizes a passive event separation and distribution model, the service waits for the arrival of the request event, and then reacts by the uninterrupted synchronous processing of the event;

Proactor implements an active event separation and distribution model, which allows multiple tasks to be executed concurrently, resulting in increased throughput and time-consuming tasks that are not affected by each task.

Advantages

The reactor is relatively simple, and it is efficient for processing scenes with short time;
The operating system can wait on multiple event sources and avoids the performance overhead and programming complexities associated with multithreaded programming;
The serialization of events is transparent to the application and can be executed synchronously without the need of locking;
Transaction separation: Separates application-independent, multi-path decomposition and allocation mechanisms from application-related callback functions,

Proactor higher performance, able to handle time-consuming concurrency scenarios;

Disadvantages

Reactor processing time-consuming operations can cause the congestion of event distribution and affect the processing of subsequent events;

Proactor is a complex logic, relying on the operating system for asynchronous support, the implementation of pure asynchronous operation of the operating system is few, the implementation of excellent such as Windows IOCP, but due to its Windows system for the limitations of the server, the current scope of application is small, and unix/ Linux system support for pure async is limited, the application of event-driven mainstream or through the select/epoll to achieve;

Applicable scenarios

Reactor: Receive multiple service requests at the same time, and sequentially synchronize their event drivers;
Proactor: An event driver that receives and processes multiple service requests asynchronously;

The source files for class diagrams and sequence diagrams are available for download, as described in the GitHub design mode EA Documentation:

Https://github.com/me115/design_patterns

Reference

Schema-Oriented Software Architecture Volume 2
Schema-Oriented Software architecture Volume 4
"Pictorial Design Pattern"

Posted by: Big CC | 27apr,2015
Blog: blog.me115.com [Subscribe]
Weibo: Sina Weibo

IO design mode: Reactor vs. Proactor

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.