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.
channel.isWritable() and
channelWritabilityChanged(ctx) to handle writability, or through
future.addListener(new GenericFutureListener()) on the future returned by
ctx.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.
| Modifier and Type | Class and Description |
|---|---|
| private static class |
| Modifier and Type | Field and Description |
|---|---|
| private final ArrayDeque | |
| private long |
| Access | Constructor and Description |
|---|---|
| public | ChannelTrafficShapingHandler(long
0 or a limit in bytes/s writeLimit, long 0 or a limit in bytes/s readLimit, long The delay between two computations of performances for
channels or 0 if no stats are to be computed. checkInterval, long The maximum delay to wait in case of traffic excess. maxTime)Create a new instance. |
| public | ChannelTrafficShapingHandler(long
0 or a limit in bytes/s writeLimit, long 0 or a limit in bytes/s readLimit, long The delay between two computations of performances for
channels or 0 if no stats are to be computed. checkInterval)Create a new instance using default max time as delay allowed value of 15000 ms. |
| public | ChannelTrafficShapingHandler(long
0 or a limit in bytes/s writeLimit, long 0 or a limit in bytes/s readLimit)Create a new instance using default Check Interval value of 1000 ms and max time as delay allowed value of 15000 ms. |
| public | ChannelTrafficShapingHandler(long
The delay between two computations of performances for
channels or 0 if no stats are to be computed. checkInterval)Create a new instance using default max time as delay allowed value of 15000 ms and no limit. |
| Modifier and Type | Method and Description |
|---|---|
| public void | handlerAdded(ChannelHandlerContext ctx)
Overrides io. Implements io. ChannelHandler was added to the actual context and it's ready to handle events.
|
| public void | handlerRemoved(ChannelHandlerContext ctx)
Overrides io. Implements io. ChannelHandler was removed from the actual context and it doesn't handle events
anymore.
|
| public long | |
| private void | |
| pack-priv void | submitWrite(final ChannelHandlerContext ctx, final Object msg, final long size, final long delay, final long now, final ChannelPromise promise)
Implements abstract io. |
| messagesQueue | back to summary |
|---|---|
| private final ArrayDeque<ChannelTrafficShapingHandler. | |
| queueSize | back to summary |
|---|---|
| private long queueSize | |
| ChannelTrafficShapingHandler | back to summary |
|---|---|
| public ChannelTrafficShapingHandler(long writeLimit, long readLimit, long checkInterval, long maxTime) Create a new instance.
| |
| ChannelTrafficShapingHandler | back to summary |
|---|---|
| public ChannelTrafficShapingHandler(long writeLimit, long readLimit, long checkInterval) Create a new instance using default max time as delay allowed value of 15000 ms.
| |
| ChannelTrafficShapingHandler | back to summary |
|---|---|
| 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.
| |
| ChannelTrafficShapingHandler | back to summary |
|---|---|
| public ChannelTrafficShapingHandler(long checkInterval) Create a new instance using default max time as delay allowed value of 15000 ms and no limit.
| |
| handlerAdded | back to summary |
|---|---|
| public void handlerAdded(ChannelHandlerContext ctx) throws Exception Overrides io. Implements io. Doc from io. Gets called after the
| |
| handlerRemoved | back to summary |
|---|---|
| public void handlerRemoved(ChannelHandlerContext ctx) throws Exception Overrides io. Implements io. Doc from io. Gets called after the
| |
| queueSize | back to summary |
|---|---|
public long queueSize()
| |
| sendAllValid | back to summary |
|---|---|
| private void sendAllValid(final ChannelHandlerContext ctx, final long now) | |
| submitWrite | back to summary |
|---|---|
| pack-priv void submitWrite(final ChannelHandlerContext ctx, final Object msg, final long size, final long delay, final long now, final ChannelPromise promise) Implements abstract io.
| |
| Modifier and Type | Field and Description |
|---|---|
| pack-priv final ChannelPromise | |
| pack-priv final long | |
| pack-priv final Object |
| Access | Constructor and Description |
|---|---|
| private |
| promise | back to summary |
|---|---|
| pack-priv final ChannelPromise promise | |
| relativeTimeAction | back to summary |
|---|---|
| pack-priv final long relativeTimeAction | |
| toSend | back to summary |
|---|---|
| pack-priv final Object toSend | |
| ToSend | back to summary |
|---|---|
| private ToSend(final long delay, final Object toSend, final ChannelPromise promise) | |