public class ChannelTrafficShapingHandler extends AbstractTrafficShapingHandler
This implementation of the AbstractTrafficShapingHandler
is for channel
traffic shaping, that is to say a per channel limitation of the bandwidth.
Add in your pipeline a new ChannelTrafficShapingHandler, before a recommended ExecutionHandler
(like
OrderedMemoryAwareThreadPoolExecutor
or MemoryAwareThreadPoolExecutor
).
ChannelTrafficShapingHandler myHandler = new ChannelTrafficShapingHandler(timer);
timer could be created using HashedWheelTimer
pipeline.addLast("CHANNEL_TRAFFIC_SHAPING", 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. For instance, if you have a
ChannelPipelineFactory
, you should create a new ChannelTrafficShapingHandler in this
ChannelPipelineFactory
each time getPipeline() method is called.
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
channelInterestChanged(ctx, event)
to handle writability, or through
future.addListener(new ChannelFutureListener())
on the future returned by
channel.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.AbstractTrafficShapingHandler.SimpleObjectSizeEstimator
ChannelHandler.Sharable
checkInterval, DEFAULT_CHECK_INTERVAL, DEFAULT_MAX_TIME, maxTime, timer, trafficCounter
Constructor and Description |
---|
ChannelTrafficShapingHandler(ObjectSizeEstimator objectSizeEstimator,
Timer timer) |
ChannelTrafficShapingHandler(ObjectSizeEstimator objectSizeEstimator,
Timer timer,
long checkInterval) |
ChannelTrafficShapingHandler(ObjectSizeEstimator objectSizeEstimator,
Timer timer,
long writeLimit,
long readLimit) |
ChannelTrafficShapingHandler(ObjectSizeEstimator objectSizeEstimator,
Timer timer,
long writeLimit,
long readLimit,
long checkInterval) |
ChannelTrafficShapingHandler(ObjectSizeEstimator objectSizeEstimator,
Timer timer,
long writeLimit,
long readLimit,
long checkInterval,
long maxTime) |
ChannelTrafficShapingHandler(Timer timer) |
ChannelTrafficShapingHandler(Timer timer,
long checkInterval) |
ChannelTrafficShapingHandler(Timer timer,
long writeLimit,
long readLimit) |
ChannelTrafficShapingHandler(Timer timer,
long writeLimit,
long readLimit,
long checkInterval) |
ChannelTrafficShapingHandler(Timer timer,
long writeLimit,
long readLimit,
long checkInterval,
long maxTime) |
Modifier and Type | Method and Description |
---|---|
void |
channelClosed(ChannelHandlerContext ctx,
ChannelStateEvent e)
Invoked when a
Channel was closed and all its related resources
were released. |
void |
channelConnected(ChannelHandlerContext ctx,
ChannelStateEvent e)
Invoked when a
Channel is open, bound to a local address, and
connected to a remote address. |
long |
queueSize() |
void |
releaseExternalResources()
Releases the external resources that this object depends on.
|
calculateSize, configure, configure, configure, doAccounting, getCheckInterval, getMaxTimeWait, getMaxWriteDelay, getMaxWriteSize, getReadLimit, getTrafficCounter, getWriteLimit, internalSubmitWrite, messageReceived, setCheckInterval, setMaxTimeWait, setMaxWriteDelay, setMaxWriteSize, setReadLimit, setWriteLimit, submitWrite, toString, writeRequested
bindRequested, channelBound, channelDisconnected, channelInterestChanged, channelOpen, channelUnbound, childChannelClosed, childChannelOpen, closeRequested, connectRequested, disconnectRequested, exceptionCaught, handleDownstream, handleUpstream, setInterestOpsRequested, unbindRequested, writeComplete
public ChannelTrafficShapingHandler(Timer timer, long writeLimit, long readLimit, long checkInterval)
public ChannelTrafficShapingHandler(Timer timer, long writeLimit, long readLimit, long checkInterval, long maxTime)
public ChannelTrafficShapingHandler(Timer timer, long writeLimit, long readLimit)
public ChannelTrafficShapingHandler(Timer timer, long checkInterval)
public ChannelTrafficShapingHandler(Timer timer)
public ChannelTrafficShapingHandler(ObjectSizeEstimator objectSizeEstimator, Timer timer, long writeLimit, long readLimit, long checkInterval)
public ChannelTrafficShapingHandler(ObjectSizeEstimator objectSizeEstimator, Timer timer, long writeLimit, long readLimit, long checkInterval, long maxTime)
public ChannelTrafficShapingHandler(ObjectSizeEstimator objectSizeEstimator, Timer timer, long writeLimit, long readLimit)
public ChannelTrafficShapingHandler(ObjectSizeEstimator objectSizeEstimator, Timer timer, long checkInterval)
public ChannelTrafficShapingHandler(ObjectSizeEstimator objectSizeEstimator, Timer timer)
public long queueSize()
public void channelClosed(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception
SimpleChannelHandler
Channel
was closed and all its related resources
were released.channelClosed
in class SimpleChannelHandler
Exception
public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception
SimpleChannelHandler
Channel
is open, bound to a local address, and
connected to a remote address.channelConnected
in class AbstractTrafficShapingHandler
Exception
public void releaseExternalResources()
ExternalResourceReleasable
releaseExternalResources
in interface ExternalResourceReleasable
releaseExternalResources
in class AbstractTrafficShapingHandler
Copyright © 2008-2016 The Netty Project. All Rights Reserved.