@Override public void messageReceived(NextFilter nextFilter, final IoSession session, Object message) throws Exception { int maxReadThroughput = this.maxReadThroughput; // process the request if our max is greater than zero if (maxReadThroughput > 0) { final State state = (State) session.getAttribute(STATE); long currentTime = System.currentTimeMillis(); long suspendTime = 0; boolean firstRead = false; synchronized (state) { state.readBytes += messageSizeEstimator.estimateSize(message); if (!state.suspendedRead) { if (state.readStartTime == 0) { firstRead = true; state.readStartTime = currentTime - 1000; } long throughput = (state.readBytes * 1000 / (currentTime - state.readStartTime)); if (throughput >= maxReadThroughput) { suspendTime = Math.max( 0, state.readBytes * 1000 / maxReadThroughput - (firstRead ? 0 : currentTime - state.readStartTime)); state.readBytes = 0; state.readStartTime = 0; state.suspendedRead = suspendTime != 0; adjustReadBufferSize(session); } } } if (suspendTime != 0) { session.suspendRead(); scheduledExecutor.schedule( new Runnable() { public void run() { synchronized (state) { state.suspendedRead = false; } session.resumeRead(); } }, suspendTime, TimeUnit.MILLISECONDS); } } nextFilter.messageReceived(session, message); }
/** * Force the NioSession to be released and cleaned up. * * @param session */ private void forceClose(final IoSession session) { log.warn("Force close - session: {}", session.getId()); if (session.containsAttribute("FORCED_CLOSE")) { log.info("Close already forced on this session: {}", session.getId()); } else { // set flag session.setAttribute("FORCED_CLOSE", Boolean.TRUE); session.suspendRead(); cleanSession(session, true); } }
@Override public void sessionCreated(IoSession session) throws Exception { session.suspendRead(); session.suspendWrite(); }