public abstract class AbstractTrafficShapingHandler extends ChannelDuplexHandler
AbstractTrafficShapingHandler allows to limit the global bandwidth
(see GlobalTrafficShapingHandler
) or per session
bandwidth (see ChannelTrafficShapingHandler
), as traffic shaping.
It allows you to implement an almost real time monitoring of the bandwidth using
the monitors from TrafficCounter
that will call back every checkInterval
the method doAccounting of this handler.
If you want for any particular reasons to stop the monitoring (accounting) or to change the read/write limit or the check interval, several methods allow that for you:
ChannelHandler.Sharable
限定符和类型 | 字段和说明 |
---|---|
protected long |
checkInterval
Delay between two performance snapshots
|
static long |
DEFAULT_CHECK_INTERVAL
Default delay between two checks: 1s
|
static long |
DEFAULT_MAX_TIME
Default max delay in case of traffic shaping
(during which no communication will occur).
|
protected long |
maxTime
Max delay in wait
|
protected TrafficCounter |
trafficCounter
Traffic Counter
|
限定符 | 构造器和说明 |
---|---|
protected |
AbstractTrafficShapingHandler()
|
protected |
AbstractTrafficShapingHandler(long checkInterval)
Constructor using NO LIMIT and
default max time as delay allowed value of 15000L ms.
|
protected |
AbstractTrafficShapingHandler(long writeLimit,
long readLimit)
|
protected |
AbstractTrafficShapingHandler(long writeLimit,
long readLimit,
long checkInterval)
Constructor using default max time as delay allowed value of 15000L ms.
|
protected |
AbstractTrafficShapingHandler(long writeLimit,
long readLimit,
long checkInterval,
long maxTime) |
限定符和类型 | 方法和说明 |
---|---|
protected long |
calculateSize(java.lang.Object msg)
Calculate the size of the given
Object . |
void |
channelRead(ChannelHandlerContext ctx,
java.lang.Object msg)
Calls
ChannelHandlerContext.fireChannelRead(Object) to forward
to the next ChannelInboundHandler in the ChannelPipeline . |
void |
channelRegistered(ChannelHandlerContext ctx)
Calls
ChannelHandlerContext.fireChannelRegistered() to forward
to the next ChannelInboundHandler in the ChannelPipeline . |
void |
configure(long newCheckInterval)
Change the check interval.
|
void |
configure(long newWriteLimit,
long newReadLimit)
Change the underlying limitations.
|
void |
configure(long newWriteLimit,
long newReadLimit,
long newCheckInterval)
Change the underlying limitations and check interval.
|
protected void |
doAccounting(TrafficCounter counter)
Called each time the accounting is computed from the TrafficCounters.
|
long |
getCheckInterval() |
long |
getMaxTimeWait() |
long |
getMaxWriteDelay() |
long |
getMaxWriteSize() |
long |
getReadLimit() |
long |
getWriteLimit() |
void |
handlerRemoved(ChannelHandlerContext ctx)
Do nothing by default, sub-classes may override this method.
|
protected static boolean |
isHandlerActive(ChannelHandlerContext ctx) |
void |
read(ChannelHandlerContext ctx)
Calls
ChannelHandlerContext.read() to forward
to the next ChannelOutboundHandler in the ChannelPipeline . |
void |
setCheckInterval(long checkInterval) |
void |
setMaxTimeWait(long maxTime)
Note the change will be taken as best effort, meaning
that all already scheduled traffics will not be
changed, but only applied to new traffics.
|
void |
setMaxWriteDelay(long maxWriteDelay)
Note the change will be taken as best effort, meaning
that all already scheduled traffics will not be
changed, but only applied to new traffics.
|
void |
setMaxWriteSize(long maxWriteSize)
Note that this limit is a best effort on memory limitation to prevent Out Of
Memory Exception.
|
void |
setReadLimit(long readLimit)
Note the change will be taken as best effort, meaning
that all already scheduled traffics will not be
changed, but only applied to new traffics.
|
void |
setWriteLimit(long writeLimit)
Note the change will be taken as best effort, meaning
that all already scheduled traffics will not be
changed, but only applied to new traffics.
|
protected void |
submitWrite(ChannelHandlerContext ctx,
java.lang.Object msg,
long delay,
ChannelPromise promise)
已过时。
|
java.lang.String |
toString() |
TrafficCounter |
trafficCounter() |
protected int |
userDefinedWritabilityIndex() |
void |
write(ChannelHandlerContext ctx,
java.lang.Object msg,
ChannelPromise promise)
Calls
ChannelOutboundInvoker.write(Object, ChannelPromise) to forward
to the next ChannelOutboundHandler in the ChannelPipeline . |
bind, close, connect, deregister, disconnect, flush
channelActive, channelInactive, channelReadComplete, channelUnregistered, channelWritabilityChanged, exceptionCaught, userEventTriggered
ensureNotSharable, handlerAdded, isSharable
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
exceptionCaught, handlerAdded
public static final long DEFAULT_CHECK_INTERVAL
public static final long DEFAULT_MAX_TIME
protected TrafficCounter trafficCounter
protected volatile long maxTime
protected volatile long checkInterval
protected AbstractTrafficShapingHandler(long writeLimit, long readLimit, long checkInterval, long maxTime)
writeLimit
- 0 or a limit in bytes/sreadLimit
- 0 or a limit in bytes/scheckInterval
- The delay between two computations of performances for
channels or 0 if no stats are to be computed.maxTime
- The maximum delay to wait in case of traffic excess.
Must be positive.protected AbstractTrafficShapingHandler(long writeLimit, long readLimit, long checkInterval)
writeLimit
- 0 or a limit in bytes/sreadLimit
- 0 or a limit in bytes/scheckInterval
- The delay between two computations of performances for
channels or 0 if no stats are to be computed.protected AbstractTrafficShapingHandler(long writeLimit, long readLimit)
writeLimit
- 0 or a limit in bytes/sreadLimit
- 0 or a limit in bytes/sprotected AbstractTrafficShapingHandler()
protected AbstractTrafficShapingHandler(long checkInterval)
checkInterval
- The delay between two computations of performances for
channels or 0 if no stats are to be computed.protected int userDefinedWritabilityIndex()
public void configure(long newWriteLimit, long newReadLimit, long newCheckInterval)
Note the change will be taken as best effort, meaning that all already scheduled traffics will not be changed, but only applied to new traffics.
So the expected usage of this method is to be used not too often, accordingly to the traffic shaping configuration.
newWriteLimit
- The new write limit (in bytes)newReadLimit
- The new read limit (in bytes)newCheckInterval
- The new check interval (in milliseconds)public void configure(long newWriteLimit, long newReadLimit)
Note the change will be taken as best effort, meaning that all already scheduled traffics will not be changed, but only applied to new traffics.
So the expected usage of this method is to be used not too often, accordingly to the traffic shaping configuration.
newWriteLimit
- The new write limit (in bytes)newReadLimit
- The new read limit (in bytes)public void configure(long newCheckInterval)
newCheckInterval
- The new check interval (in milliseconds)public long getWriteLimit()
public void setWriteLimit(long writeLimit)
Note the change will be taken as best effort, meaning that all already scheduled traffics will not be changed, but only applied to new traffics.
So the expected usage of this method is to be used not too often, accordingly to the traffic shaping configuration.
writeLimit
- the writeLimit to setpublic long getReadLimit()
public void setReadLimit(long readLimit)
Note the change will be taken as best effort, meaning that all already scheduled traffics will not be changed, but only applied to new traffics.
So the expected usage of this method is to be used not too often, accordingly to the traffic shaping configuration.
readLimit
- the readLimit to setpublic long getCheckInterval()
public void setCheckInterval(long checkInterval)
checkInterval
- the interval in ms between each step check to set, default value being 1000 ms.public void setMaxTimeWait(long maxTime)
Note the change will be taken as best effort, meaning that all already scheduled traffics will not be changed, but only applied to new traffics.
So the expected usage of this method is to be used not too often, accordingly to the traffic shaping configuration.
maxTime
- Max delay in wait, shall be less than TIME OUT in related protocol.
Must be positive.public long getMaxTimeWait()
public long getMaxWriteDelay()
public void setMaxWriteDelay(long maxWriteDelay)
Note the change will be taken as best effort, meaning that all already scheduled traffics will not be changed, but only applied to new traffics.
So the expected usage of this method is to be used not too often, accordingly to the traffic shaping configuration.
maxWriteDelay
- the maximum Write Delay in ms in the buffer allowed before write suspension is set.
Must be positive.public long getMaxWriteSize()
public void setMaxWriteSize(long maxWriteSize)
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())
maxWriteSize
- the maximum Write Size allowed in the buffer
per channel before write suspended is set,
default being bytes.protected void doAccounting(TrafficCounter counter)
counter
- the TrafficCounter that computes its performancepublic void channelRead(ChannelHandlerContext ctx, java.lang.Object msg) throws java.lang.Exception
ChannelInboundHandlerAdapter
ChannelHandlerContext.fireChannelRead(Object)
to forward
to the next ChannelInboundHandler
in the ChannelPipeline
.
Sub-classes may override this method to change behavior.channelRead
在接口中 ChannelInboundHandler
channelRead
在类中 ChannelInboundHandlerAdapter
java.lang.Exception
public void handlerRemoved(ChannelHandlerContext ctx) throws java.lang.Exception
ChannelHandlerAdapter
handlerRemoved
在接口中 ChannelHandler
handlerRemoved
在类中 ChannelHandlerAdapter
java.lang.Exception
protected static boolean isHandlerActive(ChannelHandlerContext ctx)
public void read(ChannelHandlerContext ctx)
ChannelDuplexHandler
ChannelHandlerContext.read()
to forward
to the next ChannelOutboundHandler
in the ChannelPipeline
.
Sub-classes may override this method to change behavior.read
在接口中 ChannelOutboundHandler
read
在类中 ChannelDuplexHandler
public void write(ChannelHandlerContext ctx, java.lang.Object msg, ChannelPromise promise) throws java.lang.Exception
ChannelDuplexHandler
ChannelOutboundInvoker.write(Object, ChannelPromise)
to forward
to the next ChannelOutboundHandler
in the ChannelPipeline
.
Sub-classes may override this method to change behavior.write
在接口中 ChannelOutboundHandler
write
在类中 ChannelDuplexHandler
ctx
- the ChannelHandlerContext
for which the write operation is mademsg
- the message to writepromise
- the ChannelPromise
to notify once the operation completesjava.lang.Exception
- thrown if an error occurs@Deprecated protected void submitWrite(ChannelHandlerContext ctx, java.lang.Object msg, long delay, ChannelPromise promise)
public void channelRegistered(ChannelHandlerContext ctx) throws java.lang.Exception
ChannelInboundHandlerAdapter
ChannelHandlerContext.fireChannelRegistered()
to forward
to the next ChannelInboundHandler
in the ChannelPipeline
.
Sub-classes may override this method to change behavior.channelRegistered
在接口中 ChannelInboundHandler
channelRegistered
在类中 ChannelInboundHandlerAdapter
java.lang.Exception
public TrafficCounter trafficCounter()
public java.lang.String toString()
toString
在类中 java.lang.Object
protected long calculateSize(java.lang.Object msg)
Object
.
This implementation supports ByteBuf
, ByteBufHolder
and FileRegion
.
Sub-classes may override this.msg
- the msg for which the size should be calculated.-1
if unknown.