Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is Reactor mode

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly shows you "what is Reactor mode", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "what is Reactor mode" this article.

Mind map

Mind Map 1, Reactor Model introduction 1.1.What is Reactor Mode

Reactor model is generally translated as "reactor mode", also known as "distributor mode". It is a design pattern that submits client requests to one or more service handlers. How it works is that a thread receives all requests and then dispatches them to the relevant worker thread.

1.2 Why use Reactor mode

In java, socket was used to program before there was no NIO. Socket receives requests that are blocked and need to process one request before processing the next, so performance is poor in the face of highly concurrent service requests.

Then someone would say to use multithreading (as shown in the following figure). When a request is received, a thread is created so that it does not block. In fact, this can play a certain role in improving performance, but when there are many requests, a large number of threads will be created, maintenance threads need to consume resources, and switching between threads also needs to consume performance. And the number of threads created by the system is limited, so when there is high concurrency, it will drag down the system directly. Because of the above problems, the Reactor mode is proposed.

Based on Java,Doug Lea (author of Java concurrent package), three forms are proposed: single Reactor single thread, single Reactor multithread and multi Reactor multithread.

2. The evolution of Reactor model.

Before introducing the three Reactor modes, let's briefly explain the three roles:

Reactor: responsible for responding to the event, distributing the event to the Handler bound to the corresponding event, or, in the case of a connection event, to the Acceptor. Handler: event handler. Responsible for executing the business logic corresponding to the event. Acceptor: the connect event is bound, and when the client initiates a connect request, Reactor distributes the accept event to the Acceptor for processing.

2.1 single Reactor single thread

Work flow

Only one select loop receives the request, and the client (client) registers in and the Reactor receives the registration event, which is then distributed by the reactor (dispatch) and handled by the following processor (Handler).

Popular interpretation

There is only one person in a restaurant who is both a receptionist and a waiter, who is responsible for receiving guests and delivering their orders to the chef.

The characteristics of single Reactor and single thread

The problem with single threading is actually obvious. As long as one of the Handler methods is blocked, it will cause the Handler of all client to be blocked, and the registration event will not be handled, unable to receive new requests. Therefore, this model is used less, because it can not make full use of multi-core resources.

This mode can only deal with scenarios that Handler completes relatively quickly.

2.2 single Reactor multithreaded workflow

In multithreaded Reactor, registration receiving events are done by Reactor, while other calculations, codec and codec are done by a thread pool. You can see from the figure that the worker thread is multithreaded and the Reactor that listens for registered events is single-threaded.

Popular interpretation

It is equivalent to having a receptionist and multiple waiters in the restaurant. The front desk is only responsible for receiving guests, and the waiter is only responsible for serving guests.

The characteristics of single Reactor multithreading

Compared with the single-threaded Reactor model, the multithreaded Reactor model leaves the worker thread pool for processing during Handler read and write processing, which does not cause the Reactor to fail to execute, because Reactor distribution and Handler processing are separate and can make full use of resources. So as to improve the performance of the application.

Disadvantages: Reactor only runs in the main thread and is responsible for listening and responding to all events, which will still cause performance bottlenecks in high concurrency scenarios for a short time.

2.3MultiReactor multithreaded workflow

1. MainReactor is responsible for listening to client requests, specially dealing with the establishment of new connections, and registering the established connections with subReactor.

2. SubReactor adds the assigned connection to the queue for listening. When a new event occurs, the Handler corresponding to the connection is called for processing.

Popular interpretation

It is equivalent to that there are multiple receptionists and waiters in the restaurant, the receptionist is only responsible for receiving guests, and the waiter is only responsible for serving guests.

The characteristics of multi-Reactor and multi-thread

MainReactor is mainly used to handle client requests for connection establishment. SubReactor mainly does and establishes connections to do data exchange and event business processing operations, one thread for each subReactor.

This model makes each module more specialized, less coupled, and can support higher concurrency. Many frameworks also use this pattern, such as the Netty framework that we will talk about next.

Third, the application in Netty

Netty is the best of the best in the framework, and it is impossible to summarize it with a picture or a paragraph, so I will only analyze the architectural model of the Netty framework. Continue to delve deeper into Netty in the next article. This architecture is actually similar to the multi-Reactor multithreading model.

1. BossGroup is the equivalent of mainReactor and is responsible for establishing the connection and registering the connection with the WorkGroup. WorkGroup is responsible for handling read and write events corresponding to connections.

2. BossGroup and WorkGroup are two thread pools with multiple NioEventGroup (actually threads). The default number of threads in BossGroup and WorkGroup is twice the number of cpu cores (reflected in the source code).

3. Each NioEventGroup is an infinite loop, which is responsible for listening for corresponding events. 4. Pipeline (channel) contains multiple ChannelHandler (business processing), which are executed sequentially.

The above is all the content of this article "what is Reactor Mode?" Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report