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:
Modifier and Type | Class and Description |
---|---|
(package private) static class |
AbstractTrafficShapingHandler.ReopenReadTimerTask
Class to implement setReadable at fix time
|
ChannelHandler.Sharable
Modifier and Type | Field and Description |
---|---|
(package private) static int |
CHANNEL_DEFAULT_USER_DEFINED_WRITABILITY_INDEX
Default value for Channel UserDefinedWritability index
|
protected long |
checkInterval
Delay between two performance snapshots
|
static long |
DEFAULT_CHECK_INTERVAL
Default delay between two checks: 1s
|
(package private) static long |
DEFAULT_MAX_SIZE
Default max size to not exceed in buffer (write only).
|
static long |
DEFAULT_MAX_TIME
Default max delay in case of traffic shaping
(during which no communication will occur).
|
(package private) static int |
GLOBAL_DEFAULT_USER_DEFINED_WRITABILITY_INDEX
Default value for Global UserDefinedWritability index
|
(package private) static int |
GLOBALCHANNEL_DEFAULT_USER_DEFINED_WRITABILITY_INDEX
Default value for GlobalChannel UserDefinedWritability index
|
private static InternalLogger |
logger |
protected long |
maxTime
Max delay in wait
|
(package private) long |
maxWriteDelay
Max time to delay before proposing to stop writing new objects from next handlers
|
(package private) long |
maxWriteSize
Max size in the list before proposing to stop writing new objects from next handlers
|
(package private) static long |
MINIMAL_WAIT
Default minimal time to wait
|
(package private) static AttributeKey<java.lang.Boolean> |
READ_SUSPENDED |
private long |
readLimit
Limit in B/s to apply to read
|
(package private) static AttributeKey<java.lang.Runnable> |
REOPEN_TASK |
protected TrafficCounter |
trafficCounter
Traffic Counter
|
(package private) int |
userDefinedWritabilityIndex
Rank in UserDefinedWritability (1 for Channel, 2 for Global TrafficShapingHandler).
|
private long |
writeLimit
Limit in B/s to apply to write
|
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 . |
(package private) long |
checkWaitReadTime(ChannelHandlerContext ctx,
long wait,
long now)
Method overridden in GTSH to take into account specific timer for the channel.
|
(package private) void |
checkWriteSuspend(ChannelHandlerContext ctx,
long delay,
long queueSize)
Check the writability according to delay and size for the channel.
|
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() |
(package private) void |
informReadOperation(ChannelHandlerContext ctx,
long now)
Method overridden in GTSH to take into account specific timer for the channel.
|
protected static boolean |
isHandlerActive(ChannelHandlerContext ctx) |
void |
read(ChannelHandlerContext ctx)
Calls
ChannelHandlerContext.read() to forward
to the next ChannelOutboundHandler in the ChannelPipeline . |
(package private) void |
releaseReadSuspended(ChannelHandlerContext ctx)
Release the Read suspension
|
(package private) void |
releaseWriteSuspended(ChannelHandlerContext ctx)
Explicitly release the Write suspended status.
|
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.
|
(package private) void |
setTrafficCounter(TrafficCounter newTrafficCounter) |
(package private) void |
setUserDefinedWritability(ChannelHandlerContext ctx,
boolean writable) |
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.
|
(package private) abstract void |
submitWrite(ChannelHandlerContext ctx,
java.lang.Object msg,
long size,
long delay,
long now,
ChannelPromise promise) |
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
handlerAdded, handlerRemoved, isSharable
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
exceptionCaught, handlerAdded, handlerRemoved
private static final InternalLogger logger
public static final long DEFAULT_CHECK_INTERVAL
public static final long DEFAULT_MAX_TIME
static final long DEFAULT_MAX_SIZE
static final long MINIMAL_WAIT
protected TrafficCounter trafficCounter
private volatile long writeLimit
private volatile long readLimit
protected volatile long maxTime
protected volatile long checkInterval
static final AttributeKey<java.lang.Boolean> READ_SUSPENDED
static final AttributeKey<java.lang.Runnable> REOPEN_TASK
volatile long maxWriteDelay
volatile long maxWriteSize
final int userDefinedWritabilityIndex
static final int CHANNEL_DEFAULT_USER_DEFINED_WRITABILITY_INDEX
static final int GLOBAL_DEFAULT_USER_DEFINED_WRITABILITY_INDEX
static final int GLOBALCHANNEL_DEFAULT_USER_DEFINED_WRITABILITY_INDEX
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.void setTrafficCounter(TrafficCounter newTrafficCounter)
newTrafficCounter
- the TrafficCounter to setprotected 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 beeing 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 4194304L bytes.protected void doAccounting(TrafficCounter counter)
counter
- the TrafficCounter that computes its performancevoid releaseReadSuspended(ChannelHandlerContext ctx)
public 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
long checkWaitReadTime(ChannelHandlerContext ctx, long wait, long now)
wait
- the wait delay computed in msnow
- the relative now time in msvoid informReadOperation(ChannelHandlerContext ctx, long now)
now
- the relative now time in msprotected 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 accour@Deprecated protected void submitWrite(ChannelHandlerContext ctx, java.lang.Object msg, long delay, ChannelPromise promise)
abstract void submitWrite(ChannelHandlerContext ctx, java.lang.Object msg, long size, long delay, long now, 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
void setUserDefinedWritability(ChannelHandlerContext ctx, boolean writable)
void checkWriteSuspend(ChannelHandlerContext ctx, long delay, long queueSize)
delay
- the computed delayqueueSize
- the current queueSizevoid releaseWriteSuspended(ChannelHandlerContext ctx)
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.