The main goal of this package is to allow you to shape the traffic (bandwidth limitation), but also to get statistics on how many bytes are read or written. Both functions can be active or inactive (traffic or statistics).
Two classes implement this behavior:
TrafficCounter: this class implements the counters needed by the handlers. It can be accessed to get some extra information like the read or write bytes since last check, the read and write bandwidth from last check...
AbstractTrafficShapingHandler: this abstract class implements the kernel of traffic shaping. It could be extended to fit your needs. Two classes are proposed as default implementations: see
GlobalTrafficShapingHandlerrespectively for Channel traffic shaping and global traffic shaping.
Both inbound and outbound traffic can be shaped independently. This is done by either passing in
the desired limiting values to the constructors of both the Channel and Global traffic shaping handlers,
or by calling the configure method on the
A value of 0 for either parameter indicates that there should be no limitation. This allows you to monitor the
incoming and outgoing traffic without shaping.
To activate or deactivate the statistics, you can adjust the delay to a low (suggested not less than 200ms for efficiency reasons) or a high value (let say 24H in millisecond is huge enough to not get the problem) or even using 0 which means no computation will be done.
If you want to do anything with these statistics, just override the doAccounting method.
This interval can be changed either from the method configure in
AbstractTrafficShapingHandler or directly using the method configure
Note also that you can create different GlobalTrafficShapingHandler if you want to separate classes of channels (for instance either from business point of view or from bind address point of view).
Class Summary Class Description AbstractTrafficShapingHandler ChannelTrafficShapingHandlerThis implementation of the
AbstractTrafficShapingHandleris for channel traffic shaping, that is to say a per channel limitation of the bandwidth.
GlobalChannelTrafficShapingHandlerThis implementation of the
AbstractTrafficShapingHandleris 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
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.
GlobalTrafficShapingHandlerThis implementation of the
AbstractTrafficShapingHandleris for global traffic shaping, that is to say a global limitation of the bandwidth, whatever the number of opened channels.
TrafficCounterCounts the number of read and written bytes for rate-limiting traffic.