Let’s start with the definition:

Netty is a NIO client server framework which enables quick and easy development of network applications such as protocol servers and clients. It greatly simplifies and streamlines network programming such as TCP and UDP socket server.

Netty is based on NIO network (Socket) client server implementation framework, it simplifies THE TCP/UDP client server programming, developers no longer pay attention to the underlying Socket read and write, Netty also provides many handler implementations (such as HTTP, MQTT, redis protocol, etc.), simplifying the programming complexity based on network protocols.

What is the NIO?

NIO three-piece set:
  • Channel: A Channel stores information about socket connections. ChannelPipeLine (ChannelHandlerContext), EventLoop (an I/O thread), SelectionKey (NIO mode, The NIO server is responsible for responding to the connection of NioServerSocketChannel, and the client is NioSocketChannel
  • Buffer: A Buffer used to interact with a NIO Channel, reading data from a Channel to a Buffer, and writing data from a Buffer to a Channel
  • The Selector: A Selector that allows a thread to listen to read and write states of multiple channels (channels need to register to Selector first). Then trigger the ChannelPipeLine fireChannelRead operation (serial ChannelHandlerContext holds ChannelHandler)/write to the underlying Channel (data from Buffer)

Whereas traditional IO is Stream based, where reads and writes block and the thread cannot do anything while reading or writing Socket data, NIO has a Selector that listens for all channel-related file descriptors (in Linux, Device, Socket connection, etc.) state, so that other threads (business development worker threads) can be released and not blocked (if IO is involved, Future asynchronous processing can be done). Of course, the IO thread corresponding to the Channel (NioEventLoop in NIO) still blocks reading and writing data, but the data is stored in the Buffer, and subsequent processing is buffer-oriented, not stream-oriented.

Let’s see how Netty (NIO mode) corresponds to NIO
  • At the top, the EventLoopGroup that handles client connections typically contains a NioEventLoop, which is a Selector (also a thread, responsible for the NIO) that handles state monitoring of the NioServerSocketChannel. When a connection arrives, execute accept() to create a NioSocketChannel that communicates with the Client. NioSocketChannel selects a NioEventLoop register from the Worker EventLoopGroup (the number of nioEventloops is generated based on the configuration). All subsequent NIO operations for the Channel are handled by the NioEventLoop

NioEventLoop consists of two main operations:

  • ProcessSelectedKeys () : selector function
  • RunAllTasks () : Executes tasks in the task queue, mainly scheduled tasks and read/write tasks added by external worker threads
Where can time-consuming business code be written in Netty?

Netty allows messages to be written in a non-NIO thread, directly if the current thread is the NIO thread corresponding to a Channel, or if not, the writing operation is encapsulated as a WriteTask, which is then periodically processed by NioEventLoop’s runAllTasks()

The relevant code (see AbstractChannelHandlerContext. Class) :

private void write(Object msg, boolean flush, ChannelPromise promise) {
        AbstractChannelHandlerContext next = findContextOutbound();
        final Object m = pipeline.touch(msg, next);
        EventExecutor executor = next.executor();
        if (executor.inEventLoop()) {
            if (flush) {
                next.invokeWriteAndFlush(m, promise);
            } else{ next.invokeWrite(m, promise); }}else {
            AbstractWriteTask task;
            if (flush) {
                task = WriteAndFlushTask.newInstance(next, m, promise);
            }  else{ task = WriteTask.newInstance(next, m, promise); } safeExecute(executor, task, promise, m); }}Copy the code

Historical articles:

  • Spring Boot series 3: Look at Spring’s extension mechanism from Apollo
  • How does Zookeeper ensure sequence consistency
  • Do you really understand volatile
  • Redis Scan algorithm design idea
  • Microservice session crashes
  • Moving from dynamic proxy implementation to Spring AOP is enough
  • How to Quickly get familiar with the Spring Technology Stack
  • Spring Boot series two: One diagram to understand the request processing process
  • Spring Validation implementation principle analysis
  • See the whole Dubbo service reference process in one picture