@Override public void channelRead(final ChannelHandlerContext ctx, final Object msg) throws Exception { long size = calculateSize(msg); long now = TrafficCounter.milliSecondFromNano(); if (size > 0) { long waitGlobal = trafficCounter.readTimeToWait(size, getReadLimit(), maxTime, now); Integer key = ctx.channel().hashCode(); PerChannel perChannel = channelQueues.get(key); wait = computeBalancedWait(maxLocalRead, maxGlobalRead, wait); wait = checkWaitReadTime(ctx, wait, now); if (wait >= MINIMAL_WAIT) { // At least 10ms seems a minimal if (logger.isDebugEnabled()) { logger.debug("Read Suspend: " + wait + ':' + config.isAutoRead() + ':' + isHandlerActive(ctx)); if (config.isAutoRead() && isHandlerActive(ctx)) { config.setAutoRead(false); channel.attr(READ_SUSPENDED).set(true); if (logger.isDebugEnabled()) { logger.debug("Suspend final status => " + config.isAutoRead() + ':' + isHandlerActive(ctx) + " will reopened at: " + wait); informReadOperation(ctx, now); ctx.fireChannelRead(msg);
perChannel = getOrSetPerChannel(ctx); perChannel.queueSize += size; queuesSize.addAndGet(size); checkWriteSuspend(ctx, delay, perChannel.queueSize); if (queuesSize.get() > maxGlobalWriteSize) { globalSizeExceeded = true; setUserDefinedWritability(ctx, false);
@Override protected void doAccounting(TrafficCounter counter) { computeDeviationCumulativeBytes(); super.doAccounting(counter); }
if (channel.isActive()) { for (ToSend toSend : perChannel.messagesQueue) { long size = calculateSize(toSend.toSend); trafficCounter.bytesRealWriteFlowControl(size); perChannel.channelTrafficCounter.bytesRealWriteFlowControl(size); releaseWriteSuspended(ctx); releaseReadSuspended(ctx); super.handlerRemoved(ctx);
@Override public void write(final ChannelHandlerContext ctx, final Object msg, final ChannelPromise promise) throws Exception { long size = calculateSize(msg); long now = TrafficCounter.milliSecondFromNano(); if (size > 0) { long waitGlobal = trafficCounter.writeTimeToWait(size, getWriteLimit(), maxTime, now); Integer key = ctx.channel().hashCode(); PerChannel perChannel = channelQueues.get(key); maxGlobalWrite = maxLocalWrite; wait = computeBalancedWait(maxLocalWrite, maxGlobalWrite, wait); if (logger.isDebugEnabled()) { logger.debug("Write suspend: " + wait + ':' + ctx.channel().config().isAutoRead() + ':' + isHandlerActive(ctx)); submitWrite(ctx, msg, size, wait, now, promise); return; submitWrite(ctx, msg, size, 0, now, promise);
/** * Create a new instance. * * @param executor * the {@link ScheduledExecutorService} to use for the {@link TrafficCounter}. */ public GlobalChannelTrafficShapingHandler(ScheduledExecutorService executor) { createGlobalTrafficCounter(executor); }
@Override public void handlerAdded(ChannelHandlerContext ctx) throws Exception { getOrSetPerChannel(ctx); trafficCounter.resetCumulativeTime(); super.handlerAdded(ctx); }
@Override public void run() { if (!counter.monitorActive) { return; } long newLastTime = milliSecondFromNano(); counter.resetAccounting(newLastTime); for (PerChannel perChannel : trafficShapingHandler1.channelQueues.values()) { perChannel.channelTrafficCounter.resetAccounting(newLastTime); } trafficShapingHandler1.doAccounting(counter); counter.scheduledFuture = counter.executor.schedule(this, counter.checkInterval.get(), TimeUnit.MILLISECONDS); } }
@Override protected long calculateSize(Object msg) { if (msg instanceof DataBlock) { return ((DataBlock) msg).getByteCount(); } return super.calculateSize(msg); }
@Override public void write(final ChannelHandlerContext ctx, final Object msg, final ChannelPromise promise) throws Exception { long size = calculateSize(msg); long now = TrafficCounter.milliSecondFromNano(); if (size > 0) { long waitGlobal = trafficCounter.writeTimeToWait(size, getWriteLimit(), maxTime, now); Integer key = ctx.channel().hashCode(); PerChannel perChannel = channelQueues.get(key); maxGlobalWrite = maxLocalWrite; wait = computeBalancedWait(maxLocalWrite, maxGlobalWrite, wait); if (logger.isDebugEnabled()) { logger.debug("Write suspend: " + wait + ':' + ctx.channel().config().isAutoRead() + ':' + isHandlerActive(ctx)); submitWrite(ctx, msg, size, wait, now, promise); return; submitWrite(ctx, msg, size, 0, now, promise);
if (channel.isActive()) { for (ToSend toSend : perChannel.messagesQueue) { long size = calculateSize(toSend.toSend); trafficCounter.bytesRealWriteFlowControl(size); perChannel.channelTrafficCounter.bytesRealWriteFlowControl(size); releaseWriteSuspended(ctx); releaseReadSuspended(ctx); super.handlerRemoved(ctx);
/** * Create a new instance. * * @param executor * the {@link ScheduledExecutorService} to use for the {@link TrafficCounter}. * @param checkInterval * The delay between two computations of performances for * channels or 0 if no stats are to be computed. */ public GlobalChannelTrafficShapingHandler(ScheduledExecutorService executor, long checkInterval) { super(checkInterval); createGlobalTrafficCounter(executor); }
@Override public void handlerAdded(ChannelHandlerContext ctx) throws Exception { getOrSetPerChannel(ctx); trafficCounter.resetCumulativeTime(); super.handlerAdded(ctx); }
@Override public void run() { if (!counter.monitorActive) { return; } long newLastTime = milliSecondFromNano(); counter.resetAccounting(newLastTime); for (PerChannel perChannel : trafficShapingHandler1.channelQueues.values()) { perChannel.channelTrafficCounter.resetAccounting(newLastTime); } trafficShapingHandler1.doAccounting(counter); counter.scheduledFuture = counter.executor.schedule(this, counter.checkInterval.get(), TimeUnit.MILLISECONDS); } }
@Override public void channelRead(final ChannelHandlerContext ctx, final Object msg) throws Exception { long size = calculateSize(msg); long now = TrafficCounter.milliSecondFromNano(); if (size > 0) { long waitGlobal = trafficCounter.readTimeToWait(size, getReadLimit(), maxTime, now); Integer key = ctx.channel().hashCode(); PerChannel perChannel = channelQueues.get(key); wait = computeBalancedWait(maxLocalRead, maxGlobalRead, wait); wait = checkWaitReadTime(ctx, wait, now); if (wait >= MINIMAL_WAIT) { // At least 10ms seems a minimal if (logger.isDebugEnabled()) { logger.debug("Read Suspend: " + wait + ':' + config.isAutoRead() + ':' + isHandlerActive(ctx)); if (config.isAutoRead() && isHandlerActive(ctx)) { config.setAutoRead(false); channel.attr(READ_SUSPENDED).set(true); if (logger.isDebugEnabled()) { logger.debug("Suspend final status => " + config.isAutoRead() + ':' + isHandlerActive(ctx) + " will reopened at: " + wait); informReadOperation(ctx, now); ctx.fireChannelRead(msg);
@Override public void write(final ChannelHandlerContext ctx, final Object msg, final ChannelPromise promise) throws Exception { long size = calculateSize(msg); long now = TrafficCounter.milliSecondFromNano(); if (size > 0) { long waitGlobal = trafficCounter.writeTimeToWait(size, getWriteLimit(), maxTime, now); Integer key = ctx.channel().hashCode(); PerChannel perChannel = channelQueues.get(key); maxGlobalWrite = maxLocalWrite; wait = computeBalancedWait(maxLocalWrite, maxGlobalWrite, wait); if (logger.isDebugEnabled()) { logger.debug("Write suspend: " + wait + ':' + ctx.channel().config().isAutoRead() + ':' + isHandlerActive(ctx)); submitWrite(ctx, msg, size, wait, now, promise); return; submitWrite(ctx, msg, size, 0, now, promise);
if (channel.isActive()) { for (ToSend toSend : perChannel.messagesQueue) { long size = calculateSize(toSend.toSend); trafficCounter.bytesRealWriteFlowControl(size); perChannel.channelTrafficCounter.bytesRealWriteFlowControl(size); releaseWriteSuspended(ctx); releaseReadSuspended(ctx); super.handlerRemoved(ctx);
perChannel = getOrSetPerChannel(ctx); perChannel.queueSize += size; queuesSize.addAndGet(size); checkWriteSuspend(ctx, delay, perChannel.queueSize); if (queuesSize.get() > maxGlobalWriteSize) { globalSizeExceeded = true; setUserDefinedWritability(ctx, false);
/** * Create a new instance. * * @param executor * the {@link ScheduledExecutorService} to use for the {@link TrafficCounter}. */ public GlobalChannelTrafficShapingHandler(ScheduledExecutorService executor) { createGlobalTrafficCounter(executor); }
@Override public void handlerAdded(ChannelHandlerContext ctx) throws Exception { getOrSetPerChannel(ctx); trafficCounter.resetCumulativeTime(); super.handlerAdded(ctx); }