-
Notifications
You must be signed in to change notification settings - Fork 26.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feat/netty4 #630
Feat/netty4 #630
Conversation
…ObjectBuilder 存在死循环的风险
|
||
private static final Logger logger = LoggerFactory.getLogger(NettyClient.class); | ||
|
||
private static final NioEventLoopGroup nioEventLoopGroup = new NioEventLoopGroup(Constants.DEFAULT_IO_THREADS, new NamedThreadFactory("NettyClientWorker", true)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里的ThreadFactory最好使用netty4自己的DefaultThreadFactory,它里面覆盖了thread local,有些特殊的优化
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
嗯的,确实忘记了 fastThreadlocal 这个东西,感谢提醒
} | ||
} | ||
|
||
private class InternalDecoder extends ByteToMessageDecoder { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个InternalDecoder稍微写复杂了,很多逻辑ByteToMessageDecoder里已经实现了,基本上向下面这样就ok了
`private class InternalDecoder extends ByteToMessageDecoder {
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf input, List<Object> out) throws Exception {
NettyChannel channel = NettyChannel.getOrAddChannel(ctx.channel(), url, handler);
try {
Object msg = codec.decode(channel, new NettyBackedChannelBuffer(input));
if (msg == Codec.NEED_MORE_INPUT) {
return;
}
if (msg != null)
out.add(msg);
} catch (IOException ex) {
throw ex;
} finally {
NettyChannel.removeChannelIfDisconnected(ctx.channel());
}
}
}`
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
saveReaderIndex 还是有必要的,要不然 Codec.NEED_MORE_INPUT 的情况, 可能需要回退重新读取;虽然 dubbo 的 codec 是有做了这个动作,但是其他 codec 没有做这个操作。。。考虑了一下,可能在这里做会更好一些
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
我也认为这个saveReaderIndex有必要咯。目前master上基于netty3的做法是会保存一个remaingBuffer(保存不完整的帧),每次会将input的ChannelBuffer中的所有bytes写入到remaingBuffer中然后dubbo codec从remaingBuffer中读取,这样做的目的是为啥呢?这次升4直接利用ByteBuf应该也不会有问题吧
bootstrap = new ServerBootstrap(); | ||
|
||
bossGroup = new NioEventLoopGroup(0, new NamedThreadFactory("NettyServerBoss", true)); | ||
workerGroup = new NioEventLoopGroup(Constants.DEFAULT_IO_THREADS, new NamedThreadFactory("NettyServerWorker", true)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个Constants.DEFAULT_IO_THREADS是根据cpu核数取值,这个在docker里有坑,使用cpu quota这种没有什么好办法设置CPU核数,最后取的都是宿主的核数,导致启动了很多netty线程。应该设置个max值
Match.min(cpu cores, 8) 类似这样的。
Netty 3->4 可以怎么参与进来呢? |
- 使用 netty 4 fastThreadLocal 线程工厂 - 优化 worker 的默认值 - 去掉一些无用代码
netty4 support
看最新版本的 |
Netty 4 通信模块