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.
channel.isWritable() and
channelWritabilityChanged(ctx) to handle writability, or through
future.addListener(future -> ...) 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.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.
| Modifier and Type | Class and Description |
|---|---|
| private static class | |
| private static class |
| Modifier and Type | Field and Description |
|---|---|
| private final ConcurrentMap | channelQueues
All queues per channel |
| pack-priv long | maxGlobalWriteSize
Max size in the list before proposing to stop writing new objects from next handlers for all channel (global) |
| private final AtomicLong | queuesSize
Global queues size |
| Access | Constructor and Description |
|---|---|
| public | GlobalTrafficShapingHandler(EventExecutorGroup
the executor, long EventExecutorGroup to use for the TrafficCounter.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 | GlobalTrafficShapingHandler(EventExecutorGroup
the executor, long EventExecutorGroup to use for the TrafficCounter.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 | GlobalTrafficShapingHandler(EventExecutorGroup
the executor, long EventExecutorGroup to use for the TrafficCounter.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 default max time as delay allowed value of 15000 ms. |
| public | GlobalTrafficShapingHandler(EventExecutorGroup
the executor, long EventExecutorGroup to use for the TrafficCounter.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. |
| public | GlobalTrafficShapingHandler(EventExecutor
the executor)EventExecutor to use for the TrafficCounter.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. |
| Modifier and Type | Method and Description |
|---|---|
| pack-priv long | checkWaitReadTime(final ChannelHandlerContext ctx, long
the wait delay computed in ms wait, final long the relative now time in ms now)Overrides io. |
| pack-priv void | |
| public long | |
| private GlobalTrafficShapingHandler. | |
| public void | handlerAdded(ChannelHandlerContext ctx)
Overrides default io. ChannelHandler was added to the actual context and it's ready to handle events.
|
| public void | handlerRemoved(ChannelHandlerContext ctx)
Overrides io. Overrides default io. ChannelHandler was removed from the actual context and it doesn't handle events
anymore.
|
| pack-priv void | informReadOperation(final ChannelHandlerContext ctx, final long
the relative now time in ms now)Overrides io. |
| public boolean | isSharable()
Overrides default io. true if this handler is sharable and thus can be added
to more than one ChannelPipeline.
|
| public long | |
| public final void | |
| private void | sendAllValid(final ChannelHandlerContext ctx, final GlobalTrafficShapingHandler.
|
| public void | setMaxGlobalWriteSize(long
the maximum Global Write Size allowed in the buffer
globally for all channels before write suspended is set,
default value being 400 MB. 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. |
| pack-priv void | submitWrite(final ChannelHandlerContext ctx, final Object msg, final long size, final long writedelay, final long now, final Promise<Void> promise)
Implements abstract io. |
| protected int |
| channelQueues | back to summary |
|---|---|
| private final ConcurrentMap<Integer, GlobalTrafficShapingHandler. All queues per channel | |
| maxGlobalWriteSize | back to summary |
|---|---|
| pack-priv long maxGlobalWriteSize Max size in the list before proposing to stop writing new objects from next handlers for all channel (global) | |
| queuesSize | back to summary |
|---|---|
| private final AtomicLong queuesSize Global queues size | |
| GlobalTrafficShapingHandler | back to summary |
|---|---|
| public GlobalTrafficShapingHandler(EventExecutorGroup executor, long writeLimit, long readLimit, long checkInterval, long maxTime) Create a new instance.
| |
| GlobalTrafficShapingHandler | back to summary |
|---|---|
| public GlobalTrafficShapingHandler(EventExecutorGroup executor, long writeLimit, long readLimit, long checkInterval) Create a new instance using default max time as delay allowed value of 15000 ms.
| |
| GlobalTrafficShapingHandler | back to summary |
|---|---|
| public GlobalTrafficShapingHandler(EventExecutorGroup 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 | back to summary |
|---|---|
| public GlobalTrafficShapingHandler(EventExecutorGroup executor, long checkInterval) Create a new instance using default max time as delay allowed value of 15000 ms and no limit.
| |
| GlobalTrafficShapingHandler | back to summary |
|---|---|
| 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.
| |
| checkWaitReadTime | back to summary |
|---|---|
| pack-priv long checkWaitReadTime(final ChannelHandlerContext ctx, long wait, final long now) Overrides io. Doc from io. Method overridden in GTSH to take into account specific timer for the channel.
| |
| createGlobalTrafficCounter | back to summary |
|---|---|
| pack-priv void createGlobalTrafficCounter(EventExecutorGroup executor) Create the global TrafficCounter. | |
| getMaxGlobalWriteSize | back to summary |
|---|---|
public long getMaxGlobalWriteSize()
| |
| getOrSetPerChannel | back to summary |
|---|---|
| private GlobalTrafficShapingHandler. | |
| handlerAdded | back to summary |
|---|---|
| public void handlerAdded(ChannelHandlerContext ctx) throws Exception Overrides default io. Doc from io. Gets called after the
| |
| handlerRemoved | back to summary |
|---|---|
| public void handlerRemoved(ChannelHandlerContext ctx) throws Exception Overrides io. Overrides default io. Doc from io. Gets called after the
| |
| informReadOperation | back to summary |
|---|---|
| pack-priv void informReadOperation(final ChannelHandlerContext ctx, final long now) Overrides io. Doc from io. Method overridden in GTSH to take into account specific timer for the channel.
| |
| isSharable | back to summary |
|---|---|
| public boolean isSharable() Overrides default io. Doc from io. Returns
| |
| queuesSize | back to summary |
|---|---|
public long queuesSize()
| |
| release | back to summary |
|---|---|
| public final void release() Release all internal resources of this instance. | |
| sendAllValid | back to summary |
|---|---|
| private void sendAllValid(final ChannelHandlerContext ctx, final GlobalTrafficShapingHandler. | |
| setMaxGlobalWriteSize | back to summary |
|---|---|
| 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.
| |
| submitWrite | back to summary |
|---|---|
| pack-priv void submitWrite(final ChannelHandlerContext ctx, final Object msg, final long size, final long writedelay, final long now, final Promise<Void> promise) Implements abstract io.
| |
| userDefinedWritabilityIndex | back to summary |
|---|---|
| protected int userDefinedWritabilityIndex() Overrides io.
| |
| Modifier and Type | Field and Description |
|---|---|
| pack-priv long | |
| pack-priv long | |
| pack-priv ArrayDeque | |
| pack-priv long |
| Access | Constructor and Description |
|---|---|
| private |
| lastReadTimestamp | back to summary |
|---|---|
| pack-priv long lastReadTimestamp | |
| lastWriteTimestamp | back to summary |
|---|---|
| pack-priv long lastWriteTimestamp | |
| messagesQueue | back to summary |
|---|---|
| pack-priv ArrayDeque<GlobalTrafficShapingHandler. | |
| queueSize | back to summary |
|---|---|
| pack-priv long queueSize | |
| PerChannel | back to summary |
|---|---|
| private PerChannel() | |
| Modifier and Type | Field and Description |
|---|---|
| pack-priv final Promise | |
| pack-priv final long | |
| pack-priv final long | |
| pack-priv final Object |
| Access | Constructor and Description |
|---|---|
| private |
| promise | back to summary |
|---|---|
| pack-priv final Promise<Void> promise | |
| relativeTimeAction | back to summary |
|---|---|
| pack-priv final long relativeTimeAction | |
| size | back to summary |
|---|---|
| pack-priv final long size | |
| toSend | back to summary |
|---|---|
| pack-priv final Object toSend | |
| ToSend | back to summary |
|---|---|
| private ToSend(final long delay, final Object toSend, final long size, final Promise<Void> promise) | |