Class GlobalTrafficShapingHandler
- java.lang.Object
-
- org.jboss.netty.channel.SimpleChannelHandler
-
- org.jboss.netty.handler.traffic.AbstractTrafficShapingHandler
-
- org.jboss.netty.handler.traffic.GlobalTrafficShapingHandler
-
- All Implemented Interfaces:
ChannelDownstreamHandler
,ChannelHandler
,ChannelUpstreamHandler
,ExternalResourceReleasable
@Sharable public class GlobalTrafficShapingHandler extends AbstractTrafficShapingHandler
This implementation of the
The general use should be as follow:AbstractTrafficShapingHandler
is for global traffic shaping, that is to say a global limitation of the bandwidth, whatever the number of opened channels.Create your unique GlobalTrafficShapingHandler like:
GlobalTrafficShapingHandler myHandler = new GlobalTrafficShapingHandler(timer);
timer could be created using HashedWheelTimer
pipeline.addLast("GLOBAL_TRAFFIC_SHAPING", 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.
Add it in your pipeline, before a recommended
ExecutionHandler
(likeOrderedMemoryAwareThreadPoolExecutor
orMemoryAwareThreadPoolExecutor
).pipeline.addLast("GLOBAL_TRAFFIC_SHAPING", myHandler);
When you shutdown your application, release all the external resources by calling:
myHandler.releaseExternalResources();
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static class
GlobalTrafficShapingHandler.PerChannel
private static class
GlobalTrafficShapingHandler.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 java.util.concurrent.ConcurrentMap<java.lang.Integer,GlobalTrafficShapingHandler.PerChannel>
channelQueues
(package private) long
maxGlobalWriteSize
Max size in the list before proposing to stop writing new objects from next handlers for all channel (global)private java.util.concurrent.atomic.AtomicLong
queuesSize
Global queues size-
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 GlobalTrafficShapingHandler(ObjectSizeEstimator objectSizeEstimator, Timer timer)
GlobalTrafficShapingHandler(ObjectSizeEstimator objectSizeEstimator, Timer timer, long checkInterval)
GlobalTrafficShapingHandler(ObjectSizeEstimator objectSizeEstimator, Timer timer, long writeLimit, long readLimit)
GlobalTrafficShapingHandler(ObjectSizeEstimator objectSizeEstimator, Timer timer, long writeLimit, long readLimit, long checkInterval)
GlobalTrafficShapingHandler(ObjectSizeEstimator objectSizeEstimator, Timer timer, long writeLimit, long readLimit, long checkInterval, long maxTime)
GlobalTrafficShapingHandler(Timer timer)
GlobalTrafficShapingHandler(Timer timer, long checkInterval)
GlobalTrafficShapingHandler(Timer timer, long writeLimit, long readLimit)
GlobalTrafficShapingHandler(Timer timer, long writeLimit, long readLimit, long checkInterval)
GlobalTrafficShapingHandler(Timer timer, long writeLimit, long readLimit, long checkInterval, long maxTime)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
channelClosed(ChannelHandlerContext ctx, ChannelStateEvent e)
Invoked when aChannel
was closed and all its related resources were released.void
channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e)
Invoked when aChannel
is open, bound to a local address, and connected to a remote address.(package private) long
checkWaitReadTime(ChannelHandlerContext ctx, long wait, long now)
Method overridden in GTSH to take into account specific timer for the channel.(package private) void
createGlobalTrafficCounter()
Create the global TrafficCounter.long
getMaxGlobalWriteSize()
private GlobalTrafficShapingHandler.PerChannel
getOrSetPerChannel(ChannelHandlerContext ctx)
(package private) void
informReadOperation(ChannelHandlerContext ctx, long now)
Method overridden in GTSH to take into account specific timer for the channel.long
queuesSize()
void
releaseExternalResources()
Releases the external resources that this object depends on.private void
sendAllValid(ChannelHandlerContext ctx, GlobalTrafficShapingHandler.PerChannel perChannel, long now)
void
setMaxGlobalWriteSize(long maxGlobalWriteSize)
(package private) void
submitWrite(ChannelHandlerContext ctx, MessageEvent evt, long size, long writedelay, long now)
-
Methods inherited from class org.jboss.netty.handler.traffic.AbstractTrafficShapingHandler
calculateSize, checkAttachment, checkWriteSuspend, configure, configure, configure, doAccounting, getCheckInterval, getMaxTimeWait, getMaxWriteDelay, getMaxWriteSize, getReadLimit, getTrafficCounter, getWriteLimit, 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
-
channelQueues
private final java.util.concurrent.ConcurrentMap<java.lang.Integer,GlobalTrafficShapingHandler.PerChannel> channelQueues
-
queuesSize
private final java.util.concurrent.atomic.AtomicLong queuesSize
Global queues size
-
maxGlobalWriteSize
long maxGlobalWriteSize
Max size in the list before proposing to stop writing new objects from next handlers for all channel (global)
-
-
Constructor Detail
-
GlobalTrafficShapingHandler
public GlobalTrafficShapingHandler(Timer timer, long writeLimit, long readLimit, long checkInterval)
-
GlobalTrafficShapingHandler
public GlobalTrafficShapingHandler(Timer timer, long writeLimit, long readLimit, long checkInterval, long maxTime)
-
GlobalTrafficShapingHandler
public GlobalTrafficShapingHandler(Timer timer, long writeLimit, long readLimit)
-
GlobalTrafficShapingHandler
public GlobalTrafficShapingHandler(Timer timer, long checkInterval)
-
GlobalTrafficShapingHandler
public GlobalTrafficShapingHandler(Timer timer)
-
GlobalTrafficShapingHandler
public GlobalTrafficShapingHandler(ObjectSizeEstimator objectSizeEstimator, Timer timer, long writeLimit, long readLimit, long checkInterval)
-
GlobalTrafficShapingHandler
public GlobalTrafficShapingHandler(ObjectSizeEstimator objectSizeEstimator, Timer timer, long writeLimit, long readLimit, long checkInterval, long maxTime)
-
GlobalTrafficShapingHandler
public GlobalTrafficShapingHandler(ObjectSizeEstimator objectSizeEstimator, Timer timer, long writeLimit, long readLimit)
-
GlobalTrafficShapingHandler
public GlobalTrafficShapingHandler(ObjectSizeEstimator objectSizeEstimator, Timer timer, long checkInterval)
-
GlobalTrafficShapingHandler
public GlobalTrafficShapingHandler(ObjectSizeEstimator objectSizeEstimator, Timer timer)
-
-
Method Detail
-
createGlobalTrafficCounter
void createGlobalTrafficCounter()
Create the global TrafficCounter.
-
getMaxGlobalWriteSize
public long getMaxGlobalWriteSize()
- Returns:
- the maxGlobalWriteSize default value being 400 MB.
-
setMaxGlobalWriteSize
public void setMaxGlobalWriteSize(long maxGlobalWriteSize)
- Parameters:
maxGlobalWriteSize
- the maximum Global Write Size allowed in the buffer globally for all channels before write suspended is set, default value being 400 MB.
-
queuesSize
public long queuesSize()
- Returns:
- the global size of the buffers for all queues.
-
getOrSetPerChannel
private GlobalTrafficShapingHandler.PerChannel getOrSetPerChannel(ChannelHandlerContext ctx)
-
checkWaitReadTime
long checkWaitReadTime(ChannelHandlerContext ctx, long wait, long now)
Description copied from class:AbstractTrafficShapingHandler
Method overridden in GTSH to take into account specific timer for the channel.- Overrides:
checkWaitReadTime
in classAbstractTrafficShapingHandler
wait
- the wait delay computed in msnow
- the relative now time in ms- Returns:
- the wait to use according to the context.
-
informReadOperation
void informReadOperation(ChannelHandlerContext ctx, long now)
Description copied from class:AbstractTrafficShapingHandler
Method overridden in GTSH to take into account specific timer for the channel.- Overrides:
informReadOperation
in classAbstractTrafficShapingHandler
now
- the relative now time in ms
-
submitWrite
void submitWrite(ChannelHandlerContext ctx, MessageEvent evt, long size, long writedelay, long now) throws java.lang.Exception
- Specified by:
submitWrite
in classAbstractTrafficShapingHandler
- Throws:
java.lang.Exception
-
sendAllValid
private void sendAllValid(ChannelHandlerContext ctx, GlobalTrafficShapingHandler.PerChannel perChannel, long now) throws java.lang.Exception
- Throws:
java.lang.Exception
-
channelConnected
public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws java.lang.Exception
Description copied from class:SimpleChannelHandler
Invoked when aChannel
is open, bound to a local address, and connected to a remote address.- Overrides:
channelConnected
in classAbstractTrafficShapingHandler
- Throws:
java.lang.Exception
-
channelClosed
public void channelClosed(ChannelHandlerContext ctx, ChannelStateEvent e) throws java.lang.Exception
Description copied from class:SimpleChannelHandler
Invoked when aChannel
was closed and all its related resources were released.- Overrides:
channelClosed
in classSimpleChannelHandler
- Throws:
java.lang.Exception
-
releaseExternalResources
public void releaseExternalResources()
Description copied from interface:ExternalResourceReleasable
Releases 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:
releaseExternalResources
in interfaceExternalResourceReleasable
- Overrides:
releaseExternalResources
in classAbstractTrafficShapingHandler
-
-