Analysis of Java NIO Network Framework Model of Netty and Jetty

Netty’s NIO frame model. In the previous article, in order to solve the problem of Jetty, the Asynchronous IO Framework Model of Java NIO based on the multi-channel-based event separator is analyzed. There has been no systematic analysis of Netty and Jetty network model. Two days, the code of the two network frames is readfully read, and the two network models are sorted out, and the model of Netty is drawn:

In the figure, each listener creates an Acceptor Reactor, monitors the multi-channel separator by the BOSS thread. Here is only the connection event. When a new establishment connection request is reached, the thread will respond, will receive The request is registered in the event multiple-channel separator, and the event multi-channel separator has multiple. By default, the number is twice as the number of CPU cores should be the number of CPU hyperthreades. Here, each of the reached connections, the sequence number is taken to the separator (one-dimensional space of Hash to 0 to 3), and is assigned to the corresponding separator based on the mode. The event separator began to listen to the new read and write events above. Check the thread to niworker. The read and write data is implemented by callbacking the corresponding interface of the user registered Handler. Therefore, in the case of processing time consuming data, it is necessary to submit it to the background thread without blocking the event separator, otherwise the new connection cannot be established, and the other concurrent requests cannot be processed.

Jetty is very different from Netty in the code style. Seeing Jetty code is clearer, it may be because of the previous processing problems. The previous article also said that Jetty is called a synchronized Accept () method to wait for a new connection request. When the new connection arrives, generate a new Change event to the multiplexer, the same There are also multiple multiple separators, and the selection principle is exactly the same as NETTY. Simple polling implements load balancing. This is a mode of typical semi-synchronous semi-asynchronous (HALF-SYNC / HALF-ASYNC). When only one event separator is used, it will find that the distribution thread usually introduces a lot of problems. The problems mentioned in the previous two articles are related to this.

I don’t know why Jetty and Netty are different in accepting new requests. Is there a short connection in the NETTY, while Jetty is more detrimental to long connection, such as HTTP connections? Appropriate concurrent tests will explain the problem. If Netty’s way is very good, Jetty should change it to this way.