netty4.1.74源码分享
2022-08-25 17:08:15 0 举报
netty4.1.74源码,感兴趣的可以看看
作者其他创作
大纲/内容
new.super()
异步删除复制出的fd管理对象 SelectorImpl.processDeregisterQueue() ->WindowsSelectorImpl.implDereg(SelectionKeyImpl ski) ->AbstractSelectableChannel.removeKey(SelectionKey k) ->SocketChannelImpl.kill() ->SocketDispatcher.close(FileDescriptor fd) ->native void close0(FileDescriptor fd) 关闭复制的FD
ServerBootstrap
2. 完成doClose0方法后,执行netty对象的关闭流程AbstractChannel.fireChannelInactiveAndDeregister(wasActive)
startThread()
findContextInbound
option
生成Runnable
invokeChannelRead
native poll0
NioEventLoopGroup
1. fd内容读取不到,即返回-1,并设置close=true,关闭fdAbstractChannel.doClose0(promise)
启动异步任务消费
AbstractBootstrap
SingleThreadEventExecutor
execute
通过NioEventLoop.run()触发,关注参数strategy
DefaultChannelPipeline
fireChannelRead
SingleThreadEventLoop
epoll
初始化NioServerSocketChannel
select(curDeadlineNanos)监听客户端事件,没有任务时死等,直到客户端发请求
循环NioSocketChannel触发fireChannelRead
消费Runnablepipeline添加ServerBootstrapAcceptor
step1NioEventLoopGroup初始化过程
bind
DefaultSelectorProvider
MultithreadEventLoopGroup
1. 将work的handler加入到pipeline2. 设置work的参数3. register channel,逻辑同boss
ChannelInitializer
根据线程数参数循环创建出对应NioEventLoop
doBind
初始化children的EventExecutor
read()
boss biz handler
select
pipeline.fireChannelRead(byteBuf);pipeline.fireChannelReadComplete();
AbstractNioByteChannel.NioByteUnsafe.closeOnRead(ChannelPipeline pipeline) AbstractChannel.doClose0(promise); NioSocketChannel.doClose() -> AbstractInterruptibleChannel.close() ->AbstractSelectableChannel.implCloseChannel() ->SocketChannelImpl.implCloseSelectableChannel ->SocketDispatcher.preClose(FileDescriptor fd)->native preClose0(关闭底层FD并复制已关闭的fd) ->AbstractSelectableChannel..cancel() ->AbstractSelector.cancel(SelectionKey k)
同select线性遍历,pollfd结构,没有最大文件描述符数量限制,原因是它是基于链表来存储
HeadContext.channelRead
事件监听回调,没有最大文件描述符数量限制,只监听“活跃”连接,使用mmap减少复制开销
channel
doStartThread()
AbstractNioMessageChannel
run()
监听触发加入一个doBind0任务
MultithreadEventExecutorGroup
OP_CONNECT=8
solaris\\classes\\sun\io\\ch\\DefaultSelectorProvidermacosx\\classes\\sun\io\\ch\\DefaultSelectorProviderwindows\\classes\\sun\io\\ch\\DefaultSelectorProvider
channelRead
WindowsSelectorImpl
监听FD
OP_WRITE=4
doReadMessages
OP_READ=1
打开选择器,openSelector
initAndRegister
返回fd攒着的NioSocketChannel SocketUtils.accept(javaChannel()) serverSocketChannel.accept()
register
AbstractScheduledEventExecutor
processSelectedKeys
初始化选择器chooser
NioEventLoop
AbstractChannelHandlerContext
doRegister注册到javaChannel
OP_ACCEPT=16
AbstractEventExecutor
初始化childHandler
触发boss initChannel
NioServerSocketChannel
register0
group
public static SelectorProvider create() { String osname = AccessController .doPrivileged(new GetPropertyAction(\"os.name\")); if (osname.equals(\"SunOS\")) return createProvider(\"sun.nio.ch.DevPollSelectorProvider\"); if (osname.equals(\"Linux\")) return createProvider(\"sun.nio.ch.EPollSelectorProvider\"); return new sun.nio.ch.PollSelectorProvider(); }
init
轮询FD,其实不活跃的Socket没有必要扫描,fd集合在用户态与内核态之间拷贝传递,费力
AbstractNioByteChannel.read() doReadBytes(byteBuf) pipeline.fireChannelRead pipeline.fireChannelReadComplete
pipeline.invokeHandlerAddedIfNeeded
将当前执行放入队列:ThreadExecutorMap.apply
poll
invokeChannelRead直接根据传入的head执行
ServerBootstrap.ServerBootstrapAcceptor
doBind0
DefaultChannelPromise.addListener
pipeline.fireChannelRegisteredd
runAllTasks
AbstractChannel
step2ServerBootstrap绑定过程
NioSocketChannel.doReadBytes
DefaultSelectStrategyFactory
step3ServerBootstrap客户端接入
0 条评论
下一页