@ChannelHandler.Sharable public class GlobalTrafficShapingHandler extends AbstractTrafficShapingHandler
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(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.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.ChannelHandler.Sharable
checkInterval, DEFAULT_CHECK_INTERVAL, DEFAULT_MAX_TIME, maxTime, trafficCounter
Constructor and Description |
---|
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.
|
GlobalTrafficShapingHandler(ScheduledExecutorService executor,
long checkInterval)
Create a new instance using
default max time as delay allowed value of 15000 ms and no limit.
|
GlobalTrafficShapingHandler(ScheduledExecutorService 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(ScheduledExecutorService executor,
long writeLimit,
long readLimit,
long checkInterval)
Create a new instance using
default max time as delay allowed value of 15000 ms.
|
GlobalTrafficShapingHandler(ScheduledExecutorService executor,
long writeLimit,
long readLimit,
long checkInterval,
long maxTime)
Create a new instance.
|
Modifier and Type | Method and Description |
---|---|
long |
getMaxGlobalWriteSize() |
void |
handlerAdded(ChannelHandlerContext ctx)
Do nothing by default, sub-classes may override this method.
|
void |
handlerRemoved(ChannelHandlerContext ctx)
Do nothing by default, sub-classes may override this method.
|
long |
queuesSize() |
void |
release()
Release all internal resources of this instance.
|
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.
|
protected int |
userDefinedWritabilityIndex() |
calculateSize, channelRead, channelRegistered, configure, configure, configure, doAccounting, getCheckInterval, getMaxTimeWait, getMaxWriteDelay, getMaxWriteSize, getReadLimit, getWriteLimit, isHandlerActive, read, setCheckInterval, setMaxTimeWait, setMaxWriteDelay, setMaxWriteSize, setReadLimit, setWriteLimit, submitWrite, toString, trafficCounter, write
bind, close, connect, deregister, disconnect, flush
channelActive, channelInactive, channelReadComplete, channelUnregistered, channelWritabilityChanged, exceptionCaught, userEventTriggered
ensureNotSharable, isSharable
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
exceptionCaught
public GlobalTrafficShapingHandler(ScheduledExecutorService executor, long writeLimit, long readLimit, long checkInterval, long maxTime)
executor
- the ScheduledExecutorService
to use for the TrafficCounter
.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.public GlobalTrafficShapingHandler(ScheduledExecutorService executor, long writeLimit, long readLimit, long checkInterval)
executor
- the ScheduledExecutorService
to use for the TrafficCounter
.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.public GlobalTrafficShapingHandler(ScheduledExecutorService executor, long writeLimit, long readLimit)
executor
- the ScheduledExecutorService
to use for the TrafficCounter
.writeLimit
- 0 or a limit in bytes/sreadLimit
- 0 or a limit in bytes/spublic GlobalTrafficShapingHandler(ScheduledExecutorService executor, long checkInterval)
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.public GlobalTrafficShapingHandler(EventExecutor executor)
executor
- the ScheduledExecutorService
to use for the TrafficCounter
.protected int userDefinedWritabilityIndex()
userDefinedWritabilityIndex
in class AbstractTrafficShapingHandler
public long getMaxGlobalWriteSize()
public void setMaxGlobalWriteSize(long maxGlobalWriteSize)
maxGlobalWriteSize
- the maximum Global Write Size allowed in the buffer
globally for all channels before write suspended is set,
default value being 400 MB.public long queuesSize()
public final void release()
public void handlerAdded(ChannelHandlerContext ctx) throws Exception
ChannelHandlerAdapter
handlerAdded
in interface ChannelHandler
handlerAdded
in class ChannelHandlerAdapter
Exception
public void handlerRemoved(ChannelHandlerContext ctx) throws Exception
ChannelHandlerAdapter
handlerRemoved
in interface ChannelHandler
handlerRemoved
in class ChannelHandlerAdapter
Exception
Copyright © 2008–2017 The Netty Project. All rights reserved.