Introduction to Reactor model

Reactor model

The above picture is the most authoritative definition of the role of the Reactor model:

The role composition of Reactor mode (a total of 5 roles):

1. Handle (handle or descriptor):

It is essentially a resource, provided by the operating system; changing the resource is used to represent events, such as file descriptors, or Socket descriptors in network programming. Events can come from external or internal; external events such as client connection requests, data sent by the client, etc.; internal events such as timer events generated by the operating system. It is essentially a file descriptor. Handle is the birthplace of the event;

2. Synchronous Event Demultiplexer (synchronous time separator):

It itself is a system call, used to wait for the occurrence of an event (there may be one or more than one). The calling method will be blocked when it is called, until there is an unknown event on the synchronous event separator. For Linux, the synchronous event separator refers to the commonly used I/O multiplexing mechanism. For example, select, poll, epoll, etc. In the Java NIO field, the corresponding component of the synchronous event separator is the Selector; the corresponding blocking method is the select method;

3. Event Handler:

It is composed of multiple callback methods, and these callback methods constitute an application-related feedback mechanism for an event. Compared with Java NIO, Netty has upgraded the role of event handlers. It provides developers with a large number of callback methods for us to implement corresponding callback methods for business logic when specific events are generated. deal with.

4. Concrete Event Handler (specific event handler):

It is the realization of the event handler. It itself implements the callback methods provided by the event handler, thus realizing business-specific logic. It is essentially the implementation of each processor we have written

5. Initiation Dispatcher (initialization dispatcher):

In fact, it is the Reactor role in Netty. It itself defines some specifications, which are used to control the scheduling of events, and at the same time provide facilities for applications to register and delete event handlers. It itself is the core of the entire event processor, the Initiation Dispatcher will wait for the event to occur through the synchronization time separator. Once the event occurs, the Initiation Dispatcher will wait for the event to occur through the synchronization event separator. Once an event occurs, the Initiation Dispatcher first separates each event, then calls the event handler, and finally calls the related callback method to handle these events.

 


Execution process of Reactor mode

1. When the application registers a specific event handler with the Initiation Dispatcher, the application will identify that the event handler wants the Initiation Dispatcher to notify the event when an event occurs, and the event is associated with the Handle.

2. The Initiation Dispatcher will require each event handler to pass the internal Handle to it. The Handle identifies the event handler to the operating system.

3. After all event handlers are registered, the application will call the handle_events method to start the event loop of the Initiation Dispatcher. At this time, the Initiation Dispatcher will merge each Handle, and use the synchronous event separator to wait for these events to occur. For example: the TCP protocol layer uses the select synchronization event separator to wait for the data sent by the client to arrive on the socket handle;

4. When the Handle corresponding to an event source becomes ready (for example, when TCP Socket becomes waiting for reading), the synchronization event separator will notify the Initiation Dispatcher;

5. The Initiation Dispatcher will trigger the event handler callback method to respond to the Handle in the ready state. When an event occurs, the Initiation Dispatcher will use the Handle activated by the event source as the Key to find and distribute the appropriate event handler callback method;

6. The Initiation Dispatcher will call back the event handler handle_events callback method to perform application-specific functions (business logic written by the developer) in response to this event. The type of event that occurs can be used as the method parameter and used internally by the method to perform additional service-specific separation and development;


The Reactor model described by Master Doug Lea has the same meaning as the picture above

 

Initiation Dispatcher corresponds to the Reactor role

 

 

 

 

 

Guess you like

Origin blog.csdn.net/qq_36807862/article/details/95465561