In the discussion Netty Before the architectural pattern of , Let's introduce Reactor Pattern , because Netty Based on this, the architecture pattern of the
Different threading patterns , It has a great impact on the performance of the program , In order to find out Netty Thread mode , Let's talk about it systematically Each thread mode , Finally, take a look at Netty What are the advantages of the threading model .
The existing thread models are ：
The traditional block I/O
Service model Reactor Pattern
according to Reactor The number of threads processing the resource pool is different , Yes 3 A typical implementation
Master-slave Reactor Multithreading modelMade some improvements , Among them, master and slave Reactor The multithreading model has multiple Reactor)
The yellow box indicates the object , The blue box indicates the thread , The white box means (API)
Use blocking IO Mode to get the input data , Each connection needs an independent thread to complete the data input , Business processing , The data returned
When the concurrency is large , It creates a lot of threads , It takes up a lot of system resources , After the connection is created , If the current thread has no data to read temporarily , The thread will block in read operation , This causes a waste of thread resources
For traditional blocking I/O Of the service model 2 Disadvantages , Solution
be based on I/O Reuse model ： Multiple connections share a blocking object , The application just needs to wait on a blocking object , There is no need to block and wait for all connections . When a connection has new data to process , The operating system notifies the application , Thread returns... From blocked state , Start business processing Reactor The corresponding name is : 1. Reactor mode 2. Distributor mode (Dispatcher) 3. Notifier mode (notifier)
Reuse thread resources based on thread pool ： There's no need to create threads for each connection , After the connection is completed, the business processing task is assigned to the thread for processing , A thread can handle multiple connected businesses .
I/O Reuse combined with thread pool , Namely Reactor Basic design idea of pattern , Pictured ：
- Reactor Pattern , The pattern of passing one or more inputs to the service processor at the same time ( Based on event driven )
- The server-side program processes multiple incoming requests , And synchronously dispatch them to the corresponding processing thread , therefore Reactor The pattern is also called Dispatcher Pattern
- Reactor Mode use IO Reuse listening Events , After receiving the incident , Distribute to a thread ( process ), This is the key to high concurrency of network servers
Reactor：Reactor Run... In a separate thread , Responsible for monitoring and distributing Events , Distribute to the appropriate handler for IO Respond to the incident . It's like a company operator , It takes calls from customers and routes them to the right contacts ;
Handlers： Handler execution I/O Event actual event to complete , It's similar to the actual officials in the company the customer wants to talk to .Reactor Respond by scheduling the appropriate handler I/O event , The handler performs non blocking operations .
Combined with examples ： The server side uses a thread to do all the things through multiplexing IO operation （ Include connections , read 、 Writing, etc ）, The coding is simple , clear , But if the number of client connections is large , Will not be able to support , Ahead NIO The case belongs to this model .
advantage ： Simple model , No multithreading 、 Process of communication 、 Competition issues , All in one thread
shortcoming ： Performance issues , Only one thread , Unable to fully play multi-core CPU Performance of .Handler When processing business on a connection , The entire process cannot handle other connection events , Easy to cause performance bottlenecks
shortcoming ： Reliability issues , Unexpected thread termination , Or into a dead cycle , It will make the whole system communication module unavailable , Cannot receive and process external messages , Cause node failure
Use scenarios ： Limited number of clients , Very fast business processing , such as Redis Time complexity in business processing O(1) The situation of
advantage ： Can make full use of multi-core cpu Handling capacity of
shortcoming ： Multithreaded data sharing and access are complex , reactor Handle all event monitoring and response , Running on a single thread , In high concurrency scenarios, performance bottlenecks are easy to occur .
For single Reactor In multithreaded model ,Reactor Running in a single thread , It is easy to become a performance bottleneck in high concurrency scenarios , It can make Reactor Running in multithreading
advantage ： Simple data interaction between parent thread and child thread with clear responsibilities , The parent thread only needs to receive new connections , Subthread completes subsequent business processing .
advantage ： Simple data interaction between parent thread and child thread ,Reactor The main thread only needs to pass the new connection to the child thread , Child threads do not need to return data .
shortcoming ： Programming complexity is high
Combined with examples ： This model is widely used in many projects , Include Nginx Master-slave Reactor Multi process model , Memcached Master slave multithreading ,Netty Support of master-slave multithreading model