Class AbstractTrafficShapingHandler
- java.lang.Object
-
- io.netty.channel.ChannelHandlerAdapter
-
- io.netty.channel.ChannelInboundHandlerAdapter
-
- io.netty.channel.ChannelDuplexHandler
-
- io.netty.handler.traffic.AbstractTrafficShapingHandler
-
- All Implemented Interfaces:
ChannelHandler
,ChannelInboundHandler
,ChannelOutboundHandler
- Direct Known Subclasses:
ChannelTrafficShapingHandler
,GlobalChannelTrafficShapingHandler
,GlobalTrafficShapingHandler
public abstract class AbstractTrafficShapingHandler extends ChannelDuplexHandler
AbstractTrafficShapingHandler allows to limit the global bandwidth (see
GlobalTrafficShapingHandler
) or per session bandwidth (seeChannelTrafficShapingHandler
), as traffic shaping. It allows you to implement an almost real time monitoring of the bandwidth using the monitors fromTrafficCounter
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:
- configure allows you to change read or write limits, or the checkInterval
- getTrafficCounter allows you to have access to the TrafficCounter and so to stop or start the monitoring, to change the checkInterval directly, or to have access to its values.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description (package private) static class
AbstractTrafficShapingHandler.ReopenReadTimerTask
Class to implement setReadable at fix time-
Nested classes/interfaces inherited from interface io.netty.channel.ChannelHandler
ChannelHandler.Sharable
-
-
Field Summary
Fields Modifier and Type Field Description (package private) static int
CHANNEL_DEFAULT_USER_DEFINED_WRITABILITY_INDEX
Default value for Channel UserDefinedWritability indexprotected long
checkInterval
Delay between two performance snapshotsstatic 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 indexprivate 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: 10ms(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
-
Constructor Summary
Constructors Modifier Constructor 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)
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Deprecated Methods Modifier and Type Method Description protected long
calculateSize(java.lang.Object msg)
Calculate the size of the givenObject
.void
channelRead(ChannelHandlerContext ctx, java.lang.Object msg)
CallsChannelHandlerContext.fireChannelRead(Object)
to forward to the nextChannelInboundHandler
in theChannelPipeline
.void
channelRegistered(ChannelHandlerContext ctx)
CallsChannelHandlerContext.fireChannelRegistered()
to forward to the nextChannelInboundHandler
in theChannelPipeline
.(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()
void
handlerRemoved(ChannelHandlerContext ctx)
Do nothing by default, sub-classes may override this method.(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)
CallsChannelHandlerContext.read()
to forward to the nextChannelOutboundHandler
in theChannelPipeline
.(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.(package private) abstract void
submitWrite(ChannelHandlerContext ctx, java.lang.Object msg, long size, long delay, long now, ChannelPromise promise)
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)
CallsChannelOutboundInvoker.write(Object, ChannelPromise)
to forward to the nextChannelOutboundHandler
in theChannelPipeline
.-
Methods inherited from class io.netty.channel.ChannelDuplexHandler
bind, close, connect, deregister, disconnect, flush
-
Methods inherited from class io.netty.channel.ChannelInboundHandlerAdapter
channelActive, channelInactive, channelReadComplete, channelUnregistered, channelWritabilityChanged, exceptionCaught, userEventTriggered
-
Methods inherited from class io.netty.channel.ChannelHandlerAdapter
ensureNotSharable, handlerAdded, isSharable
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface io.netty.channel.ChannelHandler
handlerAdded
-
-
-
-
Field Detail
-
logger
private static final InternalLogger logger
-
DEFAULT_CHECK_INTERVAL
public static final long DEFAULT_CHECK_INTERVAL
Default delay between two checks: 1s- See Also:
- Constant Field Values
-
DEFAULT_MAX_TIME
public static final long DEFAULT_MAX_TIME
Default max delay in case of traffic shaping (during which no communication will occur). Shall be less than TIMEOUT. Here half of "standard" 30s- See Also:
- Constant Field Values
-
DEFAULT_MAX_SIZE
static final long DEFAULT_MAX_SIZE
Default max size to not exceed in buffer (write only).- See Also:
- Constant Field Values
-
MINIMAL_WAIT
static final long MINIMAL_WAIT
Default minimal time to wait: 10ms- See Also:
- Constant Field Values
-
trafficCounter
protected TrafficCounter trafficCounter
Traffic Counter
-
writeLimit
private volatile long writeLimit
Limit in B/s to apply to write
-
readLimit
private volatile long readLimit
Limit in B/s to apply to read
-
maxTime
protected volatile long maxTime
Max delay in wait
-
checkInterval
protected volatile long checkInterval
Delay between two performance snapshots
-
READ_SUSPENDED
static final AttributeKey<java.lang.Boolean> READ_SUSPENDED
-
REOPEN_TASK
static final AttributeKey<java.lang.Runnable> REOPEN_TASK
-
maxWriteDelay
volatile long maxWriteDelay
Max time to delay before proposing to stop writing new objects from next handlers
-
maxWriteSize
volatile long maxWriteSize
Max size in the list before proposing to stop writing new objects from next handlers
-
userDefinedWritabilityIndex
final int userDefinedWritabilityIndex
Rank in UserDefinedWritability (1 for Channel, 2 for Global TrafficShapingHandler). Set in final constructor. Must be between 1 and 31
-
CHANNEL_DEFAULT_USER_DEFINED_WRITABILITY_INDEX
static final int CHANNEL_DEFAULT_USER_DEFINED_WRITABILITY_INDEX
Default value for Channel UserDefinedWritability index- See Also:
- Constant Field Values
-
GLOBAL_DEFAULT_USER_DEFINED_WRITABILITY_INDEX
static final int GLOBAL_DEFAULT_USER_DEFINED_WRITABILITY_INDEX
Default value for Global UserDefinedWritability index- See Also:
- Constant Field Values
-
GLOBALCHANNEL_DEFAULT_USER_DEFINED_WRITABILITY_INDEX
static final int GLOBALCHANNEL_DEFAULT_USER_DEFINED_WRITABILITY_INDEX
Default value for GlobalChannel UserDefinedWritability index- See Also:
- Constant Field Values
-
-
Constructor Detail
-
AbstractTrafficShapingHandler
protected AbstractTrafficShapingHandler(long writeLimit, long readLimit, long checkInterval, long maxTime)
- Parameters:
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.
-
AbstractTrafficShapingHandler
protected AbstractTrafficShapingHandler(long writeLimit, long readLimit, long checkInterval)
Constructor using default max time as delay allowed value of 15000L ms.- Parameters:
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.
-
AbstractTrafficShapingHandler
protected AbstractTrafficShapingHandler(long writeLimit, long readLimit)
Constructor using default Check Interval value of 1000L ms and default max time as delay allowed value of 15000L ms.- Parameters:
writeLimit
- 0 or a limit in bytes/sreadLimit
- 0 or a limit in bytes/s
-
AbstractTrafficShapingHandler
protected AbstractTrafficShapingHandler()
-
AbstractTrafficShapingHandler
protected AbstractTrafficShapingHandler(long checkInterval)
Constructor using NO LIMIT and default max time as delay allowed value of 15000L ms.- Parameters:
checkInterval
- The delay between two computations of performances for channels or 0 if no stats are to be computed.
-
-
Method Detail
-
setTrafficCounter
void setTrafficCounter(TrafficCounter newTrafficCounter)
- Parameters:
newTrafficCounter
- the TrafficCounter to set
-
userDefinedWritabilityIndex
protected int userDefinedWritabilityIndex()
-
configure
public void configure(long newWriteLimit, long newReadLimit, long newCheckInterval)
Change the underlying limitations and check interval.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.
- Parameters:
newWriteLimit
- The new write limit (in bytes)newReadLimit
- The new read limit (in bytes)newCheckInterval
- The new check interval (in milliseconds)
-
configure
public void configure(long newWriteLimit, long newReadLimit)
Change the underlying limitations.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.
- Parameters:
newWriteLimit
- The new write limit (in bytes)newReadLimit
- The new read limit (in bytes)
-
configure
public void configure(long newCheckInterval)
Change the check interval.- Parameters:
newCheckInterval
- The new check interval (in milliseconds)
-
getWriteLimit
public long getWriteLimit()
- Returns:
- the writeLimit
-
setWriteLimit
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.
- Parameters:
writeLimit
- the writeLimit to set
-
getReadLimit
public long getReadLimit()
- Returns:
- the readLimit
-
setReadLimit
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.
- Parameters:
readLimit
- the readLimit to set
-
getCheckInterval
public long getCheckInterval()
- Returns:
- the checkInterval
-
setCheckInterval
public void setCheckInterval(long checkInterval)
- Parameters:
checkInterval
- the interval in ms between each step check to set, default value being 1000 ms.
-
setMaxTimeWait
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.
- Parameters:
maxTime
- Max delay in wait, shall be less than TIME OUT in related protocol. Must be positive.
-
getMaxTimeWait
public long getMaxTimeWait()
- Returns:
- the max delay in wait to prevent TIME OUT
-
getMaxWriteDelay
public long getMaxWriteDelay()
- Returns:
- the maxWriteDelay
-
setMaxWriteDelay
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.
- Parameters:
maxWriteDelay
- the maximum Write Delay in ms in the buffer allowed before write suspension is set. Must be positive.
-
getMaxWriteSize
public long getMaxWriteSize()
- Returns:
- the maxWriteSize default being 4194304L bytes.
-
setMaxWriteSize
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 correspondingchannelWritabilityChanged()
- the
ChannelFuture.addListener(new GenericFutureListener())
- Parameters:
maxWriteSize
- the maximum Write Size allowed in the buffer per channel before write suspended is set, default being 4194304L bytes.
-
doAccounting
protected void doAccounting(TrafficCounter counter)
Called each time the accounting is computed from the TrafficCounters. This method could be used for instance to implement almost real time accounting.- Parameters:
counter
- the TrafficCounter that computes its performance
-
releaseReadSuspended
void releaseReadSuspended(ChannelHandlerContext ctx)
Release the Read suspension
-
channelRead
public void channelRead(ChannelHandlerContext ctx, java.lang.Object msg) throws java.lang.Exception
Description copied from class:ChannelInboundHandlerAdapter
CallsChannelHandlerContext.fireChannelRead(Object)
to forward to the nextChannelInboundHandler
in theChannelPipeline
. Sub-classes may override this method to change behavior.- Specified by:
channelRead
in interfaceChannelInboundHandler
- Overrides:
channelRead
in classChannelInboundHandlerAdapter
- Throws:
java.lang.Exception
-
handlerRemoved
public void handlerRemoved(ChannelHandlerContext ctx) throws java.lang.Exception
Description copied from class:ChannelHandlerAdapter
Do nothing by default, sub-classes may override this method.- Specified by:
handlerRemoved
in interfaceChannelHandler
- Overrides:
handlerRemoved
in classChannelHandlerAdapter
- Throws:
java.lang.Exception
-
checkWaitReadTime
long checkWaitReadTime(ChannelHandlerContext ctx, long wait, long now)
Method overridden in GTSH to take into account specific timer for the channel.- Parameters:
wait
- the wait delay computed in msnow
- the relative now time in ms- Returns:
- the wait to use according to the context
-
informReadOperation
void informReadOperation(ChannelHandlerContext ctx, long now)
Method overridden in GTSH to take into account specific timer for the channel.- Parameters:
now
- the relative now time in ms
-
isHandlerActive
protected static boolean isHandlerActive(ChannelHandlerContext ctx)
-
read
public void read(ChannelHandlerContext ctx)
Description copied from class:ChannelDuplexHandler
CallsChannelHandlerContext.read()
to forward to the nextChannelOutboundHandler
in theChannelPipeline
. Sub-classes may override this method to change behavior.- Specified by:
read
in interfaceChannelOutboundHandler
- Overrides:
read
in classChannelDuplexHandler
-
write
public void write(ChannelHandlerContext ctx, java.lang.Object msg, ChannelPromise promise) throws java.lang.Exception
Description copied from class:ChannelDuplexHandler
CallsChannelOutboundInvoker.write(Object, ChannelPromise)
to forward to the nextChannelOutboundHandler
in theChannelPipeline
. Sub-classes may override this method to change behavior.- Specified by:
write
in interfaceChannelOutboundHandler
- Overrides:
write
in classChannelDuplexHandler
- Parameters:
ctx
- theChannelHandlerContext
for which the write operation is mademsg
- the message to writepromise
- theChannelPromise
to notify once the operation completes- Throws:
java.lang.Exception
- thrown if an error occurs
-
submitWrite
@Deprecated protected void submitWrite(ChannelHandlerContext ctx, java.lang.Object msg, long delay, ChannelPromise promise)
Deprecated.
-
submitWrite
abstract void submitWrite(ChannelHandlerContext ctx, java.lang.Object msg, long size, long delay, long now, ChannelPromise promise)
-
channelRegistered
public void channelRegistered(ChannelHandlerContext ctx) throws java.lang.Exception
Description copied from class:ChannelInboundHandlerAdapter
CallsChannelHandlerContext.fireChannelRegistered()
to forward to the nextChannelInboundHandler
in theChannelPipeline
. Sub-classes may override this method to change behavior.- Specified by:
channelRegistered
in interfaceChannelInboundHandler
- Overrides:
channelRegistered
in classChannelInboundHandlerAdapter
- Throws:
java.lang.Exception
-
setUserDefinedWritability
void setUserDefinedWritability(ChannelHandlerContext ctx, boolean writable)
-
checkWriteSuspend
void checkWriteSuspend(ChannelHandlerContext ctx, long delay, long queueSize)
Check the writability according to delay and size for the channel. Set if necessary setUserDefinedWritability status.- Parameters:
delay
- the computed delayqueueSize
- the current queueSize
-
releaseWriteSuspended
void releaseWriteSuspended(ChannelHandlerContext ctx)
Explicitly release the Write suspended status.
-
trafficCounter
public TrafficCounter trafficCounter()
- Returns:
- the current TrafficCounter (if channel is still connected)
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
calculateSize
protected long calculateSize(java.lang.Object msg)
Calculate the size of the givenObject
. This implementation supportsByteBuf
,ByteBufHolder
andFileRegion
. Sub-classes may override this.- Parameters:
msg
- the msg for which the size should be calculated.- Returns:
- size the size of the msg or
-1
if unknown.
-
-