diff --git a/handler/src/main/java/io/netty/handler/traffic/AbstractTrafficShapingHandler.java b/handler/src/main/java/io/netty/handler/traffic/AbstractTrafficShapingHandler.java index 51784beaf7..3254cd3c12 100644 --- a/handler/src/main/java/io/netty/handler/traffic/AbstractTrafficShapingHandler.java +++ b/handler/src/main/java/io/netty/handler/traffic/AbstractTrafficShapingHandler.java @@ -167,12 +167,12 @@ public abstract class AbstractTrafficShapingHandler extends ChannelHandlerAdapte * The maximum delay to wait in case of traffic excess. * Must be positive. */ - protected AbstractTrafficShapingHandler(long writeLimit, long readLimit, - long checkInterval, long maxTime) { + protected AbstractTrafficShapingHandler(long writeLimit, long readLimit, long checkInterval, long maxTime) { if (maxTime <= 0) { throw new IllegalArgumentException("maxTime must be positive"); } - this.userDefinedWritabilityIndex = userDefinedWritabilityIndex(); + + userDefinedWritabilityIndex = userDefinedWritabilityIndex(); this.writeLimit = writeLimit; this.readLimit = readLimit; this.checkInterval = checkInterval; @@ -398,9 +398,9 @@ public abstract class AbstractTrafficShapingHandler extends ChannelHandlerAdapte *

Note that this limit is a best effort on memory limitation to prevent Out Of * Memory Exception. To ensure it works, the handler generating the write should * use one of the way provided by Netty to handle the capacity:

- *

- the Channel.isWritable() property and the corresponding - * channelWritabilityChanged()

- *

- the ChannelFuture.addListener(new GenericFutureListener())

+ *

- the {@code Channel.isWritable()} property and the corresponding + * {@code channelWritabilityChanged()}

+ *

- the {@code ChannelFuture.addListener(new GenericFutureListener())}

* * @param maxWriteSize the maximum Write Size allowed in the buffer * per channel before write suspended is set, @@ -430,13 +430,14 @@ public abstract class AbstractTrafficShapingHandler extends ChannelHandlerAdapte this.ctx = ctx; } + @Override public void run() { ChannelConfig config = ctx.channel().config(); if (!config.isAutoRead() && isHandlerActive(ctx)) { // If AutoRead is False and Active is True, user make a direct setAutoRead(false) // Then Just reset the status if (logger.isDebugEnabled()) { - logger.debug("Not unsuspend: " + config.isAutoRead() + ":" + + logger.debug("Not unsuspend: " + config.isAutoRead() + ':' + isHandlerActive(ctx)); } ctx.attr(READ_SUSPENDED).set(false); @@ -444,10 +445,10 @@ public abstract class AbstractTrafficShapingHandler extends ChannelHandlerAdapte // Anything else allows the handler to reset the AutoRead if (logger.isDebugEnabled()) { if (config.isAutoRead() && !isHandlerActive(ctx)) { - logger.debug("Unsuspend: " + config.isAutoRead() + ":" + + logger.debug("Unsuspend: " + config.isAutoRead() + ':' + isHandlerActive(ctx)); } else { - logger.debug("Normal unsuspend: " + config.isAutoRead() + ":" + logger.debug("Normal unsuspend: " + config.isAutoRead() + ':' + isHandlerActive(ctx)); } } @@ -456,7 +457,7 @@ public abstract class AbstractTrafficShapingHandler extends ChannelHandlerAdapte ctx.channel().read(); } if (logger.isDebugEnabled()) { - logger.debug("Unsupsend final status => " + config.isAutoRead() + ":" + logger.debug("Unsupsend final status => " + config.isAutoRead() + ':' + isHandlerActive(ctx)); } } @@ -483,7 +484,7 @@ public abstract class AbstractTrafficShapingHandler extends ChannelHandlerAdapte // Only AutoRead AND HandlerActive True means Context Active ChannelConfig config = ctx.channel().config(); if (logger.isDebugEnabled()) { - logger.debug("Read suspend: " + wait + ":" + config.isAutoRead() + ":" + logger.debug("Read suspend: " + wait + ':' + config.isAutoRead() + ':' + isHandlerActive(ctx)); } if (config.isAutoRead() && isHandlerActive(ctx)) { @@ -499,7 +500,7 @@ public abstract class AbstractTrafficShapingHandler extends ChannelHandlerAdapte } ctx.executor().schedule(reopenTask, wait, TimeUnit.MILLISECONDS); if (logger.isDebugEnabled()) { - logger.debug("Suspend final status => " + config.isAutoRead() + ":" + logger.debug("Suspend final status => " + config.isAutoRead() + ':' + isHandlerActive(ctx) + " will reopened at: " + wait); } } @@ -551,7 +552,7 @@ public abstract class AbstractTrafficShapingHandler extends ChannelHandlerAdapte long wait = trafficCounter.writeTimeToWait(size, writeLimit, maxTime, now); if (wait >= MINIMAL_WAIT) { if (logger.isDebugEnabled()) { - logger.debug("Write suspend: " + wait + ":" + ctx.channel().config().isAutoRead() + ":" + logger.debug("Write suspend: " + wait + ':' + ctx.channel().config().isAutoRead() + ':' + isHandlerActive(ctx)); } submitWrite(ctx, msg, size, wait, now, promise); @@ -569,8 +570,8 @@ public abstract class AbstractTrafficShapingHandler extends ChannelHandlerAdapte delay, TrafficCounter.milliSecondFromNano(), promise); } - abstract void submitWrite(final ChannelHandlerContext ctx, final Object msg, final long size, - final long delay, final long now, final ChannelPromise promise); + abstract void submitWrite( + ChannelHandlerContext ctx, Object msg, long size, long delay, long now, ChannelPromise promise); @Override public void channelRegistered(ChannelHandlerContext ctx) throws Exception { @@ -621,7 +622,7 @@ public abstract class AbstractTrafficShapingHandler extends ChannelHandlerAdapte .append(" maxSize: ").append(maxWriteSize) .append(" and Counter: "); if (trafficCounter != null) { - builder.append(trafficCounter.toString()); + builder.append(trafficCounter); } else { builder.append("none"); } diff --git a/handler/src/main/java/io/netty/handler/traffic/ChannelTrafficShapingHandler.java b/handler/src/main/java/io/netty/handler/traffic/ChannelTrafficShapingHandler.java index 518ffde960..8074a6a344 100644 --- a/handler/src/main/java/io/netty/handler/traffic/ChannelTrafficShapingHandler.java +++ b/handler/src/main/java/io/netty/handler/traffic/ChannelTrafficShapingHandler.java @@ -15,17 +15,17 @@ */ package io.netty.handler.traffic; -import java.util.ArrayDeque; -import java.util.concurrent.TimeUnit; - import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelPromise; +import java.util.ArrayDeque; +import java.util.concurrent.TimeUnit; + /** *

This implementation of the {@link AbstractTrafficShapingHandler} is for channel * traffic shaping, that is to say a per channel limitation of the bandwidth.

- *

Note the index used in OutboundBuffer.setUserDefinedWritability(index, boolean) is 1.

+ *

Note the index used in {@code OutboundBuffer.setUserDefinedWritability(index, boolean)} is 1.

* *

The general use should be as follow:

*