netty源碼分析-注冊(cè)及連接

線程池都準(zhǔn)備好了,我們需要利用起來(lái)了。我們一客戶端的connect為例講述這個(gè)過(guò)程。下面是我們觸發(fā)了鏈接這個(gè)動(dòng)作

ChannelFuture f = b.connect(host, port).sync();

他里面是怎樣的邏輯呢?

private ChannelFuture doResolveAndConnect(final SocketAddress remoteAddress, final SocketAddress localAddress) {
    //利用反射創(chuàng)建channel類,并且初始化它
    final ChannelFuture regFuture = initAndRegister(); 
    ... 
    //真正的鏈接服務(wù)端
    return doResolveAndConnect0(channel, remoteAddress, localAddress, channel.newPromise()); 
    }
}

中間省略了好多代碼,只留下關(guān)鍵的代碼。首先我們回憶一下NIO的經(jīng)典操作。首先創(chuàng)建一個(gè)channel,然后在selector上注冊(cè),并指明感興趣的事件,隨后selector就select了,等待感興趣的事件到來(lái),事件到達(dá),處理請(qǐng)求。這是原生的NIO的處理過(guò)程,既然netty是基于nio的,頂多是幫助我們封裝了這些操作而已,讓我們可以更加舒服的利用netty的api處理網(wǎng)絡(luò)的請(qǐng)求。看看上面的注釋,基本上和我們的了解一致,至于是不是真的一致,那么久得繼續(xù)往下看了。

final ChannelFuture initAndRegister() {
    Channel channel = null;
    channel = channelFactory.newChannel();  //利用反射創(chuàng)建對(duì)象
    init(channel);  //初始化,添加邏輯處理器,設(shè)置channel的Option與屬性Attribute
    ...
    ChannelFuture regFuture = config().group().register(channel);  
    ...
    return regFuture;
}

利用反射創(chuàng)建了代碼中我們指定的channel,init初始化,添加邏輯處理器,設(shè)置channel的Option與屬性Attribute。我們更為關(guān)鍵的是看一下如果進(jìn)行注冊(cè)上篇文章也介紹了groupMultithreadEventLoopGroup的實(shí)例。

### io.netty.channel.MultithreadEventLoopGroup#register(io.netty.channel.Channel)
@Override
public ChannelFuture register(Channel channel) {
    return next().register(channel);
}

這個(gè)next方法就是我們的選擇器發(fā)揮作用了,選擇一個(gè)孩子來(lái)進(jìn)行處理(負(fù)載均衡的考慮)。具體的是NioEventLoop的事例進(jìn)行的register操作,他沒(méi)有復(fù)寫父類的方法,所以由父類SingleThreadEventLoop來(lái)具體處理

### io.netty.channel.SingleThreadEventLoop#register(io.netty.channel.Channel)
@Override
public ChannelFuture register(Channel channel) {
    return register(new DefaultChannelPromise(channel, this));
}

將channel包裝成了DefaultChannelPromise的對(duì)象進(jìn)行操作。

### io.netty.channel.AbstractChannel.AbstractUnsafe#register
@Override
public final void register(EventLoop eventLoop, final ChannelPromise promise) {
    ...  
    AbstractChannel.this.eventLoop = eventLoop;
    ...
    eventLoop.execute(new Runnable() {  //有具體的線程池進(jìn)行處理,參數(shù)傳遞過(guò)來(lái)的
                @Override
                public void run() {
                    register0(promise);
                }
            });
      ...
    }
}

老樣子,省略好多代碼,只留下重點(diǎn)。eventLoop是NioEnevtLoop的實(shí)例,所以看一下他的execute,同樣的他沒(méi)有復(fù)寫這個(gè)方法,所以還是由父類提供

### io.netty.util.concurrent.SingleThreadEventExecutor#execute
@Override
public void execute(Runnable task) {
        ...
        startThread();   //開啟線程
        addTask(task);   //處理請(qǐng)求
        ...  
}

### io.netty.util.concurrent.SingleThreadEventExecutor#startThread
private void startThread() {
   ...
            doStartThread();
    ...
}

private void doStartThread() {
    assert thread == null;
    executor.execute(new Runnable() {  //重點(diǎn)關(guān)注這個(gè)executor
        @Override
        public void run() {
            thread = Thread.currentThread();
            if (interrupted) {
                thread.interrupt();
            }

            boolean success = false;
            updateLastExecutionTime();
            try {
                SingleThreadEventExecutor.this.run();  //SingleThreadEventExecutor.this是NioEventLoop的事例
                success = true;
            } catch (Throwable t) {
                logger.warn("Unexpected exception from an event executor: ", t);
            } finally {
                for (;;) {
                    int oldState = state;
                    if (oldState >= ST_SHUTTING_DOWN || STATE_UPDATER.compareAndSet(
                            SingleThreadEventExecutor.this, oldState, ST_SHUTTING_DOWN)) {
                        break;
                    }
                }

                // Check if confirmShutdown() was called at the end of the loop.
                if (success && gracefulShutdownStartTime == 0) {
                    logger.error("Buggy " + EventExecutor.class.getSimpleName() + " implementation; " +
                            SingleThreadEventExecutor.class.getSimpleName() + ".confirmShutdown() must be called " +
                            "before run() implementation terminates.");
                }

                try {
                    // Run all remaining tasks and shutdown hooks.
                    for (;;) {
                        if (confirmShutdown()) {
                            break;
                        }
                    }
                } finally {
                    try {
                        cleanup();
                    } finally {
                        STATE_UPDATER.set(SingleThreadEventExecutor.this, ST_TERMINATED);
                        threadLock.release();
                        if (!taskQueue.isEmpty()) {
                            logger.warn(
                                    "An event executor terminated with " +
                                            "non-empty task queue (" + taskQueue.size() + ')');
                        }

                        terminationFuture.setSuccess(null);
                    }
                }
            }
        }
    });
}

這里有一個(gè)細(xì)節(jié)點(diǎn)不能忽略就是executor.execute,我們要知道這個(gè)executor是啥,再創(chuàng)建NioEventLoopGroup時(shí),有這樣的邏輯

### io.netty.util.concurrent.MultithreadEventExecutorGroup#MultithreadEventExecutorGroup(int, java.util.concurrent.Executor, io.netty.util.concurrent.EventExecutorChooserFactory, java.lang.Object...)
if (executor == null) {
    executor = new ThreadPerTaskExecutor(newDefaultThreadFactory());
}
 
protected ThreadFactory newDefaultThreadFactory() {
    return new DefaultThreadFactory(getClass());
}

### io.netty.util.concurrent.DefaultThreadFactory
@Override
public Thread newThread(Runnable r) {
    Thread t = newThread(new DefaultRunnableDecorator(r), prefix + nextId.incrementAndGet());
    try {
        if (t.isDaemon() != daemon) {
            t.setDaemon(daemon);
        }

        if (t.getPriority() != priority) {
            t.setPriority(priority);
        }
    } catch (Exception ignored) {
        // Doesn't matter even if failed to set.
    }
    return t;
}
 
private static final class DefaultRunnableDecorator implements Runnable {

    private final Runnable r;

    DefaultRunnableDecorator(Runnable r) {
        this.r = r;
    }

    @Override
    public void run() {
        try {
            r.run();
        } finally {
            FastThreadLocal.removeAll();
        }
    }
}

線程工廠創(chuàng)建線程的邏輯,線程池里面設(shè)置了線程工廠,那么線程池運(yùn)行多線程任務(wù)的時(shí)候,其實(shí)是利用線程工廠創(chuàng)建線程來(lái)運(yùn)行

### io.netty.util.concurrent.ThreadPerTaskExecutor
public final class ThreadPerTaskExecutor implements Executor {
    private final ThreadFactory threadFactory;

    public ThreadPerTaskExecutor(ThreadFactory threadFactory) {
        if (threadFactory == null) {
            throw new NullPointerException("threadFactory");
        }
        this.threadFactory = threadFactory;
    }

    @Override
    public void execute(Runnable command) {
        threadFactory.newThread(command).start();
    }
}

當(dāng)線程池有任務(wù)過(guò)來(lái)時(shí),會(huì)調(diào)用線程工廠創(chuàng)建線程,并且啟動(dòng)該線程來(lái)處理,我們看一下NioEventLoop的run方法


@Override
protected void run() {
    for (;;) {
         ...
         processSelectedKeys();   //處理Nio中的SelectedKeys   
         ...          
    }
}
### 
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) {  //如何是鏈接的請(qǐng)求,調(diào)用unsafe的finishConnect
            // 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
            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) {  
            unsafe.read();  //讀取數(shù)據(jù)
        }
    } catch (CancelledKeyException ignored) {
        unsafe.close(unsafe.voidPromise());
    }
}

好像終于和我們的NIO有點(diǎn)聯(lián)系了。無(wú)非也就是等感興趣的事件來(lái)了就處理,調(diào)用unsafe來(lái)處理,首先我們說(shuō)一下unsafe,他是NioSocketChannelUnsafe的事例,而這個(gè)類繼承了NioByteUnsafe,并且大部分的方法都是在NioByteUnSafe,我們比較關(guān)心她的讀取數(shù)據(jù)的過(guò)程

@Override
    public final void read() {
        final ChannelConfig config = config();
        final ChannelPipeline pipeline = pipeline();
        final ByteBufAllocator allocator = config.getAllocator();
        final RecvByteBufAllocator.Handle allocHandle = recvBufAllocHandle();
        allocHandle.reset(config);

        ByteBuf byteBuf = null;
        boolean close = false;
        try {
            do {
                byteBuf = allocHandle.allocate(allocator);
                allocHandle.lastBytesRead(doReadBytes(byteBuf));
                if (allocHandle.lastBytesRead() <= 0) {
                    // nothing was read. release the buffer.
                    byteBuf.release();
                    byteBuf = null;
                    close = allocHandle.lastBytesRead() < 0;
                    break;
                }

                allocHandle.incMessagesRead(1);
                readPending = false;
                pipeline.fireChannelRead(byteBuf);     //觸發(fā)pipeline的生命周期方法,接收消息,處理消息
                byteBuf = null;
            } while (allocHandle.continueReading());

            allocHandle.readComplete();
            pipeline.fireChannelReadComplete();

            if (close) {
                closeOnRead(pipeline);
            }
        } catch (Throwable t) {
            handleReadException(pipeline, byteBuf, t, close, allocHandle);
        } finally {
            // Check if there is a readPending which was not processed yet.
            // This could be for two reasons:
            // * The user called Channel.read() or ChannelHandlerContext.read() in channelRead(...) method
            // * The user called Channel.read() or ChannelHandlerContext.read() in channelReadComplete(...) method
            //
            // See https://github.com/netty/netty/issues/2254
            if (!readPending && !config.isAutoRead()) {
                removeReadOp();
            }
        }
    }
}

調(diào)用pipeline的生命周期方,同時(shí)將數(shù)據(jù)傳遞過(guò)去,handler開始處理了。以上皆是處理了SelectionKey的過(guò)程。注冊(cè)搞好了,我們就可以開始連接。在我們追蹤下來(lái),connect核心的代碼

doConnect(remoteAddress, localAddress)

### io.netty.channel.socket.nio.NioSocketChannel#doConnect
@Override
protected boolean doConnect(SocketAddress remoteAddress, SocketAddress localAddress) throws Exception {
    if (localAddress != null) {
        doBind0(localAddress);
    }

    boolean success = false;
    try {
        boolean connected = SocketUtils.connect(javaChannel(), remoteAddress);
        if (!connected) {
            selectionKey().interestOps(SelectionKey.OP_CONNECT);
        }
        success = true;
        return connected;
    } finally {
        if (!success) {
            doClose();
        }
    }
}

socket 鏈接遠(yuǎn)程服務(wù)器,因?yàn)槭钱惒芥溄樱詂onnected為false,那么就注冊(cè)了OP_CONNECT事件,這樣,當(dāng)連接事件做好之后,在線程組中會(huì)有無(wú)限循環(huán),查詢準(zhǔn)備好的事件,連接事件好了,就會(huì)進(jìn)行處理,同時(shí)觸發(fā)聲明周期的方法,進(jìn)行流程的流轉(zhuǎn)。
以上。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 230,002評(píng)論 6 542
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 99,400評(píng)論 3 429
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人,你說(shuō)我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 178,136評(píng)論 0 383
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我,道長(zhǎng),這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,714評(píng)論 1 317
  • 正文 為了忘掉前任,我火速辦了婚禮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 72,452評(píng)論 6 412
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 55,818評(píng)論 1 328
  • 那天,我揣著相機(jī)與錄音,去河邊找鬼。 笑死,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,812評(píng)論 3 446
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 42,997評(píng)論 0 290
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 49,552評(píng)論 1 335
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 41,292評(píng)論 3 358
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 43,510評(píng)論 1 374
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 39,035評(píng)論 5 363
  • 正文 年R本政府宣布,位于F島的核電站,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 44,721評(píng)論 3 348
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 35,121評(píng)論 0 28
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 36,429評(píng)論 1 294
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 52,235評(píng)論 3 398
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 48,480評(píng)論 2 379

推薦閱讀更多精彩內(nèi)容