Netty-threading model

Preface

Before we want to understand Netty, we need to understand the operating mechanism of Nio. When using Netty for server-side development, we usually set the following code:

EventLoopGroup bossGroup = new NioEventLoopGroup(1);
EventLoopGroup workerGroup = new NioEventLoopGroup(2);

The reason for the above code setting is explained below from the threading model.

Reactor thread model (Nio multiplexing + thread pool)

  • The Reactor model is based on event-driven development. The core components include Reactor and thread pool. Reactor is responsible for monitoring and distributing events, and thread pool is responsible for processing events. According to the number of Reactor and the number of thread pools, Reactor is divided into three types. model:

    • Single thread model (single Reactor single thread)
    • Multi-threaded model (single Reactor multi-threaded)
    • Master-slave multi-threading model (multi-reactor multi-threading)

Single thread model

Insert picture description here * Reactor internally monitors connection events through selectors, and distributes them through dispatch after receiving the events.

  • If it is a connection establishment event: it must be received by the selectedKey containing the acceptor. At this time, a new Handler is called and the SocketChannel is registered as a Read event, and the handler is associated with the selectedKey
  • If it is a read-write event: directly use k.attachment() to call out the corresponding Handler to process the read service and the write service.
    • Handler completes the business process of read->(decode->compute->encode)->send
    • The advantages of this model are simple, but the disadvantages are obvious. When a Handler is blocked, it will cause the handlers and accpetors of other clients to not be executed, and cannot achieve high performance. It is only suitable for scenarios where business processing is very fast.

Multithreaded model

Insert picture description here

  • Similar to the single-threaded model, the difference is that when reading and writing, tasks are placed in the thread pool and processed in parallel.

Master-slave multithreading model

Insert picture description here

  • Set up more selectors to receive events! Register to a different selector every time you receive it, and let it handle the event by stream

Netty's master-slave multithreading model

  • The master-slave multithreading model has multiple Reactors, that is, there are multiple selectors, so we define a bossGroup and a workGroup. The core code is as follows:
NioEventLoopGroup bossGroup = new NioEventLoopGroup();
NioEventLoopGroup workerGroup = new NioEventLoopGroup();
ServerBootstrap bootstrap = new ServerBootstrap();
bootstrap.group(bossGroup,workerGroup)
        .channel(NioServerSocketChannel.class)
        .option(ChannelOption.TCP_NODELAY, true)
        .option(ChannelOption.SO_BACKLOG, 1024)
        .childHandler(new ServerHandlerInitializer());

Note : In fact, in Netty, the bossGroup thread pool will only randomly select a thread to handle client connections. At the same time, NioServerSocetChannel is bound to the bossGroup thread, and NioSocketChannel is bound to the workGroup thread

Guess you like

Origin blog.csdn.net/null_zhouximin/article/details/112913182