- java.lang.Object
-
- io.netty5.handler.traffic.AbstractTrafficShapingHandler
-
- io.netty5.handler.traffic.GlobalChannelTrafficShapingHandler
-
- All Implemented Interfaces:
ChannelHandler
public class GlobalChannelTrafficShapingHandler extends AbstractTrafficShapingHandler
This implementation of theAbstractTrafficShapingHandleris for global and per channel traffic shaping, that is to say a global limitation of the bandwidth, whatever the number of opened channels and a per channel limitation of the bandwidth.
This version shall not be in the same pipeline than other TrafficShapingHandler.
The general use should be as follow:
- Create your unique GlobalChannelTrafficShapingHandler like:
GlobalChannelTrafficShapingHandler myHandler = new GlobalChannelTrafficShapingHandler(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).
Note that as this is a fusion of both Global and Channel Traffic Shaping, limits are in 2 sets, respectively Global and Channel.
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(future -> ...)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.
Be sure to callrelease()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.
-
-
Field Summary
-
Fields inherited from class io.netty5.handler.traffic.AbstractTrafficShapingHandler
checkInterval, DEFAULT_CHECK_INTERVAL, DEFAULT_MAX_TIME, maxTime, trafficCounter
-
-
Constructor Summary
Constructors Constructor Description GlobalChannelTrafficShapingHandler(EventExecutorGroup executor)Create a new instance.GlobalChannelTrafficShapingHandler(EventExecutorGroup executor, long checkInterval)Create a new instance.GlobalChannelTrafficShapingHandler(EventExecutorGroup executor, long writeGlobalLimit, long readGlobalLimit, long writeChannelLimit, long readChannelLimit)Create a new instance.GlobalChannelTrafficShapingHandler(EventExecutorGroup executor, long writeGlobalLimit, long readGlobalLimit, long writeChannelLimit, long readChannelLimit, long checkInterval)Create a new instance.GlobalChannelTrafficShapingHandler(EventExecutorGroup executor, long writeGlobalLimit, long readGlobalLimit, long writeChannelLimit, long readChannelLimit, long checkInterval, long maxTime)Create a new instance.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description floataccelerationFactor()voidchannelRead(ChannelHandlerContext ctx, Object msg)Invoked when the currentChannelhas read a message from the peer.Collection<TrafficCounter>channelTrafficCounters()To allow for instance doAccounting to use the TrafficCounter per channel.protected longcheckWaitReadTime(ChannelHandlerContext ctx, long wait, long now)voidconfigureChannel(long newWriteLimit, long newReadLimit)protected voiddoAccounting(TrafficCounter counter)Called each time the accounting is computed from the TrafficCounters.longgetMaxGlobalWriteSize()longgetReadChannelLimit()longgetWriteChannelLimit()voidhandlerAdded(ChannelHandlerContext ctx)Gets called after theChannelHandlerwas added to the actual context and it's ready to handle events.voidhandlerRemoved(ChannelHandlerContext ctx)Gets called after theChannelHandlerwas removed from the actual context and it doesn't handle events anymore.protected voidinformReadOperation(ChannelHandlerContext ctx, long now)booleanisSharable()Returnstrueif this handler is sharable and thus can be added to more than oneChannelPipeline.floatmaxDeviation()protected longmaximumCumulativeReadBytes()protected longmaximumCumulativeWrittenBytes()longqueuesSize()voidrelease()Release all internal resources of this instance.voidsetMaxDeviation(float maxDeviation, float slowDownFactor, float accelerationFactor)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.voidsetReadChannelLimit(long readLimit)voidsetWriteChannelLimit(long writeLimit)floatslowDownFactor()protected voidsubmitWrite(ChannelHandlerContext ctx, Object msg, long size, long writedelay, long now, Promise<Void> promise)StringtoString()protected intuserDefinedWritabilityIndex()Future<Void>write(ChannelHandlerContext ctx, Object msg)Called once a write operation is made.-
Methods inherited from class io.netty5.handler.traffic.AbstractTrafficShapingHandler
calculateSize, channelRegistered, configure, configure, configure, getCheckInterval, getMaxTimeWait, getMaxWriteDelay, getMaxWriteSize, getReadLimit, getWriteLimit, isHandlerActive, read, setCheckInterval, setMaxTimeWait, setMaxWriteDelay, setMaxWriteSize, setReadLimit, setWriteLimit, submitWrite, trafficCounter
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface io.netty5.channel.ChannelHandler
bind, channelActive, channelExceptionCaught, channelInactive, channelInboundEvent, channelReadComplete, channelShutdown, channelUnregistered, channelWritabilityChanged, close, connect, deregister, disconnect, flush, pendingOutboundBytes, register, sendOutboundEvent, shutdown
-
-
-
-
Constructor Detail
-
GlobalChannelTrafficShapingHandler
public GlobalChannelTrafficShapingHandler(EventExecutorGroup executor, long writeGlobalLimit, long readGlobalLimit, long writeChannelLimit, long readChannelLimit, long checkInterval, long maxTime)
Create a new instance.- Parameters:
executor- theEventExecutorGroupto use for theTrafficCounter.writeGlobalLimit- 0 or a limit in bytes/sreadGlobalLimit- 0 or a limit in bytes/swriteChannelLimit- 0 or a limit in bytes/sreadChannelLimit- 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.
-
GlobalChannelTrafficShapingHandler
public GlobalChannelTrafficShapingHandler(EventExecutorGroup executor, long writeGlobalLimit, long readGlobalLimit, long writeChannelLimit, long readChannelLimit, long checkInterval)
Create a new instance.- Parameters:
executor- theEventExecutorGroupto use for theTrafficCounter.writeGlobalLimit- 0 or a limit in bytes/sreadGlobalLimit- 0 or a limit in bytes/swriteChannelLimit- 0 or a limit in bytes/sreadChannelLimit- 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.
-
GlobalChannelTrafficShapingHandler
public GlobalChannelTrafficShapingHandler(EventExecutorGroup executor, long writeGlobalLimit, long readGlobalLimit, long writeChannelLimit, long readChannelLimit)
Create a new instance.- Parameters:
executor- theEventExecutorGroupto use for theTrafficCounter.writeGlobalLimit- 0 or a limit in bytes/sreadGlobalLimit- 0 or a limit in bytes/swriteChannelLimit- 0 or a limit in bytes/sreadChannelLimit- 0 or a limit in bytes/s
-
GlobalChannelTrafficShapingHandler
public GlobalChannelTrafficShapingHandler(EventExecutorGroup executor, long checkInterval)
Create a new instance.- Parameters:
executor- theEventExecutorGroupto use for theTrafficCounter.checkInterval- The delay between two computations of performances for channels or 0 if no stats are to be computed.
-
GlobalChannelTrafficShapingHandler
public GlobalChannelTrafficShapingHandler(EventExecutorGroup executor)
Create a new instance.- Parameters:
executor- theEventExecutorGroupto use for theTrafficCounter.
-
-
Method Detail
-
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.
-
isSharable
public boolean isSharable()
Description copied from interface:ChannelHandlerReturnstrueif this handler is sharable and thus can be added to more than oneChannelPipeline. By default, this method returnsfalse. If this method returnsfalse, you have to create a new handler instance every time you add it to a pipeline because it has unshared state such as member variables.
-
maxDeviation
public float maxDeviation()
- Returns:
- the current max deviation
-
accelerationFactor
public float accelerationFactor()
- Returns:
- the current acceleration factor
-
slowDownFactor
public float slowDownFactor()
- Returns:
- the current slow down factor
-
setMaxDeviation
public void setMaxDeviation(float maxDeviation, float slowDownFactor, float accelerationFactor)- Parameters:
maxDeviation- the maximum deviation to allow during computation of average, default deviation being 0.1, so +/-10% of the desired bandwidth. Maximum being 0.4.slowDownFactor- the factor set as +x% to the too fast client (minimal value being 0, meaning no slow down factor), default being 40% (0.4).accelerationFactor- the factor set as -x% to the too slow client (maximal value being 0, meaning no acceleration factor), default being -10% (-0.1).
-
doAccounting
protected void doAccounting(TrafficCounter counter)
Description copied from class:AbstractTrafficShapingHandlerCalled each time the accounting is computed from the TrafficCounters. This method could be used for instance to implement almost real time accounting.- Overrides:
doAccountingin classAbstractTrafficShapingHandler- Parameters:
counter- the TrafficCounter that computes its performance
-
getMaxGlobalWriteSize
public long getMaxGlobalWriteSize()
- Returns:
- the maxGlobalWriteSize
-
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.
-
queuesSize
public long queuesSize()
- Returns:
- the global size of the buffers for all queues.
-
configureChannel
public void configureChannel(long newWriteLimit, long newReadLimit)- Parameters:
newWriteLimit- Channel write limitnewReadLimit- Channel read limit
-
getWriteChannelLimit
public long getWriteChannelLimit()
- Returns:
- Channel write limit
-
setWriteChannelLimit
public void setWriteChannelLimit(long writeLimit)
- Parameters:
writeLimit- Channel write limit
-
getReadChannelLimit
public long getReadChannelLimit()
- Returns:
- Channel read limit
-
setReadChannelLimit
public void setReadChannelLimit(long readLimit)
- Parameters:
readLimit- Channel read limit
-
release
public final void release()
Release all internal resources of this instance.
-
handlerAdded
public void handlerAdded(ChannelHandlerContext ctx) throws Exception
Description copied from interface:ChannelHandlerGets called after theChannelHandlerwas added to the actual context and it's ready to handle events.- Throws:
Exception
-
handlerRemoved
public void handlerRemoved(ChannelHandlerContext ctx) throws Exception
Description copied from interface:ChannelHandlerGets called after theChannelHandlerwas removed from the actual context and it doesn't handle events anymore.- Specified by:
handlerRemovedin interfaceChannelHandler- Overrides:
handlerRemovedin classAbstractTrafficShapingHandler- Throws:
Exception
-
channelRead
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception
Description copied from interface:ChannelHandlerInvoked when the currentChannelhas read a message from the peer.- Specified by:
channelReadin interfaceChannelHandler- Overrides:
channelReadin classAbstractTrafficShapingHandler- Throws:
Exception
-
checkWaitReadTime
protected long checkWaitReadTime(ChannelHandlerContext ctx, long wait, long now)
-
informReadOperation
protected void informReadOperation(ChannelHandlerContext ctx, long now)
-
maximumCumulativeWrittenBytes
protected long maximumCumulativeWrittenBytes()
-
maximumCumulativeReadBytes
protected long maximumCumulativeReadBytes()
-
channelTrafficCounters
public Collection<TrafficCounter> channelTrafficCounters()
To allow for instance doAccounting to use the TrafficCounter per channel.- Returns:
- the list of TrafficCounters that exists at the time of the call.
-
write
public Future<Void> write(ChannelHandlerContext ctx, Object msg)
Description copied from interface:ChannelHandlerCalled once a write operation is made. The write operation will write the messages through theChannelPipeline. Those are then ready to be flushed to the actualChannelonceChannel.flush()is called.- Specified by:
writein interfaceChannelHandler- Overrides:
writein classAbstractTrafficShapingHandler- Parameters:
ctx- theChannelHandlerContextfor which the write operation is mademsg- the message to write- Returns:
- the
Futurewhich will be notified once the operation completes.
-
submitWrite
protected void submitWrite(ChannelHandlerContext ctx, Object msg, long size, long writedelay, long now, Promise<Void> promise)
-
toString
public String toString()
- Overrides:
toStringin classAbstractTrafficShapingHandler
-
-