Netty内部实现 1 消息处理主框架
Netty内部实现 一 消息处理主框架
1、EventExecutorGroup 内部维护多个消息循环,每一个group由一个EventLoop(EventExecutor)来监听和回调。具体实现类MultithreadEventExecutorGroup:内部使用children维护了多个EventLoop,默认分配策略是依次为每个channel分配EventLoop。一个eventLoop循环多个连接获取消息。这样保证每个连接的消息是单线程的。
2、EventLoop的具体实现是EpollEventLoop或NIOEventLoop,内部逻辑是每个obj对应一个线程,一个blockingqueue,一个事件循环。每一个循环周期,根据ioRatio,分别进行io事件监听和处理对应的回调。其中,processReady 回调各个handler。runAllTasks 依次回调LinkedBlockingQueue里的其他任务,在handler里面可能会扔过来runnable,基于时间的调度也会有runnable
3、processReady 过程
processReady->AbstractEpollUnsafe.epollInReadyRunnable->EpollServerSocketUnsafe.epollInReady->pipeline.fireChannelRead->各个handler
4、handler中挂回调
ctx.executor().execute(command);