Netty

1、Reactor模型

    1、单线程Reactor

    2、线程池Reactor

          1、一个线程专门处理accecpt(),该线程还处理SSL握手等,因为该线程的压力很大,回是瓶颈。

     3、主从Reactor

          1、主reactor用于accept(包含SSL握手和验证,登录等)

          2、从reactor

              用于处理其他事件

2、线程模型:

    netty的handler处理是在IO线程里运行的。如果自己的业务很小,可以不在handler里面使用自己的线程池,如果自己的业务很费时间,需要使用自己的线程池进行异步处理。

3、Netty的启动,写API都是异步的。

4、 ctx.write(msg);也是异步的,内部实现可能是将bytebuffer放进netty自己的缓冲队列里,而没有提交给tcp缓冲区。

      ctx.writeAndFlush(time);

     这个方法会走到下面的代码:可以看出Netty对于NIO写的处理是只循环写16次,如果还没写完则不继续写了。使用NIO的写逻辑都应该是这样不断的循环写吧。

                                          IO写应该先在自己的缓冲区缓冲足够的数据,然后再调操作系统API写(一方面调操作系统API需要不断在内核态和用户态切换,另一方面对介质的写操作应该是一次提交大量的数据,而不是小量的                                            数据,这能提高效率)。

protected void doWrite(ChannelOutboundBuffer in) throws Exception {
        for (;;) {
            int size = in.size();
            if (size == 0) {
                // All written so clear OP_WRITE
                clearOpWrite();
                break;
            }
            long writtenBytes = 0;
            boolean done = false;
            boolean setOpWrite = false;

            // Ensure the pending writes are made of ByteBufs only.
            ByteBuffer[] nioBuffers = in.nioBuffers();
            int nioBufferCnt = in.nioBufferCount();
            long expectedWrittenBytes = in.nioBufferSize();
            SocketChannel ch = javaChannel();

            // Always us nioBuffers() to workaround data-corruption.
            // See https://github.com/netty/netty/issues/2761
            switch (nioBufferCnt) {
                case 0:
                    // We have something else beside ByteBuffers to write so fallback to normal writes.
                    super.doWrite(in);
                    return;
                case 1:
                    // Only one ByteBuf so use non-gathering write
                    ByteBuffer nioBuffer = nioBuffers[0];
//因为采取的是NIO,所以一次写可能写不完。默认是循环写16次,如果还没写完,则返回。
for (int i = config().getWriteSpinCount() - 1; i >= 0; i --) { final int localWrittenBytes = ch.write(nioBuffer); if (localWrittenBytes == 0) { setOpWrite = true; break; } expectedWrittenBytes -= localWrittenBytes; writtenBytes += localWrittenBytes; if (expectedWrittenBytes == 0) { done = true; break; } } break; default: for (int i = config().getWriteSpinCount() - 1; i >= 0; i --) { final long localWrittenBytes = ch.write(nioBuffers, 0, nioBufferCnt); if (localWrittenBytes == 0) { setOpWrite = true; break; } expectedWrittenBytes -= localWrittenBytes; writtenBytes += localWrittenBytes; if (expectedWrittenBytes == 0) { done = true; break; } } break; } // Release the fully written buffers, and update the indexes of the partially written buffer. in.removeBytes(writtenBytes); if (!done) { // Did not write all buffers completely. incompleteWrite(setOpWrite); break; } } }

 5、netty的解码器在处理数据时可能会缓冲很多的数据,直到复合一个报文要求才会将缓冲的数据提交给下一个handler。

原文地址:https://www.cnblogs.com/YDDMAX/p/5414598.html