use of io.mycat.proxy.session.Session in project Mycat2 by MyCATApache.
the class NIOAcceptor method processReadKey.
/**
* 仅后台维护的主动创建的连接使用
*/
@SuppressWarnings("unchecked")
protected void processReadKey(SelectionKey curKey) throws IOException {
// only from cluster server socket
Session session = (Session) curKey.attachment();
assert session != null;
session.getCurNIOHandler().onSocketRead(session);
}
use of io.mycat.proxy.session.Session in project Mycat2 by MyCATApache.
the class NIOAcceptor method processWriteKey.
/**
* 仅后台维护的主动创建的连接使用
*/
protected void processWriteKey(SelectionKey curKey) throws IOException {
// only from cluster server socket
Session session = (Session) curKey.attachment();
assert session != null;
session.getCurNIOHandler().onSocketWrite(session);
}
use of io.mycat.proxy.session.Session in project Mycat2 by MyCATApache.
the class MycatMonitorCallback method getSession.
static Session getSession() {
SessionThread thread = getThread();
Session curSession = thread.getCurSession();
if (curSession instanceof MycatSession) {
return curSession;
} else if (curSession instanceof MySQLClientSession) {
MycatSession mycatSession = ((MySQLClientSession) curSession).getMycat();
if (mycatSession == null) {
return curSession;
} else {
return mycatSession;
}
} else {
throw new MycatException("unknown session");
}
}
use of io.mycat.proxy.session.Session 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