Tomcat 源碼分析 (二) : Connector

NIOEndPoint

NIOEndPointbind()方法開啟一個(gè)SocketServer

    @Override
    public void bind() throws Exception {
         //開啟一個(gè)server socket
        serverSock = ServerSocketChannel.open();
        //根據(jù)配置文件設(shè)置server socket的屬性
        socketProperties.setProperties(serverSock.socket());
        InetSocketAddress addr = (getAddress()!=null?new InetSocketAddress(getAddress(),getPort()):new InetSocketAddress(getPort()));
        serverSock.socket().bind(addr,getAcceptCount());
        serverSock.configureBlocking(true); //mimic APR behavior

        // Initialize thread count defaults for acceptor, poller
        if (acceptorThreadCount == 0) {
            // FIXME: Doesn't seem to work that well with multiple accept threads
            // 這個(gè)東西和下面的配置有關(guān)
            acceptorThreadCount = 1;
        }
        if (pollerThreadCount <= 0) {
            //minimum one poller thread
            pollerThreadCount = 1;
        }
        stopLatch = new CountDownLatch(pollerThreadCount);

        // 如果需要的話,初始化SSL
        initialiseSsl();
        //開啟Selector,堅(jiān)挺NIO的 IO的事件
        selectorPool.open();
    }

·

Acceptor線程接收客戶請(qǐng)求

在Tomcat啟動(dòng)的時(shí)候會(huì)啟動(dòng)一個(gè)Endpoint,并會(huì)調(diào)用它的startInternal方法,在這里開啟了一個(gè)Acceptor的子線程。利用這個(gè)Acceptor子線程來接收Client端的Socket連接

   @Override
    public void run() {
            ........................省略代碼................................................
           // 這里調(diào)用上面NIOEndPoint 的serverSocketChannel 來接收一個(gè)客戶端發(fā)送來的socket
           socket = endpoint.serverSocketAccept();
           ........................省略代碼................................................
          // setSocketOptions() will hand the socket off to
         // an appropriate processor if successful
         if (!endpoint.setSocketOptions(socket)) {
                //及時(shí)關(guān)閉Socket連接
               endpoint.closeSocket(socket);  
          }
    }

從Tomcat8 以后,Tomcat的默認(rèn)連接為NIO。所以在這里的EndPoint的具體實(shí)現(xiàn)是NioEndPoint

    @Override
    protected boolean setSocketOptions(SocketChannel socket) {
        // Process the connection
        try {
            //disable blocking, APR style, we are gonna be polling it
            socket.configureBlocking(false);
            Socket sock = socket.socket();
            socketProperties.setProperties(sock);

            NioChannel channel = nioChannels.pop();
            if (channel == null) {
                SocketBufferHandler bufhandler = new SocketBufferHandler(
                        socketProperties.getAppReadBufSize(),
                        socketProperties.getAppWriteBufSize(),
                        socketProperties.getDirectBuffer());
                if (isSSLEnabled()) {
                    channel = new SecureNioChannel(socket, bufhandler, selectorPool, this);
                } else {
                    // NIOChannel 實(shí)質(zhì)上對(duì)ByteChannel 的一個(gè)封裝實(shí)現(xiàn)
                    channel = new NioChannel(socket, bufhandler);
                }
            } else {
                // 根據(jù)Socket,對(duì)設(shè)置當(dāng)前的NioChannel
                channel.setIOChannel(socket);
                channel.reset();
            }
            getPoller0().register(channel);
        } catch (Throwable t) {
            ExceptionUtils.handleThrowable(t);
            try {
                log.error("",t);
            } catch (Throwable tt) {
                ExceptionUtils.handleThrowable(tt);
            }
            // Tell to close the socket
            return false;
        }
        return true;
    }

通過閱讀代碼可以看出其處理過程如下:

  1. 設(shè)置非阻塞,以及其他的一些參數(shù)如SoTimeout、ReceiveBufferSize、SendBufferSize

  2. 然后將SocketChannel封裝成一個(gè)NioChannel,封裝過程使用了緩存,即避免了重復(fù)創(chuàng)建NioChannel,封裝過程中使用了緩存,既避免了重復(fù)創(chuàng)建NioChannel對(duì)象,直接利用原有的NIOChannel,并將NioChannel中的數(shù)據(jù)全部清空。

  3. 選擇一個(gè)Poller進(jìn)行注冊(cè)

再來看一下Poller

  public void register(final NioChannel socket) {
            socket.setPoller(this);
            NioSocketWrapper ka = new NioSocketWrapper(socket, NioEndpoint.this);
            socket.setSocketWrapper(ka);
            ka.setPoller(this);
            ka.setReadTimeout(getConnectionTimeout());
            ka.setWriteTimeout(getConnectionTimeout());
            ka.setKeepAliveLeft(NioEndpoint.this.getMaxKeepAliveRequests());
            ka.setSecure(isSSLEnabled());
            PollerEvent r = eventCache.pop();
            ka.interestOps(SelectionKey.OP_READ);//this is what OP_REGISTER turns into.
            if ( r==null) r = new PollerEvent(socket,ka,OP_REGISTER);
            else r.reset(socket,ka,OP_REGISTER); //
            addEvent(r); //將event 加入到事件處理隊(duì)列中
        }

這里又是進(jìn)行一些參數(shù)包裝,將socket和Poller的關(guān)系綁定,再次從緩存中取出或者重新構(gòu)建一個(gè)PollerEvent,然后將該event放到Poller的事件隊(duì)列中等待被異步處理

再來看看對(duì)于被加入到隊(duì)列中的events的處理

        public boolean events() {
            boolean result = false;

            PollerEvent pe = null;
            while ( (pe = events.poll()) != null ) {
                result = true;
                try {
                    pe.run(); //開始處理這個(gè)pollerEvent
                    pe.reset(); //
                    if (running && !paused) {
                        eventCache.push(pe);
                    }
                } catch ( Throwable x ) {
                    log.error("",x);
                }
            }

            return result;
        }

Poller的run 方法會(huì)對(duì)

   @Override
        public void run() {
.........................省略代碼................................

                // 遍歷并處理 已經(jīng)準(zhǔn)備好的NIO 事件
                while (iterator != null && iterator.hasNext()) {
                    SelectionKey sk = iterator.next();
                    NioSocketWrapper attachment = (NioSocketWrapper)sk.attachment();

                    if (attachment == null) {
                        iterator.remove();
                    } else {
                        iterator.remove();
                        //將已經(jīng)準(zhǔn)備好的IO事件和 綁定的Socket進(jìn)行分類處理
                        processKey(sk, attachment);
                    }
                }//while
  ............................省略代碼.............
}
     protected void processKey(SelectionKey sk, NioSocketWrapper attachment) {
            try {
                if ( close ) {
                    cancelledKey(sk);
                } else if ( sk.isValid() && attachment != null ) {
                    if (sk.isReadable() || sk.isWritable() ) {
                        if ( attachment.getSendfileData() != null ) {
                            processSendfile(sk,attachment, false);
                        } else {
                            //為了避免多線程對(duì)于attach的socketChannel處理的沖突,在這里將socketChannel的
                            // ready 操作位取反
                            unreg(sk, attachment, sk.readyOps());
                            boolean closeSocket = false;
                            // 如果SelectorKey 為Read,那就去處理Read
                            if (sk.isReadable()) {
                                if (!processSocket(attachment, SocketEvent.OPEN_READ, true)) {
                                    closeSocket = true;
                                }
                            }
                            // 如果SelectorKey 為Read,那就去處理Write
                            if (!closeSocket && sk.isWritable()) {
                                if (!processSocket(attachment, SocketEvent.OPEN_WRITE, true)) {
                                    closeSocket = true;
                                }
                            }
                            if (closeSocket) {
                                // 將attach的socketChannel從key中解綁
                                cancelledKey(sk);
                            }
                        }
                    }
                } else {
                    //invalid key
                    cancelledKey(sk);
                }
            } catch ( CancelledKeyException ckx ) {
                cancelledKey(sk);
            } catch (Throwable t) {
                ExceptionUtils.handleThrowable(t);
                log.error("",t);
            }
        }
   /**
     * Process the given SocketWrapper with the given status. Used to trigger
     * processing as if the Poller (for those endpoints that have one)
     * selected the socket.
     *
     * @param socketWrapper The socket wrapper to process
     * @param event         The socket event to be processed
     * @param dispatch      Should the processing be performed on a new
     *                          container thread
     *
     * @return if processing was triggered successfully
     */
    public boolean processSocket(SocketWrapperBase<S> socketWrapper,
            SocketEvent event, boolean dispatch) {
        try {
            if (socketWrapper == null) {
                return false;
            }
            SocketProcessorBase<S> sc = processorCache.pop();
            if (sc == null) {
                
                sc = createSocketProcessor(socketWrapper, event);
            } else {
                sc.reset(socketWrapper, event);
            }
            Executor executor = getExecutor();
            if (dispatch && executor != null) {
              //將SocketProcessorBase交給線程池中的線程來處理
                executor.execute(sc);
            } else {
                sc.run();
            }
        } catch (RejectedExecutionException ree) {
            getLog().warn(sm.getString("endpoint.executor.fail", socketWrapper) , ree);
            return false;
        } catch (Throwable t) {
            ExceptionUtils.handleThrowable(t);
            // This means we got an OOM or similar creating a thread, or that
            // the pool and its queue are full
            getLog().error(sm.getString("endpoint.process.fail"), t);
            return false;
        }
        return true;
    }

線程池中的線程即為 NioEndPonint$SocketProcessor 內(nèi)部類來看一下其內(nèi)部的doRun()方法

doRun{
  .....................
    //如果握手已經(jīng)完成....
    if (handshake == 0) {
                    SocketState state = SocketState.OPEN;
                    // Process the request from this socket
                    if (event == null) {
                        state = getHandler().process(socketWrapper, SocketEvent.OPEN_READ);
                    } else {
                        // 這里是關(guān)鍵
                        state = getHandler().process(socketWrapper, event);
                    }
                    if (state == SocketState.CLOSED) {
                        close(socket, key);
                    }
                }
    ....................
}

從上面代碼中可以看出,注意它調(diào)用了hanler.process(socket)來生成響應(yīng)數(shù)據(jù)。并且根據(jù)處理之后的返回狀態(tài)來決定是否關(guān)閉Socket連接

對(duì)于handler.process(socket)的處理。

NioEndpoint類中的Handler接口的具體實(shí)現(xiàn)是靜態(tài)類 AbstractProtocol$ConnectionHandler<S>通過查看其process(socket)方法.這個(gè)方法邏輯很長(zhǎng)并且很復(fù)雜。說多了也沒用。
它在這個(gè)方法里做的動(dòng)作

  1. 通過協(xié)議類型得到相應(yīng)的Socket的 Processor,并cache起來,在這里這個(gè)Processor就是Http11Processor
  2. 通過一個(gè)ThreadLocal 類標(biāo)識(shí)現(xiàn)在的這個(gè)線程正在處理一個(gè)請(qǐng)求
  3. 調(diào)用Processor的process方法。

上面的Processor的process方法通過抽象類間接的調(diào)用了Http11Processorservice()方法。這個(gè)service()方法也是相當(dāng)復(fù)雜`
它主要完成的動(dòng)作有

  • 填充Request,Reponse屬性
  • 調(diào)用CoyoteAdapter的service()方法

通過以上不走,一個(gè)請(qǐng)求連接就從Connector走到了Container

小結(jié):

實(shí)現(xiàn)一個(gè)tomcat連接器Connector就是實(shí)現(xiàn)ProtocolHander接口的過程。Connector用來接收Socket Client端的請(qǐng)求,通過內(nèi)置的線程池去調(diào)用Servlet Container生成響應(yīng)結(jié)果,并將響應(yīng)結(jié)果同步或異步的返回給Socket Client。在第三方應(yīng)用集成tomcat作為Web容器時(shí),一般不會(huì)動(dòng)Servlet Container端的代碼,那么connector的性能將是整個(gè)Web容器性能的關(guān)鍵。

Tomcat 對(duì)于Socket的處理.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 1.Tomcat總體架構(gòu) Tomcat有Connector和Container兩大核心組件,Connector組件...
    monkey01閱讀 12,743評(píng)論 6 23
  • 這是我們分析tomcat的第八篇文章,這次我們分析連接器,我們?cè)缇拖敕治鲞B接器了,因?yàn)楦鞣N原因拖了好久。不過也確實(shí)...
    莫那一魯?shù)?/span>閱讀 2,106評(píng)論 0 7
  • 概述 Tomcat是一個(gè)JSP/Servlet容器。其作為Servlet容器,有三種工作模式:獨(dú)立的Servlet...
    jiangmo閱讀 2,322評(píng)論 0 13
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,564評(píng)論 19 139
  • 微信消息提醒,暖暖像往常一樣拿起手機(jī),看到有新朋友申請(qǐng)加好友。 暖暖點(diǎn)開看了看,又放下手機(jī),沒有再理會(huì),試圖繼續(xù)手...
    時(shí)貳婳閱讀 1,093評(píng)論 1 4

友情鏈接更多精彩內(nèi)容