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
Modifier and Type | Field and Description |
---|---|
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
|
Modifier | Constructor and Description |
---|---|
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) |
Modifier and Type | Method and Description |
---|---|
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() |
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)
Deprecated.
|
java.lang.String |
toString() |
TrafficCounter |
trafficCounter() |
protected int |
userDefinedWritabilityIndex() |
void |
write(ChannelHandlerContext ctx,
java.lang.Object msg,
ChannelPromise promise)
Calls
ChannelHandlerContext.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, handlerRemoved, isSharable
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
exceptionCaught, handlerAdded, handlerRemoved
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
in interface ChannelInboundHandler
channelRead
in class ChannelInboundHandlerAdapter
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
in interface ChannelOutboundHandler
read
in class ChannelDuplexHandler
public void write(ChannelHandlerContext ctx, java.lang.Object msg, ChannelPromise promise) throws java.lang.Exception
ChannelDuplexHandler
ChannelHandlerContext.write(Object, ChannelPromise)
to forward
to the next ChannelOutboundHandler
in the ChannelPipeline
.
Sub-classes may override this method to change behavior.write
in interface ChannelOutboundHandler
write
in class 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
in interface ChannelInboundHandler
channelRegistered
in class ChannelInboundHandlerAdapter
java.lang.Exception
public TrafficCounter trafficCounter()
public java.lang.String toString()
toString
in class java.lang.Object
protected long calculateSize(java.lang.Object msg)
Object
.
This implementation supports ByteBuf
and ByteBufHolder
. Sub-classes may override this.msg
- the msg for which the size should be calculated.-1
if unknown.Copyright © 2008–2018 The Netty Project. All rights reserved.