Search in sources :

Example 1 with NIOHandler

use of io.mycat.proxy.handler.NIOHandler in project Mycat2 by MyCATApache.

the class ProxyReactorThread method processReadKey.

@SuppressWarnings("unchecked")
protected void processReadKey(SelectionKey curKey) throws IOException {
    T session = (T) curKey.attachment();
    setCurSession(session);
    NIOHandler curNIOHandler = session.getCurNIOHandler();
    if (curNIOHandler != null) {
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("processReadKey sessionId:{}", session.sessionId());
        }
        curNIOHandler.onSocketRead(session);
    } else {
        ByteBuffer allocate = ByteBuffer.allocate(8192);
        int read = session.channel().read(allocate);
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug(ByteUtil.dump(allocate.array(), 0, allocate.position()));
            LOGGER.debug("processReadKey bug sessionId:{}", session.sessionId());
        }
    }
}
Also used : NIOHandler(io.mycat.proxy.handler.NIOHandler) BackendNIOHandler(io.mycat.proxy.handler.BackendNIOHandler) ByteBuffer(java.nio.ByteBuffer)

Example 2 with NIOHandler

use of io.mycat.proxy.handler.NIOHandler in project Mycat2 by MyCATApache.

the class ProxyReactorThread method processConnectKey.

/**
 * 该方法仅Reactor自身创建的主动连接使用
 */
@SuppressWarnings("unchecked")
protected void processConnectKey(SelectionKey curKey) throws IOException {
    T session = (T) curKey.attachment();
    setCurSession(session);
    SocketChannel channel = (SocketChannel) curKey.channel();
    NIOHandler curNIOHandler = session.getCurNIOHandler();
    if (curNIOHandler instanceof BackendNIOHandler) {
        BackendNIOHandler handler = (BackendNIOHandler) curNIOHandler;
        try {
            if (channel.finishConnect()) {
                handler.onConnect(curKey, session, true, null);
            } else {
                handler.onConnect(curKey, session, false, new ConnectException());
            }
        } catch (Exception e) {
            handler.onConnect(curKey, session, false, e);
        }
    }
}
Also used : SocketChannel(java.nio.channels.SocketChannel) NIOHandler(io.mycat.proxy.handler.NIOHandler) BackendNIOHandler(io.mycat.proxy.handler.BackendNIOHandler) BackendNIOHandler(io.mycat.proxy.handler.BackendNIOHandler) IOException(java.io.IOException) ClosedSelectorException(java.nio.channels.ClosedSelectorException) ConnectException(java.net.ConnectException) ConnectException(java.net.ConnectException)

Example 3 with NIOHandler

use of io.mycat.proxy.handler.NIOHandler in project Mycat2 by MyCATApache.

the class ProxyReactorThread method run.

public void run() {
    while (!this.isInterrupted()) {
        try {
            pendingJobsEmpty = pendingJobs.isEmpty();
            long startTime = System.nanoTime();
            if (pendingJobsEmpty) {
                // /////////////epoll///////////////////
                int numOfKeys = selector.select(SELECTOR_TIMEOUT);
                // ////////////////////////////////
                if (numOfKeys == 0) {
                    long dis = System.nanoTime() - startTime;
                    if (dis < (SELECTOR_TIMEOUT / 2)) {
                        invalidSelectCount++;
                    }
                }
            // //////epoll///////////
            } else {
                selector.selectNow();
            }
            updateLastActiveTime();
            final Set<SelectionKey> keys = selector.selectedKeys();
            if (keys.isEmpty()) {
                if (!pendingJobsEmpty) {
                    ioTimes = 0;
                    this.processNIOJob();
                }
                continue;
            } else if ((ioTimes > 5) & !pendingJobsEmpty) {
                ioTimes = 0;
                this.processNIOJob();
            }
            ioTimes++;
            for (final SelectionKey key : keys) {
                try {
                    if (!key.isValid() || !key.channel().isOpen()) {
                        continue;
                    }
                    int readdyOps = key.readyOps();
                    setCurSession(null);
                    // 如果当前收到连接请求
                    if ((readdyOps & SelectionKey.OP_ACCEPT) != 0) {
                        processAcceptKey(key);
                    } else // 如果当前连接事件
                    if ((readdyOps & SelectionKey.OP_CONNECT) != 0) {
                        this.processConnectKey(key);
                    } else if ((readdyOps & SelectionKey.OP_READ) != 0) {
                        this.processReadKey(key);
                    } else if ((readdyOps & SelectionKey.OP_WRITE) != 0) {
                        this.processWriteKey(key);
                    }
                } catch (Exception e) {
                    // 如果设置为IOException方便调试,避免吞没其他类型异常
                    LOGGER.error("", e);
                    Session curSession = getCurSession();
                    if (curSession != null) {
                        NIOHandler curNIOHandler = curSession.getCurNIOHandler();
                        if (curNIOHandler != null) {
                            curNIOHandler.onException(curSession, e);
                        } else {
                            curSession.close(false, curSession.setLastMessage(e));
                        }
                        setCurSession(null);
                    }
                }
            }
            keys.clear();
            // ///////epoll //////////
            if (invalidSelectCount > 512) {
                Selector newSelector = SelectorUtil.rebuildSelector(this.selector);
                if (newSelector != null) {
                    this.selector = newSelector;
                }
                invalidSelectCount = 0;
            }
        // ///////epoll//////////
        } catch (ClosedSelectorException e) {
            LOGGER.warn("selector is closed");
            break;
        } catch (Throwable e) {
            LOGGER.warn("Unknown session:{}", getCurSession(), e);
        }
    }
}
Also used : SelectionKey(java.nio.channels.SelectionKey) NIOHandler(io.mycat.proxy.handler.NIOHandler) BackendNIOHandler(io.mycat.proxy.handler.BackendNIOHandler) IOException(java.io.IOException) ClosedSelectorException(java.nio.channels.ClosedSelectorException) ConnectException(java.net.ConnectException) Session(io.mycat.proxy.session.Session) Selector(java.nio.channels.Selector) ClosedSelectorException(java.nio.channels.ClosedSelectorException)

Aggregations

BackendNIOHandler (io.mycat.proxy.handler.BackendNIOHandler)3 NIOHandler (io.mycat.proxy.handler.NIOHandler)3 IOException (java.io.IOException)2 ConnectException (java.net.ConnectException)2 ClosedSelectorException (java.nio.channels.ClosedSelectorException)2 Session (io.mycat.proxy.session.Session)1 ByteBuffer (java.nio.ByteBuffer)1 SelectionKey (java.nio.channels.SelectionKey)1 Selector (java.nio.channels.Selector)1 SocketChannel (java.nio.channels.SocketChannel)1