Netty (16)-Netty model

Netty has made certain improvements based on the master-slave Reactors multi-threading model. Among them, the master-slave Reactor multi-threading model has multiple Reactors.

One, the simple version of the Netty model

Netty model simple version

1) The BossGroup thread maintains the Selector and only pays attention to the Accept event.
2) When the Accept event is received, the corresponding SocketChannel is obtained, encapsulated into a NIOSocketChannel, and registered to the Worker thread (event loop) for maintenance
3) When the Worker thread listens After an event occurs in the channel registered in the Selector, it will be processed (completed by the Handler). Note: the Handler has been added to the channel

Second, the advanced version of the Netty model

Advanced version of Netty model

Third, the detailed version of the Netty model

Netty model detailed version

1) Netty abstracts two groups of thread pools, BossGroup, which is specifically responsible for receiving client connections, WorkerGroup is specifically responsible for network reads and writes
2), BossGroup and WorkerGroup types are both NioEventLoopGroup
3), NioEventLoopGroup is equivalent to an event loop group, this group contains Multiple event loops, each event loop is NioEventLoop
4), NioEventLoop represents a thread that performs processing tasks in a continuous loop, and each NioEventLoop has a selector to monitor the socket network communication bound to it
5), NioEventLoopGroup can have multiple threads, that is, it can contain multiple NioEventLoop
6). There are three steps in the loop execution of NioEventLoop under each BossGroup:

  1. Poll accept event
  2. Process the accept event, establish a connection with the client, generate a NioSocketChannel, and register it to the selector on the NioEventLoop under a WorkerGroup
  3. Process the tasks of the task queue, namely runAllTasks

7) Steps executed by the NioEventLoop loop under each WorkerGroup:

  1. Polling for read and write events
  2. Process IO events, namely read and write events, and process them in the corresponding NioSocketChannel
  3. Process the tasks of the task queue, namely runAllTasks

8) NioEventLoop under each WorkerGroup will use pipeline (pipeline) when processing business, pipeline contains channel, that is, the corresponding channel can be obtained through pipeline, and many processors are maintained in the pipeline

9) NioEventLoop adopts a serialized design internally. From message reading->decoding->processing->encoding->sending, the IO thread NioEventLoop is always responsible

  • NioEventLoopGroup contains multiple NioEventLoops
  • Each NioEventLoop contains a selector and a taskQueue
  • Each NioEventLoop selector can be registered to monitor multiple NioChannels
  • Each NioChannel will only be bound to a unique NioEventLoop
  • Each NioChannel is bound to its own ChannelPipeline

Guess you like

Origin blog.csdn.net/yangxshn/article/details/113954591