欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Netty事件循環(huán)主邏輯NioEventLoop的run方法分析

 更新時間:2022年03月24日 18:24:13   作者:liron  
這篇文章主要介紹了Netty事件循環(huán)主邏輯NioEventLoop的run方法分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪

Netty事件循環(huán)主邏輯

Netty 事件循環(huán)主邏輯在 NioEventLoop.run 中的 processSelectedKeys函數(shù)中

protected void run() {
      //主循環(huán)不斷讀取IO事件和task,因為 EventLoop 也是 juc 的 ScheduledExecutorService 實現(xiàn)
        for (;;) {
            try {
                switch (selectStrategy.calculateStrategy(selectNowSupplier, hasTasks())) {
                    case SelectStrategy.CONTINUE:
                        continue;
                    case SelectStrategy.SELECT:
                        select(wakenUp.getAndSet(false));

                        

                        if (wakenUp.get()) {
                            selector.wakeup();
                        }
                        // fall through
                    default:
                }

                cancelledKeys = 0;
                needsToSelectAgain = false;
            // IO事件占總執(zhí)行時間的百分比 */
                final int ioRatio = this.ioRatio;
                if (ioRatio == 100) {
                    try {
                        processSelectedKeys();
                    } finally {
                        // Ensure we always run tasks.
                        runAllTasks();
                    }
                } else {
                    final long ioStartTime = System.nanoTime();
                    try {
                        processSelectedKeys();
                    } finally {
                        // Ensure we always run tasks.
                        final long ioTime = System.nanoTime() - ioStartTime;
                        runAllTasks(ioTime * (100 - ioRatio) / ioRatio);
                    }
                }
            } catch (Throwable t) {
                handleLoopException(t);
            }
            // Always handle shutdown even if the loop processing threw an exception.
            try {
                if (isShuttingDown()) {
                    closeAll();
                    if (confirmShutdown()) {
                        return;
                    }
                }
            } catch (Throwable t) {
                handleLoopException(t);
            }
        }
    }

processSelectedKeys 函數(shù) 執(zhí)行時會判斷是否執(zhí)行優(yōu)化的版本,即判斷 SelectedSelectionKeySet 是否為空。

是否開啟優(yōu)化取決于是否設(shè)置了環(huán)境變量  io.netty.noKeySetOptimization ,默認是 false 代表開啟

private static final boolean DISABLE_KEYSET_OPTIMIZATION =
            SystemPropertyUtil.getBoolean("io.netty.noKeySetOptimization", false);

原理是通過反射的方式設(shè)置 eventLoop綁定的selector中的 selectKeys屬性 為 SelectedSelectionKeySet ,好處是不用 迭代  selector.selectedKeys()

初始化 EventLoop

注入時機為初始化 EventLoop 的時候

private SelectorTuple openSelector() {
        12      //注入邏輯40 
        Object maybeException = AccessController.doPrivileged(new PrivilegedAction<Object>() {
            @Override
            public Object run() {
                try {
                    Field selectedKeysField = selectorImplClass.getDeclaredField("selectedKeys");
                    Field publicSelectedKeysField = selectorImplClass.getDeclaredField("publicSelectedKeys");

                    Throwable cause = ReflectionUtil.trySetAccessible(selectedKeysField);
                    if (cause != null) {
                        return cause;
                    }
                    cause = ReflectionUtil.trySetAccessible(publicSelectedKeysField);
                    if (cause != null) {
                        return cause;
                    }

                    selectedKeysField.set(unwrappedSelector, selectedKeySet);
                    publicSelectedKeysField.set(unwrappedSelector, selectedKeySet);
                    return null;
                } catch (NoSuchFieldException e) {
                    return e;
                } catch (IllegalAccessException e) {
                    return e;
                }
            }
        });

        ........78     }

處理讀事件

處理讀事件主要在processSelectedKey 中 ,分別對 讀、寫、連接事件進行了處理。

private void processSelectedKeysOptimized() {
        for (int i = 0; i < selectedKeys.size; ++i) {
            final SelectionKey k = selectedKeys.keys[i];
            // null out entry in the array to allow to have it GC'ed once the Channel close
            // See https://github.com/netty/netty/issues/2363
            selectedKeys.keys[i] = null;
            final Object a = k.attachment();
            if (a instanceof AbstractNioChannel) {
                //分別處理每個channel的事件
                processSelectedKey(k, (AbstractNioChannel) a);
            } else {
                @SuppressWarnings("unchecked")
                NioTask<SelectableChannel> task = (NioTask<SelectableChannel>) a;
                processSelectedKey(k, task);
            }
            if (needsToSelectAgain) {
                // null out entries in the array to allow to have it GC'ed once the Channel close
                // See https://github.com/netty/netty/issues/2363
                selectedKeys.reset(i + 1);
                selectAgain();
                i = -1;
            }
        }
    }
    private void processSelectedKey(SelectionKey k, AbstractNioChannel ch) {
        final AbstractNioChannel.NioUnsafe unsafe = ch.unsafe();
        if (!k.isValid()) {
            final EventLoop eventLoop;
            try {
                eventLoop = ch.eventLoop();
            } catch (Throwable ignored) {
                // If the channel implementation throws an exception because there is no event loop, we ignore this
                // because we are only trying to determine if ch is registered to this event loop and thus has authority
                // to close ch.
                return;
            }
            // Only close ch if ch is still registered to this EventLoop. ch could have deregistered from the event loop
            // and thus the SelectionKey could be cancelled as part of the deregistration process, but the channel is
            // still healthy and should not be closed.
            // See https://github.com/netty/netty/issues/5125
            if (eventLoop != this || eventLoop == null) {
                return;
            }
            // close the channel if the key is not valid anymore
            unsafe.close(unsafe.voidPromise());
            return;
        }
        try {
            int readyOps = k.readyOps();
            // We first need to call finishConnect() before try to trigger a read(...) or write(...) as otherwise
            // the NIO JDK channel implementation may throw a NotYetConnectedException.
            if ((readyOps & SelectionKey.OP_CONNECT) != 0) {
                // remove OP_CONNECT as otherwise Selector.select(..) will always return without blocking
                // See https://github.com/netty/netty/issues/924
                int ops = k.interestOps();
                ops &= ~SelectionKey.OP_CONNECT;
                k.interestOps(ops);
          //處理了連接事件
                unsafe.finishConnect();
            }
            // Process OP_WRITE first as we may be able to write some queued buffers and so free memory.
            if ((readyOps & SelectionKey.OP_WRITE) != 0) {
                // Call forceFlush which will also take care of clear the OP_WRITE once there is nothing left to write
 //將要寫入的buffer flush掉
          ch.unsafe().forceFlush();
            }

            // Also check for readOps of 0 to workaround possible JDK bug which may otherwise lead
            // to a spin loop
            if ((readyOps & (SelectionKey.OP_READ | SelectionKey.OP_ACCEPT)) != 0 || readyOps == 0) {
           //回調(diào) pipeline 上所有的 ChannelInboundHandler 的 fireChannelRead  和 channelReadComplete 函數(shù)
                unsafe.read();
            }
        } catch (CancelledKeyException ignored) {
            unsafe.close(unsafe.voidPromise());
        }
    }

注意 

NioServerSocketChannel 和 NioSocketChannel 都是 同樣的 處理邏輯, 不同的是  前者 只關(guān)注 OP_ACCEPT 和 OP_READ事件, 后者 關(guān)注  OP_READ、OP_WRITE、OP_CONNECT事件

當(dāng)NioServerSocketChannel 發(fā)生 OP_ACCEPT事件時 會 觸發(fā)

 AbstractNioChannel.NioUnsafe.read ->  NioSctpServerChannel.doReadMessages(List<Object>)  -> ServerBootstrapAcceptor.channelRead ,

將受到的 NioSocketChannel 注冊到 childEventLoop 。

以上就是Netty事件循環(huán)主邏輯NioEventLoop的run方法分析的詳細內(nèi)容,更多關(guān)于Netty循環(huán)邏輯NioEventLoop run方法的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • 使用Spring?Retry實現(xiàn)業(yè)務(wù)異常重試

    使用Spring?Retry實現(xiàn)業(yè)務(wù)異常重試

    在系統(tǒng)中經(jīng)常遇到業(yè)務(wù)重試的邏輯,比如三方接口調(diào)用,timeout重試三遍,異常處理重試的兜底邏輯等,本文給大家介紹一下如何使用Spring?Retry優(yōu)雅的實現(xiàn)業(yè)務(wù)異常重試,需要的朋友可以參考下
    2024-01-01
  • SpringBoot中使用Redis的完整實例

    SpringBoot中使用Redis的完整實例

    這篇文章主要給大家介紹了關(guān)于SpringBoot中使用Redis的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-09-09
  • Maven中optional標(biāo)簽用法詳解

    Maven中optional標(biāo)簽用法詳解

    這篇文章主要介紹了Maven中optional標(biāo)簽,文章中有詳細的代碼示例供大家參考,對大家的學(xué)習(xí)或工作有一定的參考價值,感興趣的小伙伴可以借鑒一下
    2023-05-05
  • java中form以post、get方式提交數(shù)據(jù)中文亂碼問題總結(jié)

    java中form以post、get方式提交數(shù)據(jù)中文亂碼問題總結(jié)

    這篇文章主要介紹了java中form以post、get方式提交數(shù)據(jù)中文亂碼問題總結(jié),需要的朋友可以參考下
    2014-10-10
  • spring boot Logging的配置以及使用詳解

    spring boot Logging的配置以及使用詳解

    這篇文章主要介紹了spring boot Logging的配置以及使用詳解,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-01-01
  • 一篇文章帶你了解Java SpringBoot Nacos

    一篇文章帶你了解Java SpringBoot Nacos

    這篇文章主要介紹了SpringBoot使用Nacos配置中心的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-09-09
  • Java之String[] args含義及作用

    Java之String[] args含義及作用

    這篇文章主要介紹了Java之String[] args含義及作用,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下
    2021-08-08
  • SpringBoot調(diào)用對方webService接口的幾種方法示例

    SpringBoot調(diào)用對方webService接口的幾種方法示例

    平常我們開發(fā)調(diào)用接口一般會用到幾種數(shù)據(jù)格式,比如有restful的,這個是目前最流行的,也是最簡單開發(fā)的,還有一種就是webservice數(shù)據(jù)格式,本文給大家介紹了幾種SpringBoot調(diào)用對方webService接口的方法,文中有相關(guān)的代碼示例供大家參考,需要的朋友可以參考下
    2023-11-11
  • 詳解Java停止線程的四種方法

    詳解Java停止線程的四種方法

    如何停止java的線程一直是一個困惱我們開發(fā)多線程程序的一個問題嗎,本文主要介紹了詳解Java停止線程的四種方法,感興趣的可以了解一下
    2021-05-05
  • 詳解Spring注解@Validated 失效分析

    詳解Spring注解@Validated 失效分析

    這篇文章主要介紹了Spring注解@Validated失效分析,文中有詳細代碼示例講解了@Validated為何失效,需要的小伙伴可以參考一下
    2023-04-04

最新評論