takeaway

Original article, reproduced please indicate the source.

Netty-source-code-analysis netty-source-code-analysis

After two hors d ‘oeuvres, I already have some fans on my official account, and some fans have added me as friends. Some of my fans have expressed their affirmation of my article through wechat, so I would like to express my gratitude.

Hello, big guy, your public number article is very careful, very good quality, I hope to learn from you in the future, communication

I am also from a netty white began to learn, in the study also encountered a lot of confusion and difficulties. I’ll try to write in plain English (I don’t even use fancy words), and we’ll tell netty bit by bit like a story. I will elaborate on the confusion I once encountered and how I understood it later, and tell you how I went from confusion to understanding.

In this article, we will enter the world of Netty. Today, we will take a peek at netty’s elegant posture. Later, we will take a closer look at her eyes and nose.

1 Overall Architecture

This is a very important diagram, very comprehensive, it not only contains netty components, this diagram is a complete network application developed with Netty. We’re not going to go through this graph today, because we’re going to come back to it, and I’m going to walk you through every single detail of this graph.

  • Channel: indicates a connection.

  • PipeLine: A bidirectional list of channelhandlers.

  • ChannelHandler: ChannelHandler is not marked in the diagram. Each vertical rectangle in the PipeLine represents a ChannelHandler, which is encapsulated as a ChannelHandlerContext when added to the PipeLine. So the vertical rectangle in PipeLine is actually the ChannelHandlerContext. ChannelHandler is our registered IO event and IO command handler.

  • ByteBuf: byte data container.

  • ByteBufAllocator: Class to allocate ByteBuf.

  • EventLoop: Is a thread.

  • EventLoopGroup: a group of eventloops.

2 Channel

Common Channel implementations are NioServerSocketChannel and NioSocketChannel, which correspond to JDK ServerSocketChannel and SocketChannel respectively.

NioServerSocketChannel represents a listening port and does not actually establish a connection. A SocketChannel is a real connection.

3 PipeLine

Each Channel is assigned a PipeLine instance, in which there is a bidirectional list of ChannelHandlderContext, and a ChannelHandlerContext has a ChannelHandler. Head and Tail are the two special ChannelHandlercontexts built into the PipeLine. Other ChannelHandlerContexts such as Decoder, BizHandler and Encoder are added by the user.

PipeLine controls channel-related events and commands that are propagated in each ChannelHandlerContext. You may have read other Netty books that don’t use the word command. Other Netty books call them inbound events and outbound events, and maybe I’m calling them inaccurately and untechnically, but you’ll see the benefits of that as you read through my series. Keep an eye on me.

If you have read other Netty books called inbound events and outbound events and are confused, please contact me to make sure you understand.

4 ChannelHandler

A handler that handles events and commands related to a Channel. Each ChannelHandler is encapsulated into a ChannleHandlerContext and added to the Pipeline. The ChannelHandler in Netty is divided into ChannelInBoundHandler and ChannelOutBoundHandler. The ChannelInBoundHandler handles channel-related events. ChannelOutBoundHandler handles channel-related commands.

Of course, a ChannelHandler can also be both ChannelInBoundHandler and ChannelOutBoundHandler.

5 ByteBuf

ByteBuf corresponds to the JDK’s ByteBuffer, which is a container for byte data. Netty ByteBuf works better than JDK ByteBuffer.

6 ByteBufferAllocator

ByteBuf is generated from ByteBufferAllocator.

In the netty ByteBufferApplocator has two kinds, respectively PooledByteBufAllocator and UnPooledByteBufApplocator. As the name implies, we know that one is pooled and one is not pooled. If we use PooledByteBufAllocator, we can reduce the generation of memory garbage and reduce the CPU overhead when allocating memory.

Seven EventLoop and EventLoopGroup

7.1 EventLoop

EventLoop implements the JDK ScheduledExecutorService interface, which is actually a thread pool, but there is only one thread in the pool, so it can be considered a thread. In Netty, an EventLoop is bound to each Channel. All port binding, connection establishment, read and write operations on the Channel are completed by the EventLoop. If the calling thread is not an EventLoop thread, The call is then wrapped as an asynchronous task and handed over to EventLoop. This is why Future returns are everywhere in Netty, and most of the netty methods we call in our programming will return a Future.

To describe it figuratively, if the car is a Channel and the driver is a thread, but not a thread bound to the Channel, the engine is an EventLoop bound to the Channel. The driver expects the car to walk and cannot directly push the car. When the driver shifts to refuel, it will be packaged into walking instructions (asynchronous task) and handed to the engine to complete. Turning on air conditioning and listening to songs are packaged into power generation commands (asynchronous tasks) that require power from an engine. Although the driver is driving, the engine, not the driver, is the source of power for the car.

7.2 EventLoopGroup

An EventLoopGroup is a set of EventLoops. Netty selects one EventLoop from the EventLoopGroup to bind to the Channel in a polling manner when creating a Channel. Once the binding is completed, The EventLoop will work for the Channel for the rest of its life. Of course you can bind multiple channels to an EventLoop.

8 summarizes

In this article, we have a general understanding of netty components, including Channel, PipeLine, ChannelHandler, ByteBuf, ByteBufAllocator, EventLoop and other key components. Let’s analyze these components one by one.


About the author

Wang Jianxin, senior Java engineer of Zhuan Architecture Department, mainly responsible for service governance, RPC framework, distributed call tracking, monitoring system, etc. Love technology, love learning, welcome to contact and exchange.

Original article, code word is not easy, don’t forget to like, share.