How to deal with complex and time-consuming business logic in Netty?

problem description:

one or more channel we know will register on EventLoop . This EventLoop not only listens to events on Channel , but also handles business logic (including outbound logic) after the event arrives, that is, Netty handler calls on the same Channel are serial, as shown in the figure below. It causes some simple events to go unanswered (it could also be events on other channel ). So Netty is not recommended to handle complex business logic, such as database operations, in handler . What about time-consuming logic like this in Netty ?

clipboard.png

my thoughts

I think there should be two ways to deal with it

  1. uses a custom thread pool to encapsulate the time-consuming logic that needs to be processed into task and throw it into executor pool , but we know that having too many threads is not a good thing and will affect throughput.
  2. throw these tasks into the idle EventLoop so that you can make full use of the thread resources of Netty , but how can such code be implemented? is there a mature framework?
Apr.29,2022

further study should be to find the answer!

EventLoop is called EventLoop because EventLoop requires cyclic listening for IO events, and any time-consuming logic may cause IO events to be unresponsive, so the second option is not advisable, although the general rule for Netty applications is to reuse EventLoop as much as possible to reduce thread creation overhead, but the premise is that the network IO sensitivity is not affected.

therefore, a special business thread pool is needed to deal with complex and time-consuming business logic. As long as the number of threads in the business thread pool and event loop thread pool is set reasonably, it will not have much impact on throughput. There are usually two ways to implement a business thread pool:

  1. in ChannelHandler , use the custom Java thread pool.
  2. with the business thread pool provided by Netty , ChannelPipeline has some add () methods that receive a EventExecutorGroup . If an event is passed to a custom EventExecutorGroup , it will be handled by some EventExecutor contained in this EventExecutorGroup . Netty provides a default implementation called DefaultEventExecutorGroup .

clipboard.png


does the business logic you refer to refer to the logic in the corresponding channelRead method in LoggingHandler in your code above? In your way, the thread that ultimately executes the logic in the channelRead method will be the thread in the thread pool you passed in?

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1ea8b37-5cd14.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1ea8b37-5cd14.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?