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());
}
}
}
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);
}
}
}
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);
}
}
}
Aggregations