Class GlobalTrafficShapingHandler
- All Implemented Interfaces:
ChannelHandler, ChannelInboundHandler, ChannelOutboundHandler
This implementation of the AbstractTrafficShapingHandler is 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 the EventExecutor as it may be shared, so you need to do this by your own.-
Nested Class Summary
Nested classes/interfaces inherited from interface ChannelHandler
ChannelHandler.SharableModifier and TypeInterfaceDescriptionstatic @interfaceIndicates that the same instance of the annotatedChannelHandlercan be added to one or moreChannelPipelines multiple times without a race condition. -
Field Summary
Fields inherited from class AbstractTrafficShapingHandler
checkInterval, DEFAULT_CHECK_INTERVAL, DEFAULT_MAX_TIME, maxTime, trafficCounterModifier and TypeFieldDescriptionprotected longDelay between two performance snapshotsstatic final longDefault delay between two checks: 1sstatic final longDefault max delay in case of traffic shaping (during which no communication will occur).protected longMax delay in waitprotected TrafficCounterTraffic Counter -
Constructor Summary
ConstructorsConstructorDescriptionGlobalTrafficShapingHandler(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(ScheduledExecutorService executor, long checkInterval) Create a new instance using default max time as delay allowed value of 15000 ms and no limit.GlobalTrafficShapingHandler(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(ScheduledExecutorService executor, long writeLimit, long readLimit, long checkInterval) Create a new instance using default max time as delay allowed value of 15000 ms.GlobalTrafficShapingHandler(ScheduledExecutorService executor, long writeLimit, long readLimit, long checkInterval, long maxTime) Create a new instance. -
Method Summary
Modifier and TypeMethodDescriptionlongvoidDo nothing by default, sub-classes may override this method.voidDo nothing by default, sub-classes may override this method.longfinal voidrelease()Release all internal resources of this instance.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.protected intMethods inherited from class AbstractTrafficShapingHandler
calculateSize, channelRead, channelRegistered, configure, configure, configure, doAccounting, getCheckInterval, getMaxTimeWait, getMaxWriteDelay, getMaxWriteSize, getReadLimit, getWriteLimit, isHandlerActive, read, setCheckInterval, setMaxTimeWait, setMaxWriteDelay, setMaxWriteSize, setReadLimit, setWriteLimit, submitWrite, toString, trafficCounter, writeModifier and TypeMethodDescriptionprotected longcalculateSize(Object msg) Calculate the size of the givenObject.voidchannelRead(ChannelHandlerContext ctx, Object msg) CallsChannelHandlerContext.fireChannelRead(Object)to forward to the nextChannelInboundHandlerin theChannelPipeline.voidCallsChannelHandlerContext.fireChannelRegistered()to forward to the nextChannelInboundHandlerin theChannelPipeline.voidconfigure(long newCheckInterval) Change the check interval.voidconfigure(long newWriteLimit, long newReadLimit) Change the underlying limitations.voidconfigure(long newWriteLimit, long newReadLimit, long newCheckInterval) Change the underlying limitations and check interval.protected voiddoAccounting(TrafficCounter counter) Called each time the accounting is computed from the TrafficCounters.longlonglonglonglonglongprotected static booleanvoidCallsChannelHandlerContext.read()to forward to the nextChannelOutboundHandlerin theChannelPipeline.voidsetCheckInterval(long checkInterval) voidsetMaxTimeWait(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.voidsetMaxWriteDelay(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.voidsetMaxWriteSize(long maxWriteSize) Note that this limit is a best effort on memory limitation to prevent Out Of Memory Exception.voidsetReadLimit(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.voidsetWriteLimit(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 voidsubmitWrite(ChannelHandlerContext ctx, Object msg, long delay, ChannelPromise promise) Deprecated.toString()voidwrite(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) CallsChannelOutboundInvoker.write(Object, ChannelPromise)to forward to the nextChannelOutboundHandlerin theChannelPipeline.Methods inherited from class ChannelDuplexHandler
bind, close, connect, deregister, disconnect, flushModifier and TypeMethodDescriptionvoidbind(ChannelHandlerContext ctx, SocketAddress localAddress, ChannelPromise promise) CallsChannelOutboundInvoker.bind(SocketAddress, ChannelPromise)to forward to the nextChannelOutboundHandlerin theChannelPipeline.voidclose(ChannelHandlerContext ctx, ChannelPromise promise) CallsChannelOutboundInvoker.close(ChannelPromise)to forward to the nextChannelOutboundHandlerin theChannelPipeline.voidconnect(ChannelHandlerContext ctx, SocketAddress remoteAddress, SocketAddress localAddress, ChannelPromise promise) CallsChannelOutboundInvoker.connect(SocketAddress, SocketAddress, ChannelPromise)to forward to the nextChannelOutboundHandlerin theChannelPipeline.voidderegister(ChannelHandlerContext ctx, ChannelPromise promise) CallsChannelOutboundInvoker.deregister(ChannelPromise)to forward to the nextChannelOutboundHandlerin theChannelPipeline.voiddisconnect(ChannelHandlerContext ctx, ChannelPromise promise) CallsChannelOutboundInvoker.disconnect(ChannelPromise)to forward to the nextChannelOutboundHandlerin theChannelPipeline.voidCallsChannelHandlerContext.flush()to forward to the nextChannelOutboundHandlerin theChannelPipeline.Methods inherited from class ChannelInboundHandlerAdapter
channelActive, channelInactive, channelReadComplete, channelUnregistered, channelWritabilityChanged, exceptionCaught, userEventTriggeredModifier and TypeMethodDescriptionvoidCallsChannelHandlerContext.fireChannelActive()to forward to the nextChannelInboundHandlerin theChannelPipeline.voidCallsChannelHandlerContext.fireChannelInactive()to forward to the nextChannelInboundHandlerin theChannelPipeline.voidCallsChannelHandlerContext.fireChannelReadComplete()to forward to the nextChannelInboundHandlerin theChannelPipeline.voidCallsChannelHandlerContext.fireChannelUnregistered()to forward to the nextChannelInboundHandlerin theChannelPipeline.voidCallsChannelHandlerContext.fireChannelWritabilityChanged()to forward to the nextChannelInboundHandlerin theChannelPipeline.voidexceptionCaught(ChannelHandlerContext ctx, Throwable cause) CallsChannelHandlerContext.fireExceptionCaught(Throwable)to forward to the nextChannelHandlerin theChannelPipeline.voiduserEventTriggered(ChannelHandlerContext ctx, Object evt) CallsChannelHandlerContext.fireUserEventTriggered(Object)to forward to the nextChannelInboundHandlerin theChannelPipeline.Methods inherited from class ChannelHandlerAdapter
ensureNotSharable, isSharableModifier and TypeMethodDescriptionprotected voidbooleanReturntrueif the implementation isChannelHandler.Sharableand so can be added to differentChannelPipelines.
-
Constructor Details
-
GlobalTrafficShapingHandler
public GlobalTrafficShapingHandler(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(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(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
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
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- theEventExecutorto use for theTrafficCounter.
-
-
Method Details
-
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. -
handlerAdded
Description copied from class:ChannelHandlerAdapterDo nothing by default, sub-classes may override this method.- Specified by:
handlerAddedin interfaceChannelHandler- Overrides:
handlerAddedin classChannelHandlerAdapter- Throws:
Exception
-
handlerRemoved
Description copied from class:ChannelHandlerAdapterDo nothing by default, sub-classes may override this method.- Specified by:
handlerRemovedin interfaceChannelHandler- Overrides:
handlerRemovedin classAbstractTrafficShapingHandler- Throws:
Exception
-