Class ChannelTrafficShapingHandler
- All Implemented Interfaces:
ChannelHandler
,ChannelInboundHandler
,ChannelOutboundHandler
This implementation of the AbstractTrafficShapingHandler
is for channel
traffic shaping, that is to say a per channel limitation of the bandwidth.
Note the index used in OutboundBuffer.setUserDefinedWritability(index, boolean)
is 1.
The general use should be as follow:
Add in your pipeline a new ChannelTrafficShapingHandler.
ChannelTrafficShapingHandler myHandler = new ChannelTrafficShapingHandler();
pipeline.addLast(myHandler);
Note that this handler has a Pipeline Coverage of "one" which means a new handler must be created for each new channel as the counter cannot 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.
-
Nested Class Summary
Nested ClassesNested 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
FieldsModifier and TypeFieldDescriptionprivate final ArrayDeque
<ChannelTrafficShapingHandler.ToSend> private long
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
ConstructorsConstructorDescriptionChannelTrafficShapingHandler
(long checkInterval) Create a new instance using default max time as delay allowed value of 15000 ms and no limit.ChannelTrafficShapingHandler
(long writeLimit, long readLimit) Create a new instance using default Check Interval value of 1000 ms and max time as delay allowed value of 15000 ms.ChannelTrafficShapingHandler
(long writeLimit, long readLimit, long checkInterval) Create a new instance using default max time as delay allowed value of 15000 ms.ChannelTrafficShapingHandler
(long writeLimit, long readLimit, long checkInterval, long maxTime) Create a new instance. -
Method Summary
Modifier and TypeMethodDescriptionvoid
Do nothing by default, sub-classes may override this method.void
Do nothing by default, sub-classes may override this method.long
private void
sendAllValid
(ChannelHandlerContext ctx, long now) (package private) void
submitWrite
(ChannelHandlerContext ctx, Object msg, long size, long delay, long now, ChannelPromise promise) Methods inherited from class io.netty.handler.traffic.AbstractTrafficShapingHandler
calculateSize, channelRead, channelRegistered, checkWaitReadTime, checkWriteSuspend, configure, configure, configure, doAccounting, getCheckInterval, getMaxTimeWait, getMaxWriteDelay, getMaxWriteSize, getReadLimit, getWriteLimit, informReadOperation, isHandlerActive, read, releaseReadSuspended, releaseWriteSuspended, setCheckInterval, setMaxTimeWait, setMaxWriteDelay, setMaxWriteSize, setReadLimit, setTrafficCounter, setUserDefinedWritability, setWriteLimit, submitWrite, toString, trafficCounter, userDefinedWritabilityIndex, 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 Details
-
messagesQueue
-
queueSize
private long queueSize
-
-
Constructor Details
-
ChannelTrafficShapingHandler
public ChannelTrafficShapingHandler(long writeLimit, long readLimit, long checkInterval, long maxTime) Create a new instance.- 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.
-
ChannelTrafficShapingHandler
public ChannelTrafficShapingHandler(long writeLimit, long readLimit, long checkInterval) Create a new instance using default max time as delay allowed value of 15000 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.
-
ChannelTrafficShapingHandler
public ChannelTrafficShapingHandler(long writeLimit, long readLimit) Create a new instance using default Check Interval value of 1000 ms and max time as delay allowed value of 15000 ms.- Parameters:
writeLimit
- 0 or a limit in bytes/sreadLimit
- 0 or a limit in bytes/s
-
ChannelTrafficShapingHandler
public ChannelTrafficShapingHandler(long checkInterval) Create a new instance using default max time as delay allowed value of 15000 ms and no limit.- Parameters:
checkInterval
- The delay between two computations of performances for channels or 0 if no stats are to be computed.
-
-
Method Details
-
handlerAdded
Description copied from class:ChannelHandlerAdapter
Do nothing by default, sub-classes may override this method.- Specified by:
handlerAdded
in interfaceChannelHandler
- Overrides:
handlerAdded
in classChannelHandlerAdapter
- Throws:
Exception
-
handlerRemoved
Description copied from class:ChannelHandlerAdapter
Do nothing by default, sub-classes may override this method.- Specified by:
handlerRemoved
in interfaceChannelHandler
- Overrides:
handlerRemoved
in classAbstractTrafficShapingHandler
- Throws:
Exception
-
submitWrite
void submitWrite(ChannelHandlerContext ctx, Object msg, long size, long delay, long now, ChannelPromise promise) - Specified by:
submitWrite
in classAbstractTrafficShapingHandler
-
sendAllValid
-
queueSize
public long queueSize()- Returns:
- current size in bytes of the write buffer.
-