The design pattern of the object behavior class, sorting and distributing synchronization events. Alias Dispatcher(dispenser)
The reactor pattern is a common pattern for handling concurrent I/O, and is used for synchronous I/O, with the central idea of registering all I/O events to be processed on a central I/O multiplexer while the main thread blocks on the multiplexer; once i/ o The event arrives or is ready (the difference is whether the multiplexer is an edge or horizontal trigger), and the Multiplexer returns and distributes the appropriate I/O events to the corresponding processor.
The event handling mechanism of Reactor
normal function call mechanism: The program calls a function----function execution, the program waits----function returns the result and control to the program----Program to continue processing. The so-called event-driven, simply is what you click on the button ( i.e. what event is generated ), what the computer does ( that is, what function to call ) .
Event-driven models
Reactor interpretation "Reactor" is an event-driven mechanism. Unlike normal function calls, the application is not actively invoking aAPIfinish the deal, but on the contrary,Reactorreverse the event processing process, the application needs to provide the appropriate interface and register toReactoron, if the corresponding time occurs,ReactorThe interfaces that the application registers are actively invoked, which are also known as"Callback function". UseLibeventis alsoto Libeventthe framework registers the appropriate events and callback functions, and when these times are audible,LibeventThese callback functions are called to handle the corresponding event (I/Oread-write, timing, and signal).
The reactor pattern is very similar to the observer model in some ways: when a subject changes, all the dependent bodies are notified. However, the observer pattern is associated with a single event source, while the reactor pattern is associated with multiple event sources.
Advantages of the Reactor model
Reactor mode is one of the prerequisites for writing high-performance Web servers , and it has the following advantages:
- The response is fast and does not have to be blocked for a single synchronization time, although Reactor itself is still synchronous;
- programming is relatively simple, can avoid complex multi-threading and synchronization problems, and avoid multithreading / The switching overhead of the process;
- Scalability , which can be easily increased by increasing Reactor The number of instances to take full advantage of the CPU resources;
- Reusability , reactor The framework itself is independent of the specific event processing logic and has high reusability.
Reactor mode framework
Using the reactor model, several components are required: event source, reactor framework, multiplexing mechanism, and event handlers .
The overall framework of the Reactor model
Reactor Model UML
Handle represents the management of the operating system resources, including: network links, open files, timers, synchronization objects and so on. Linux is a file descriptor, on Windows is the Socket or Handle , where unification is called a "handle set", and the program registers the event of interest, such as an I/O event, on the specified handle .
- Event demultiplexer--Events Multi-channel distribution mechanism
The I/O multiplexing mechanisms provided by the operating system, such as SELECT and Epoll. The program first registers its concerned handle (event source) and its events on the event demultiplexer;
when an event arrives, event Demultiplexer notifies "the event is ready for one or more handles in a registered handle set," And when the program receives a notification, it can be processed in a non-blocking situation. corresponding to Libevent, still select, poll, Epoll, etc., but libevent is encapsulated with struct eventop, with a unified interface to support these I/O multiplexing mechanisms, It achieves the purpose of hiding the underlying system mechanism from the outside.
Event separators, which are provided by the operating system, are generally called on Linux, such as SELECT, poll, Epoll, and so on, waiting for events to occur on a handle collection. Accepts a client connection, establishes an event handler for the client, and registers this event handler (Reactor) with the event dispatcher (Handler).
Reactor , is an interface for event management, internal use Event Demultiplexer Register, unregister events, and run event loops when there are events entering when the "Ready" state is called, the callback function that invokes the registration event handles the event.
Provide interface: Register, delete and distribute Event Handler. event demultiplexer waits for events to occur, and when a new event is detected, the event is given to initiation Dispatcher, which goes to the callback Event Handler.
corresponding to the libevent, is the event_base structure. A typical reactor declaration method
- Event handler--Events Handler
The event handler provides a set of interfaces, each of which corresponds to a type of event that Reactor is invoked when the corresponding event occurs and performs the corresponding event handling. Typically, it binds a valid handle.
The event handler, which handles processing functions for a particular event. Generally in the basicHandlerThere will also be further layers of abstraction that can be used to abstract suchDecode,Processand theEncoderthese processes. Like theWeb Serverterms,Decodeis usuallyHTTPthe parsing of the request,Processthe process will further involveListnerand theServletof the call. To simplify the design,Event Handleris usually designed as a state machine,GoFof theState Patternto achieve. corresponds to the libevent, which is the event structure body. Here are two typical event handler class declarations, each with their pros and cons.
inherit the above class to implement the Hook method. The application registers the concrete event Handler with the Reactorand waits for the events to be processed. When an event occurs, these methods are called back.
examples of application scenarios
Scene: Long-distance bus on the road, some people get off the train, but passengers always want to be able to get a rest on the bus.
Traditional practice: Every time (or every station), the driver or conductor asks each passenger whether to get off the bus or not.
Reactor Practice: The car is the passenger access to the main body (Reactor), after the passenger on the bus to the conductor (acceptor) Registration, then the passengers can rest and sleep, When the passenger arrives at the destination, the conductor wakes it up.
[Libevent] Reactor reactor design mode