Class GlobalTrafficShapingHandler
- java.lang.Object
-
- io.netty.channel.ChannelHandlerAdapter
-
- io.netty.channel.ChannelInboundHandlerAdapter
-
- io.netty.channel.ChannelDuplexHandler
-
- io.netty.handler.traffic.AbstractTrafficShapingHandler
-
- io.netty.handler.traffic.GlobalTrafficShapingHandler
-
- All Implemented Interfaces:
ChannelHandler,ChannelInboundHandler,ChannelOutboundHandler
@Sharable public class GlobalTrafficShapingHandler extends AbstractTrafficShapingHandler
This implementation of the
AbstractTrafficShapingHandleris for global traffic shaping, that is to say a global limitation of the bandwidth, whatever the number of opened channels.Note the index used in
OutboundBuffer.setUserDefinedWritability(index, boolean)is 2.The general use should be as follow:
Create your unique GlobalTrafficShapingHandler like:
GlobalTrafficShapingHandler myHandler = new GlobalTrafficShapingHandler(executor);
The executor could be the underlying IO worker pool
pipeline.addLast(myHandler);
Note that this handler has a Pipeline Coverage of "all" which means only one such handler must be created and shared among all channels as the counter must be shared among all channels.
Other arguments can be passed like write or read limitation (in bytes/s where 0 means no limitation) or the check interval (in millisecond) that represents the delay between two computations of the bandwidth and so the call back of the doAccounting method (0 means no accounting at all).
A value of 0 means no accounting for checkInterval. If you need traffic shaping but no such accounting, it is recommended to set a positive value, even if it is high since the precision of the Traffic Shaping depends on the period where the traffic is computed. The highest the interval, the less precise the traffic shaping will be. It is suggested as higher value something close to 5 or 10 minutes.
maxTimeToWait, by default set to 15s, allows to specify an upper bound of time shaping.
- In your handler, you should consider to use the
channel.isWritable()andchannelWritabilityChanged(ctx)to handle writability, or throughfuture.addListener(new GenericFutureListener())on the future returned byctx.write(). You shall also consider to have object size in read or write operations relatively adapted to the bandwidth you required: for instance having 10 MB objects for 10KB/s will lead to burst effect, while having 100 KB objects for 1 MB/s should be smoothly handle by this TrafficShaping handler.
Some configuration methods 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 those methods are to be used not too often, accordingly to the traffic shaping configuration.
release()once this handler is not needed anymore to release all internal resources. This will not shutdown theEventExecutoras it may be shared, so you need to do this by your own.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static classGlobalTrafficShapingHandler.PerChannelprivate static classGlobalTrafficShapingHandler.ToSend-
Nested classes/interfaces inherited from class io.netty.handler.traffic.AbstractTrafficShapingHandler
AbstractTrafficShapingHandler.ReopenReadTimerTask
-
Nested classes/interfaces inherited from interface io.netty.channel.ChannelHandler
ChannelHandler.Sharable
-
-
Field Summary
Fields Modifier and Type Field Description private java.util.concurrent.ConcurrentMap<java.lang.Integer,GlobalTrafficShapingHandler.PerChannel>channelQueuesAll queues per channel(package private) longmaxGlobalWriteSizeMax size in the list before proposing to stop writing new objects from next handlers for all channel (global)private java.util.concurrent.atomic.AtomicLongqueuesSizeGlobal queues size-
Fields inherited from class io.netty.handler.traffic.AbstractTrafficShapingHandler
CHANNEL_DEFAULT_USER_DEFINED_WRITABILITY_INDEX, checkInterval, DEFAULT_CHECK_INTERVAL, DEFAULT_MAX_SIZE, DEFAULT_MAX_TIME, GLOBAL_DEFAULT_USER_DEFINED_WRITABILITY_INDEX, GLOBALCHANNEL_DEFAULT_USER_DEFINED_WRITABILITY_INDEX, maxTime, maxWriteDelay, maxWriteSize, MINIMAL_WAIT, READ_SUSPENDED, REOPEN_TASK, trafficCounter, userDefinedWritabilityIndex
-
-
Constructor Summary
Constructors Constructor Description GlobalTrafficShapingHandler(EventExecutor executor)Create a new instance using default Check Interval value of 1000 ms and default max time as delay allowed value of 15000 ms and no limit.GlobalTrafficShapingHandler(java.util.concurrent.ScheduledExecutorService executor, long checkInterval)Create a new instance using default max time as delay allowed value of 15000 ms and no limit.GlobalTrafficShapingHandler(java.util.concurrent.ScheduledExecutorService executor, long writeLimit, long readLimit)Create a new instance using default Check Interval value of 1000 ms and default max time as delay allowed value of 15000 ms.GlobalTrafficShapingHandler(java.util.concurrent.ScheduledExecutorService executor, long writeLimit, long readLimit, long checkInterval)Create a new instance using default max time as delay allowed value of 15000 ms.GlobalTrafficShapingHandler(java.util.concurrent.ScheduledExecutorService executor, long writeLimit, long readLimit, long checkInterval, long maxTime)Create a new instance.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description (package private) longcheckWaitReadTime(ChannelHandlerContext ctx, long wait, long now)Method overridden in GTSH to take into account specific timer for the channel.(package private) voidcreateGlobalTrafficCounter(java.util.concurrent.ScheduledExecutorService executor)Create the global TrafficCounter.longgetMaxGlobalWriteSize()private GlobalTrafficShapingHandler.PerChannelgetOrSetPerChannel(ChannelHandlerContext ctx)voidhandlerAdded(ChannelHandlerContext ctx)Do nothing by default, sub-classes may override this method.voidhandlerRemoved(ChannelHandlerContext ctx)Do nothing by default, sub-classes may override this method.(package private) voidinformReadOperation(ChannelHandlerContext ctx, long now)Method overridden in GTSH to take into account specific timer for the channel.longqueuesSize()voidrelease()Release all internal resources of this instance.private voidsendAllValid(ChannelHandlerContext ctx, GlobalTrafficShapingHandler.PerChannel perChannel, long now)voidsetMaxGlobalWriteSize(long maxGlobalWriteSize)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.(package private) voidsubmitWrite(ChannelHandlerContext ctx, java.lang.Object msg, long size, long writedelay, long now, ChannelPromise promise)protected intuserDefinedWritabilityIndex()-
Methods inherited from class io.netty.handler.traffic.AbstractTrafficShapingHandler
calculateSize, channelRead, channelRegistered, checkWriteSuspend, configure, configure, configure, doAccounting, getCheckInterval, getMaxTimeWait, getMaxWriteDelay, getMaxWriteSize, getReadLimit, getWriteLimit, isHandlerActive, read, releaseReadSuspended, releaseWriteSuspended, setCheckInterval, setMaxTimeWait, setMaxWriteDelay, setMaxWriteSize, setReadLimit, setTrafficCounter, setUserDefinedWritability, setWriteLimit, submitWrite, toString, trafficCounter, write
-
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, isSharable
-
-
-
-
Field Detail
-
channelQueues
private final java.util.concurrent.ConcurrentMap<java.lang.Integer,GlobalTrafficShapingHandler.PerChannel> channelQueues
All queues per channel
-
queuesSize
private final java.util.concurrent.atomic.AtomicLong queuesSize
Global queues size
-
maxGlobalWriteSize
long maxGlobalWriteSize
Max size in the list before proposing to stop writing new objects from next handlers for all channel (global)
-
-
Constructor Detail
-
GlobalTrafficShapingHandler
public GlobalTrafficShapingHandler(java.util.concurrent.ScheduledExecutorService executor, long writeLimit, long readLimit, long checkInterval, long maxTime)Create a new instance.- Parameters:
executor- theScheduledExecutorServiceto use for theTrafficCounter.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.
-
GlobalTrafficShapingHandler
public GlobalTrafficShapingHandler(java.util.concurrent.ScheduledExecutorService executor, long writeLimit, long readLimit, long checkInterval)Create a new instance using default max time as delay allowed value of 15000 ms.- Parameters:
executor- theScheduledExecutorServiceto use for theTrafficCounter.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.
-
GlobalTrafficShapingHandler
public GlobalTrafficShapingHandler(java.util.concurrent.ScheduledExecutorService executor, long writeLimit, long readLimit)Create a new instance using default Check Interval value of 1000 ms and default max time as delay allowed value of 15000 ms.- Parameters:
executor- theScheduledExecutorServiceto use for theTrafficCounter.writeLimit- 0 or a limit in bytes/sreadLimit- 0 or a limit in bytes/s
-
GlobalTrafficShapingHandler
public GlobalTrafficShapingHandler(java.util.concurrent.ScheduledExecutorService executor, long checkInterval)Create a new instance using default max time as delay allowed value of 15000 ms and no limit.- Parameters:
executor- theScheduledExecutorServiceto use for theTrafficCounter.checkInterval- The delay between two computations of performances for channels or 0 if no stats are to be computed.
-
GlobalTrafficShapingHandler
public GlobalTrafficShapingHandler(EventExecutor executor)
Create a new instance using default Check Interval value of 1000 ms and default max time as delay allowed value of 15000 ms and no limit.- Parameters:
executor- theScheduledExecutorServiceto use for theTrafficCounter.
-
-
Method Detail
-
createGlobalTrafficCounter
void createGlobalTrafficCounter(java.util.concurrent.ScheduledExecutorService executor)
Create the global TrafficCounter.
-
userDefinedWritabilityIndex
protected int userDefinedWritabilityIndex()
- Overrides:
userDefinedWritabilityIndexin classAbstractTrafficShapingHandler- Returns:
- the index to be used by the TrafficShapingHandler to manage the user defined writability. For Channel TSH it is defined as 1, for Global TSH it is defined as 2, for GlobalChannel TSH it is defined as 3.
-
getMaxGlobalWriteSize
public long getMaxGlobalWriteSize()
- Returns:
- the maxGlobalWriteSize default value being 400 MB.
-
setMaxGlobalWriteSize
public void setMaxGlobalWriteSize(long maxGlobalWriteSize)
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:
maxGlobalWriteSize- the maximum Global Write Size allowed in the buffer globally for all channels before write suspended is set, default value being 400 MB.
-
queuesSize
public long queuesSize()
- Returns:
- the global size of the buffers for all queues.
-
release
public final void release()
Release all internal resources of this instance.
-
getOrSetPerChannel
private GlobalTrafficShapingHandler.PerChannel getOrSetPerChannel(ChannelHandlerContext ctx)
-
handlerAdded
public void handlerAdded(ChannelHandlerContext ctx) throws java.lang.Exception
Description copied from class:ChannelHandlerAdapterDo nothing by default, sub-classes may override this method.- Specified by:
handlerAddedin interfaceChannelHandler- Overrides:
handlerAddedin classChannelHandlerAdapter- Throws:
java.lang.Exception
-
handlerRemoved
public void handlerRemoved(ChannelHandlerContext ctx) throws java.lang.Exception
Description copied from class:ChannelHandlerAdapterDo nothing by default, sub-classes may override this method.- Specified by:
handlerRemovedin interfaceChannelHandler- Overrides:
handlerRemovedin classAbstractTrafficShapingHandler- Throws:
java.lang.Exception
-
checkWaitReadTime
long checkWaitReadTime(ChannelHandlerContext ctx, long wait, long now)
Description copied from class:AbstractTrafficShapingHandlerMethod overridden in GTSH to take into account specific timer for the channel.- Overrides:
checkWaitReadTimein classAbstractTrafficShapingHandlerwait- 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)
Description copied from class:AbstractTrafficShapingHandlerMethod overridden in GTSH to take into account specific timer for the channel.- Overrides:
informReadOperationin classAbstractTrafficShapingHandlernow- the relative now time in ms
-
submitWrite
void submitWrite(ChannelHandlerContext ctx, java.lang.Object msg, long size, long writedelay, long now, ChannelPromise promise)
- Specified by:
submitWritein classAbstractTrafficShapingHandler
-
sendAllValid
private void sendAllValid(ChannelHandlerContext ctx, GlobalTrafficShapingHandler.PerChannel perChannel, long now)
-
-