Java 杂谈深入浅出Netty源码剖析

【第10篇】Netty自适应缓冲区分配策略源码分析

2019-05-25  本文已影响0人  爱学习的蹭蹭

Netty底层的策略

AdaptiveRecvByteBufAllocator

AdaptiveRecvByteBufAllocator关系图
 //静态代码块的作用是对SIZE_TABLE数组填写1~38的坐标的值是16,32,48....一直到65536
    //自动减少或者增加的幅度就是来自于这个数组。具体逻辑在HandleImpl对的record方法。
    static {
        List<Integer> sizeTable = new ArrayList<Integer>();
        for (int i = 16; i < 512; i += 16) {
            sizeTable.add(i);//1~16的设置是16到(512-16)
        }

        for (int i = 512; i > 0; i <<= 1) {
            sizeTable.add(i);//从512到65536
        }

        SIZE_TABLE = new int[sizeTable.size()];
        for (int i = 0; i < SIZE_TABLE.length; i ++) {
            SIZE_TABLE[i] = sizeTable.get(i);//填写到SIZE_TABLE数组
        }
    }

//句柄实现
private final class HandleImpl extends MaxMessageHandle {
        private final int minIndex;//最小索引
        private final int maxIndex;//最大索引
        private int index;//索引
        private int nextReceiveBufferSize;//下一个接收缓存区的大小
        private boolean decreaseNow;//减少的标识

        public HandleImpl(int minIndex, int maxIndex, int initial) {
            this.minIndex = minIndex;
            this.maxIndex = maxIndex;
            index = getSizeTableIndex(initial); //获取表里面的索引大小
            nextReceiveBufferSize = SIZE_TABLE[index];
        }
        //预估处理接收值
        @Override
        public int guess() {
            return nextReceiveBufferSize;
        }
        //处理记录实际的字节
        private void record(int actualReadBytes) {
            //判断实际读取字节与最大索引值
            if (actualReadBytes <= SIZE_TABLE[Math.max(0, index - INDEX_DECREMENT - 1)]) {
                if (decreaseNow) {
                    //获取最大索引
                    index = Math.max(index - INDEX_DECREMENT, minIndex);
                    nextReceiveBufferSize = SIZE_TABLE[index];//获取下一个接收缓存大小
                    decreaseNow = false;
                } else {
                    decreaseNow = true;
                }
            } else if (actualReadBytes >= nextReceiveBufferSize) {
               //获取最小索引
                index = Math.min(index + INDEX_INCREMENT, maxIndex);
                nextReceiveBufferSize = SIZE_TABLE[index];
                decreaseNow = false;
            }
        }

        //完成读写记录
        @Override
        public void readComplete() {
            record(totalBytesRead());
        }
    }
HandleImpl
 public abstract class MaxMessageHandle implements ExtendedHandle {
        private ChannelConfig config;//管道配置变量
        private int maxMessagePerRead;//最大预期消息读变量
        private int totalMessages;//总消息变量
        private int totalBytesRead;//总读的总字节变量
        private int attemptedBytesRead;//尝试去读的字节变量
        private int lastBytesRead;//最后字节读变量
        //不检测提供者尝试读取的字节
        private final UncheckedBooleanSupplier defaultMaybeMoreSupplier = new UncheckedBooleanSupplier() {
            @Override
            public boolean get() {
                return attemptedBytesRead == lastBytesRead;
            }
        };

        /**
         *  重置ChannelConfig 管道配置
         */
        @Override
        public void reset(ChannelConfig config) {
            this.config = config;
            maxMessagePerRead = maxMessagesPerRead();
            totalMessages = totalBytesRead = 0;
        }

        //分配字节缓存器处理,此类比较重要
        @Override
        public ByteBuf allocate(ByteBufAllocator alloc) {
            return alloc.ioBuffer(guess());
        }
 
       tedBytesRead = bytes;
        }
         //分配字节读
        protected final int totalBytesRead() {
            return totalBytesRead < 0 ? Integer.MAX_VALUE : totalBytesRead;
        }
    }
ByteBuf

ByteBufAllocator展开里面的方法,可以看出来非常重要的一个类,里面包含ioBuffer(IO缓冲区),directBuffer(直接缓冲区),heapBuffer(堆缓冲区),compositeBuffer(复合缓冲区),compositeDirectBuffer(复合直接缓冲区)compositeHeapBuffer(复合堆缓冲区),然后在展开的类的方法有一个calculateNewCapacity方法,此方法尤为重要,计算ByteBuf的新容量,当ByteBuf需要以maxCapacity为上限扩展minNewCapacity时使用该容量。

AbstractByteBufAllocator

PooledByteBufAllocator和UnpooledByteBufAllocator关系图

PooledByteBufAllocator UnpooledByteBufAllocator

运算符

线程

值传递和引用传递

上一篇下一篇

猜你喜欢

热点阅读