Class ChannelTrafficShapingHandler
- java.lang.Object
-
- org.jboss.netty.channel.SimpleChannelHandler
-
- org.jboss.netty.handler.traffic.AbstractTrafficShapingHandler
-
- org.jboss.netty.handler.traffic.ChannelTrafficShapingHandler
-
- All Implemented Interfaces:
ChannelDownstreamHandler,ChannelHandler,ChannelUpstreamHandler,ExternalResourceReleasable
public class ChannelTrafficShapingHandler extends AbstractTrafficShapingHandler
This implementation of the
The general use should be as follow:AbstractTrafficShapingHandleris 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(likeOrderedMemoryAwareThreadPoolExecutororMemoryAwareThreadPoolExecutor).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 thisChannelPipelineFactoryeach 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.
- When you shutdown your application, release all the external resources (except the timer internal itself)
by calling:
myHandler.releaseExternalResources();
- In your handler, you should consider to use the
channel.isWritable()andchannelInterestChanged(ctx, event)to handle writability, or throughfuture.addListener(new ChannelFutureListener())on the future returned bychannel.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.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static classChannelTrafficShapingHandler.ToSend-
Nested classes/interfaces inherited from class org.jboss.netty.handler.traffic.AbstractTrafficShapingHandler
AbstractTrafficShapingHandler.ReadWriteStatus, AbstractTrafficShapingHandler.ReopenReadTimerTask, AbstractTrafficShapingHandler.SimpleObjectSizeEstimator
-
Nested classes/interfaces inherited from interface org.jboss.netty.channel.ChannelHandler
ChannelHandler.Sharable
-
-
Field Summary
Fields Modifier and Type Field Description private ChannelHandlerContextctxprivate java.util.List<ChannelTrafficShapingHandler.ToSend>messagesQueueprivate longqueueSizeprivate TimeoutwriteTimeout-
Fields inherited from class org.jboss.netty.handler.traffic.AbstractTrafficShapingHandler
CHANNEL_DEFAULT_USER_DEFINED_WRITABILITY_INDEX, checkInterval, DEFAULT_CHECK_INTERVAL, DEFAULT_MAX_SIZE, DEFAULT_MAX_TIME, GLOBAL_DEFAULT_USER_DEFINED_WRITABILITY_INDEX, GLOBALCHANNEL_DEFAULT_USER_DEFINED_WRITABILITY_INDEX, index, logger, maxTime, maxWriteDelay, maxWriteSize, MINIMAL_WAIT, release, timeout, timer, trafficCounter
-
-
Constructor Summary
Constructors Constructor 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)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidchannelClosed(ChannelHandlerContext ctx, ChannelStateEvent e)Invoked when aChannelwas closed and all its related resources were released.voidchannelConnected(ChannelHandlerContext ctx, ChannelStateEvent e)Invoked when aChannelis open, bound to a local address, and connected to a remote address.longqueueSize()voidreleaseExternalResources()Releases the external resources that this object depends on.private voidsendAllValid(ChannelHandlerContext ctx, long now)(package private) voidsubmitWrite(ChannelHandlerContext ctx, MessageEvent evt, long size, long delay, long now)-
Methods inherited from class org.jboss.netty.handler.traffic.AbstractTrafficShapingHandler
calculateSize, checkAttachment, checkWaitReadTime, checkWriteSuspend, configure, configure, configure, doAccounting, getCheckInterval, getMaxTimeWait, getMaxWriteDelay, getMaxWriteSize, getReadLimit, getTrafficCounter, getWriteLimit, informReadOperation, internalSubmitWrite, messageReceived, releaseReadSuspended, releaseWriteSuspended, setCheckInterval, setMaxTimeWait, setMaxWriteDelay, setMaxWriteSize, setReadLimit, setTrafficCounter, setWritable, setWriteLimit, submitWrite, toString, userDefinedWritabilityIndex, writeRequested
-
Methods inherited from class org.jboss.netty.channel.SimpleChannelHandler
bindRequested, channelBound, channelDisconnected, channelInterestChanged, channelOpen, channelUnbound, childChannelClosed, childChannelOpen, closeRequested, connectRequested, disconnectRequested, exceptionCaught, handleDownstream, handleUpstream, setInterestOpsRequested, unbindRequested, writeComplete
-
-
-
-
Field Detail
-
messagesQueue
private final java.util.List<ChannelTrafficShapingHandler.ToSend> messagesQueue
-
queueSize
private long queueSize
-
writeTimeout
private volatile Timeout writeTimeout
-
ctx
private volatile ChannelHandlerContext ctx
-
-
Constructor Detail
-
ChannelTrafficShapingHandler
public ChannelTrafficShapingHandler(Timer timer, long writeLimit, long readLimit, long checkInterval)
-
ChannelTrafficShapingHandler
public ChannelTrafficShapingHandler(Timer timer, long writeLimit, long readLimit, long checkInterval, long maxTime)
-
ChannelTrafficShapingHandler
public ChannelTrafficShapingHandler(Timer timer, long writeLimit, long readLimit)
-
ChannelTrafficShapingHandler
public ChannelTrafficShapingHandler(Timer timer, long checkInterval)
-
ChannelTrafficShapingHandler
public ChannelTrafficShapingHandler(Timer timer)
-
ChannelTrafficShapingHandler
public ChannelTrafficShapingHandler(ObjectSizeEstimator objectSizeEstimator, Timer timer, long writeLimit, long readLimit, long checkInterval)
-
ChannelTrafficShapingHandler
public ChannelTrafficShapingHandler(ObjectSizeEstimator objectSizeEstimator, Timer timer, long writeLimit, long readLimit, long checkInterval, long maxTime)
-
ChannelTrafficShapingHandler
public ChannelTrafficShapingHandler(ObjectSizeEstimator objectSizeEstimator, Timer timer, long writeLimit, long readLimit)
-
ChannelTrafficShapingHandler
public ChannelTrafficShapingHandler(ObjectSizeEstimator objectSizeEstimator, Timer timer, long checkInterval)
-
ChannelTrafficShapingHandler
public ChannelTrafficShapingHandler(ObjectSizeEstimator objectSizeEstimator, Timer timer)
-
-
Method Detail
-
submitWrite
void submitWrite(ChannelHandlerContext ctx, MessageEvent evt, long size, long delay, long now) throws java.lang.Exception
- Specified by:
submitWritein classAbstractTrafficShapingHandler- Throws:
java.lang.Exception
-
sendAllValid
private void sendAllValid(ChannelHandlerContext ctx, long now) throws java.lang.Exception
- Throws:
java.lang.Exception
-
queueSize
public long queueSize()
- Returns:
- current size in bytes of the write buffer.
-
channelClosed
public void channelClosed(ChannelHandlerContext ctx, ChannelStateEvent e) throws java.lang.Exception
Description copied from class:SimpleChannelHandlerInvoked when aChannelwas closed and all its related resources were released.- Overrides:
channelClosedin classSimpleChannelHandler- Throws:
java.lang.Exception
-
channelConnected
public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws java.lang.Exception
Description copied from class:SimpleChannelHandlerInvoked when aChannelis open, bound to a local address, and connected to a remote address.- Overrides:
channelConnectedin classAbstractTrafficShapingHandler- Throws:
java.lang.Exception
-
releaseExternalResources
public void releaseExternalResources()
Description copied from interface:ExternalResourceReleasableReleases the external resources that this object depends on. You should not call this method if the external resources (e.g. thread pool) are in use by other objects.- Specified by:
releaseExternalResourcesin interfaceExternalResourceReleasable- Overrides:
releaseExternalResourcesin classAbstractTrafficShapingHandler
-
-