What is a Reactor

The three IO modes and corresponding development modes are as follows:

BIONIOAIOThread-Per-ConnectionReactorProactor

Reactor is a development model with the following core processes:

1. Register the event of interest; 2. Scan for the occurrence of interested events; 3

In short, register events, select events, Dispatch events, and Handle events.

We found that the whole Reactor model revolves around events from the beginning to the end. The following table corresponds to events monitored by different channels in Netty.

client/serverSocketChannel/ServerSocketChannelOP_ACCEPTOP_CONNECTOP_READOP_WRITEclientSocketChannelYYYserverServerSocket ChannelYserverSocketChannelYY

  • Client-oriented SocketChannels listen for connection, read, and write events.
  • The Accept () method is called and the corresponding SocketChannel is created.
  • For a service-oriented SocketChannel, that is, the created sub-channel, it listens for read and write events.

After the above events are registered, a multiplexer will scan and listen for these events, which will be handled by Netty.

Two, Reactor three versions

In network programming, if each client maintains a connection to the server, it is a great stress on the server. For example, BIO, which is the single-threaded mode of the table above:

There are three links between the three clients and the server, and for each link there is a Handler to handle the corresponding event: read, decode, calculate, encode, and respond, which takes up three threads. The read and response blocks the thread, and a serious problem arises: the more connections are blocked, the more threads are occupied.

This way, abstracted into code, is:

Let’s look at how Reactor addresses these issues.

1. Reactor Single-threaded model

Single threading is relatively simple. All the work is done by one thread, including receiving connections, registering events, scanning, distributing, processing, etc. The blocking problem is solved, but the single thread is not efficient, and once the thread dies, the whole system is game over, which is intolerable.

2. Reactor Multithreaded model

To solve the single-threaded problem, we introduced thread pools. In this pattern, the time-consuming decode, compute, and encode operations are handed over to the thread pool. Compared with single thread mode, the efficiency is significantly improved.

3. Reactor multithreaded model

Based on the previous version, the primary Reactor handles receive connections separately and registers the established SocketChannel with the specified secondary Reactor using the master-slave model.

Use the Reactor model in Netty

When the number of threads is not specified, the optimal number of threads is calculated based on the number of CPU cores. In the third master-slave mode, bossGroup is responsible for receiving connections and registering SocketChannel with workGroup, and woekGroup is responsible for handling other events.

Original link: aysaml.com/articles/20…

Three things to watch ❤️

If you find this article helpful, I’d like to invite you to do three small favors for me:

1. Like, forward, with your “like and comment”, is the power of my creation.

2. Follow the public account “Wish heaven has no BUG” and share original knowledge from time to time. Also look forward to the follow-up article ing

3. Reply [Learning] scan the code to obtain the learning materials package

Author: Write code by mouth link: juejin.cn/post/693979… The copyright belongs to the author. Commercial reprint please contact the author for authorization, non-commercial reprint please indicate the source.