@Override public void close() { super.close(); reset(); }
@Override public void run() { try { conn.handleRequest(transfer, id, operation); } catch (Throwable e) { logger.error("Failed to handle request, id: " + id + ", operation: " + operation, e); conn.sendError(transfer, id, e); } } }
public void addAsyncCallback(int id, AsyncCallback<?> ac) { ac.setTransfer(this); conn.addAsyncCallback(id, ac); }
@Override public void executeNextStatement() { int priority = PreparedStatement.MIN_PRIORITY; while (true) { PreparedCommand c = getNextBestCommand(priority, true); if (c == null) { try { haveWork.tryAcquire(loopInterval, TimeUnit.MILLISECONDS); haveWork.drainPermits(); } catch (InterruptedException e) { throw new AssertionError(); } break; } try { c.execute(); } catch (Throwable e) { c.transfer.getTransferConnection().sendError(c.transfer, c.id, e); } } }
@Override public void run() { try { conn.handleResponse(transfer, id, status); } catch (Throwable e) { // String msg = "Failed to handle response, id: " + id + ", status: " + status; throw DbException.convert(e); } } }
private void handlePacket(Transfer transfer) throws IOException { boolean isRequest = transfer.readByte() == Transfer.REQUEST; int id = transfer.readInt(); Runnable task; if (isRequest) { int operation = transfer.readInt(); task = new RequestPacketDeliveryTask(this, transfer, id, operation); } else { int status = transfer.readInt(); task = new ResponsePacketDeliveryTask(this, transfer, id, status); } TransferPacketHandler packetHandler = getPacketHandler(); if (packetHandler == null) { task.run(); } else { packetHandler.handle(task); } } }
int packetLength = transfer.readInt(); if (length - 4 == packetLength) { handlePacket(transfer); break; } else if (length - 4 > packetLength) { handlePacket(transfer); pos = pos + packetLength + 4; length = length - (packetLength + 4);
@Override public void executeNextStatementIfNeeded(PreparedStatement current) { // 如果出来各高优化级的命令,最多只抢占3次,避免堆栈溢出 if (nested >= 3) return; nested++; int priority = current.getPriority(); boolean hasHigherPriorityCommand = false; while (true) { PreparedCommand c = getNextBestCommand(priority, false); if (c == null) { break; } hasHigherPriorityCommand = true; try { c.execute(); } catch (Throwable e) { c.transfer.getTransferConnection().sendError(c.transfer, c.id, e); } } if (hasHigherPriorityCommand) { current.setPriority(priority + 1); } nested--; }
/** * Close a connection. */ @Override public void close() { try { for (Session s : sessions.values()) closeSession(s); sessions.clear(); super.close(); } catch (Exception e) { logger.error("Failed to close connection", e); } } }