Class GlobalChannelTrafficShapingHandler

All Implemented Interfaces:
ChannelHandler, ChannelInboundHandler, ChannelOutboundHandler

@Sharable public class GlobalChannelTrafficShapingHandler extends AbstractTrafficShapingHandler
This implementation of the AbstractTrafficShapingHandler is 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() 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.

Be sure to call 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.
  • Field Details

    • logger

      private static final InternalLogger logger
    • channelQueues

      All queues per channel
    • queuesSize

      private final AtomicLong queuesSize
      Global queues size
    • cumulativeWrittenBytes

      private final AtomicLong cumulativeWrittenBytes
      Maximum cumulative writing bytes for one channel among all (as long as channels stay the same)
    • cumulativeReadBytes

      private final AtomicLong cumulativeReadBytes
      Maximum cumulative read bytes for one channel among all (as long as channels stay the same)
    • maxGlobalWriteSize

      volatile long maxGlobalWriteSize
      Max size in the list before proposing to stop writing new objects from next handlers for all channel (global)
    • writeChannelLimit

      private volatile long writeChannelLimit
      Limit in B/s to apply to write
    • readChannelLimit

      private volatile long readChannelLimit
      Limit in B/s to apply to read
    • DEFAULT_DEVIATION

      private static final float DEFAULT_DEVIATION
      See Also:
    • MAX_DEVIATION

      private static final float MAX_DEVIATION
      See Also:
    • DEFAULT_SLOWDOWN

      private static final float DEFAULT_SLOWDOWN
      See Also:
    • DEFAULT_ACCELERATION

      private static final float DEFAULT_ACCELERATION
      See Also:
    • maxDeviation

      private volatile float maxDeviation
    • accelerationFactor

      private volatile float accelerationFactor
    • slowDownFactor

      private volatile float slowDownFactor
    • readDeviationActive

      private volatile boolean readDeviationActive
    • writeDeviationActive

      private volatile boolean writeDeviationActive
  • Constructor Details

    • GlobalChannelTrafficShapingHandler

      public GlobalChannelTrafficShapingHandler(ScheduledExecutorService executor, long writeGlobalLimit, long readGlobalLimit, long writeChannelLimit, long readChannelLimit, long checkInterval, long maxTime)
      Create a new instance.
      Parameters:
      executor - the ScheduledExecutorService to use for the TrafficCounter.
      writeGlobalLimit - 0 or a limit in bytes/s
      readGlobalLimit - 0 or a limit in bytes/s
      writeChannelLimit - 0 or a limit in bytes/s
      readChannelLimit - 0 or a limit in bytes/s
      checkInterval - 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(ScheduledExecutorService executor, long writeGlobalLimit, long readGlobalLimit, long writeChannelLimit, long readChannelLimit, long checkInterval)
      Create a new instance.
      Parameters:
      executor - the ScheduledExecutorService to use for the TrafficCounter.
      writeGlobalLimit - 0 or a limit in bytes/s
      readGlobalLimit - 0 or a limit in bytes/s
      writeChannelLimit - 0 or a limit in bytes/s
      readChannelLimit - 0 or a limit in bytes/s
      checkInterval - The delay between two computations of performances for channels or 0 if no stats are to be computed.
    • GlobalChannelTrafficShapingHandler

      public GlobalChannelTrafficShapingHandler(ScheduledExecutorService executor, long writeGlobalLimit, long readGlobalLimit, long writeChannelLimit, long readChannelLimit)
      Create a new instance.
      Parameters:
      executor - the ScheduledExecutorService to use for the TrafficCounter.
      writeGlobalLimit - 0 or a limit in bytes/s
      readGlobalLimit - 0 or a limit in bytes/s
      writeChannelLimit - 0 or a limit in bytes/s
      readChannelLimit - 0 or a limit in bytes/s
    • GlobalChannelTrafficShapingHandler

      public GlobalChannelTrafficShapingHandler(ScheduledExecutorService executor, long checkInterval)
      Create a new instance.
      Parameters:
      executor - the ScheduledExecutorService to use for the TrafficCounter.
      checkInterval - The delay between two computations of performances for channels or 0 if no stats are to be computed.
    • GlobalChannelTrafficShapingHandler

      public GlobalChannelTrafficShapingHandler(ScheduledExecutorService executor)
      Create a new instance.
      Parameters:
      executor - the ScheduledExecutorService to use for the TrafficCounter.
  • Method Details