@ChannelHandler.Sharable public class GlobalChannelTrafficShapingHandler extends AbstractTrafficShapingHandler
AbstractTrafficShapingHandler is 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.channel.isWritable() and
channelWritabilityChanged(ctx) to handle writability, or through
future.addListener(new GenericFutureListener()) on the future returned by
ctx.write().release() once this handler is not needed anymore to release all internal resources.
This will not shutdown the Timer as it may be shared, so you need to do this by your own.| Modifier and Type | Class and Description |
|---|---|
(package private) static class |
GlobalChannelTrafficShapingHandler.PerChannel |
private static class |
GlobalChannelTrafficShapingHandler.ToSend |
AbstractTrafficShapingHandler.ReadWriteStatus, AbstractTrafficShapingHandler.ReopenReadTimerTask, AbstractTrafficShapingHandler.SimpleObjectSizeEstimatorChannelHandler.Sharable| Modifier and Type | Field and Description |
|---|---|
private float |
accelerationFactor |
(package private) java.util.concurrent.ConcurrentMap<java.lang.Integer,GlobalChannelTrafficShapingHandler.PerChannel> |
channelQueues
All queues per channel
|
private java.util.concurrent.atomic.AtomicLong |
cumulativeReadBytes
Maximum cumulative read bytes for one channel among all (as long as channels stay the same)
|
private java.util.concurrent.atomic.AtomicLong |
cumulativeWrittenBytes
Maximum cumulative writing bytes for one channel among all (as long as channels stay the same)
|
private static float |
DEFAULT_ACCELERATION |
private static float |
DEFAULT_DEVIATION |
private static float |
DEFAULT_SLOWDOWN |
private static InternalLogger |
logger |
private static float |
MAX_DEVIATION |
private float |
maxDeviation |
(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
|
private long |
readChannelLimit
Limit in B/s to apply to read
|
private boolean |
readDeviationActive |
private float |
slowDownFactor |
private long |
writeChannelLimit
Limit in B/s to apply to write
|
private boolean |
writeDeviationActive |
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, maxTime, maxWriteDelay, maxWriteSize, MINIMAL_WAIT, release, timeout, timer, trafficCounter| Constructor and Description |
|---|
GlobalChannelTrafficShapingHandler(ObjectSizeEstimator objectSizeEstimator,
Timer timer) |
GlobalChannelTrafficShapingHandler(ObjectSizeEstimator objectSizeEstimator,
Timer timer,
long checkInterval) |
GlobalChannelTrafficShapingHandler(ObjectSizeEstimator objectSizeEstimator,
Timer timer,
long writeLimit,
long readLimit,
long writeChannelLimit,
long readChannelLimit) |
GlobalChannelTrafficShapingHandler(ObjectSizeEstimator objectSizeEstimator,
Timer timer,
long writeLimit,
long readLimit,
long writeChannelLimit,
long readChannelLimit,
long checkInterval) |
GlobalChannelTrafficShapingHandler(ObjectSizeEstimator objectSizeEstimator,
Timer timer,
long writeLimit,
long readLimit,
long writeChannelLimit,
long readChannelLimit,
long checkInterval,
long maxTime) |
GlobalChannelTrafficShapingHandler(Timer timer)
Create a new instance.
|
GlobalChannelTrafficShapingHandler(Timer timer,
long checkInterval)
Create a new instance.
|
GlobalChannelTrafficShapingHandler(Timer timer,
long writeGlobalLimit,
long readGlobalLimit,
long writeChannelLimit,
long readChannelLimit)
Create a new instance.
|
GlobalChannelTrafficShapingHandler(Timer timer,
long writeGlobalLimit,
long readGlobalLimit,
long writeChannelLimit,
long readChannelLimit,
long checkInterval)
Create a new instance.
|
GlobalChannelTrafficShapingHandler(Timer timer,
long writeGlobalLimit,
long readGlobalLimit,
long writeChannelLimit,
long readChannelLimit,
long checkInterval,
long maxTime)
Create a new instance.
|
| Modifier and Type | Method and Description |
|---|---|
float |
accelerationFactor() |
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. |
java.util.Collection<TrafficCounter> |
channelTrafficCounters()
To allow for instance doAccounting to use the TrafficCounter per channel.
|
protected long |
checkWaitReadTime(ChannelHandlerContext ctx,
long wait,
long now)
Method overridden in GTSH to take into account specific timer for the channel.
|
private long |
computeBalancedWait(float maxLocal,
float maxGlobal,
long wait) |
private void |
computeDeviationCumulativeBytes() |
void |
configureChannel(long newWriteLimit,
long newReadLimit) |
(package private) void |
createGlobalTrafficCounter(Timer timer)
Create the global TrafficCounter.
|
protected void |
doAccounting(TrafficCounter counter)
Called each time the accounting is computed from the TrafficCounters.
|
long |
getMaxGlobalWriteSize() |
private GlobalChannelTrafficShapingHandler.PerChannel |
getOrSetPerChannel(ChannelHandlerContext ctx) |
long |
getReadChannelLimit() |
long |
getWriteChannelLimit() |
protected void |
informReadOperation(ChannelHandlerContext ctx,
long now)
Method overridden in GTSH to take into account specific timer for the channel.
|
float |
maxDeviation() |
protected long |
maximumCumulativeReadBytes() |
protected long |
maximumCumulativeWrittenBytes() |
void |
messageReceived(ChannelHandlerContext ctx,
MessageEvent evt)
Invoked when a message object (e.g:
ChannelBuffer) was received
from a remote peer. |
long |
queuesSize() |
void |
release()
Release all internal resources of this instance.
|
private void |
sendAllValid(ChannelHandlerContext ctx,
GlobalChannelTrafficShapingHandler.PerChannel perChannel,
long now) |
void |
setMaxDeviation(float maxDeviation,
float slowDownFactor,
float accelerationFactor) |
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.
So the expected usage of this method is to be used not too often, accordingly to the traffic shaping configuration. |
void |
setReadChannelLimit(long readLimit) |
void |
setWriteChannelLimit(long writeLimit) |
float |
slowDownFactor() |
protected void |
submitWrite(ChannelHandlerContext ctx,
MessageEvent evt,
long size,
long writedelay,
long now) |
java.lang.String |
toString() |
(package private) int |
userDefinedWritabilityIndex() |
void |
writeRequested(ChannelHandlerContext ctx,
MessageEvent evt)
Invoked when
Channel.write(Object) is called. |
calculateSize, checkAttachment, checkWriteSuspend, configure, configure, configure, getCheckInterval, getMaxTimeWait, getMaxWriteDelay, getMaxWriteSize, getReadLimit, getTrafficCounter, getWriteLimit, internalSubmitWrite, releaseExternalResources, releaseReadSuspended, releaseWriteSuspended, setCheckInterval, setMaxTimeWait, setMaxWriteDelay, setMaxWriteSize, setReadLimit, setTrafficCounter, setWritable, setWriteLimit, submitWritebindRequested, channelBound, channelDisconnected, channelInterestChanged, channelOpen, channelUnbound, childChannelClosed, childChannelOpen, closeRequested, connectRequested, disconnectRequested, exceptionCaught, handleDownstream, handleUpstream, setInterestOpsRequested, unbindRequested, writeCompleteprivate static final InternalLogger logger
final java.util.concurrent.ConcurrentMap<java.lang.Integer,GlobalChannelTrafficShapingHandler.PerChannel> channelQueues
private final java.util.concurrent.atomic.AtomicLong queuesSize
private final java.util.concurrent.atomic.AtomicLong cumulativeWrittenBytes
private final java.util.concurrent.atomic.AtomicLong cumulativeReadBytes
long maxGlobalWriteSize
private volatile long writeChannelLimit
private volatile long readChannelLimit
private static final float DEFAULT_DEVIATION
private static final float MAX_DEVIATION
private static final float DEFAULT_SLOWDOWN
private static final float DEFAULT_ACCELERATION
private volatile float maxDeviation
private volatile float accelerationFactor
private volatile float slowDownFactor
private volatile boolean readDeviationActive
private volatile boolean writeDeviationActive
public GlobalChannelTrafficShapingHandler(Timer timer, long writeGlobalLimit, long readGlobalLimit, long writeChannelLimit, long readChannelLimit, long checkInterval, long maxTime)
timer - the Timer to use for the TrafficCounter.writeGlobalLimit - 0 or a limit in bytes/sreadGlobalLimit - 0 or a limit in bytes/swriteChannelLimit - 0 or a limit in bytes/sreadChannelLimit - 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 GlobalChannelTrafficShapingHandler(Timer timer, long writeGlobalLimit, long readGlobalLimit, long writeChannelLimit, long readChannelLimit, long checkInterval)
timer - the Timer to use for the TrafficCounter.writeGlobalLimit - 0 or a limit in bytes/sreadGlobalLimit - 0 or a limit in bytes/swriteChannelLimit - 0 or a limit in bytes/sreadChannelLimit - 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 GlobalChannelTrafficShapingHandler(Timer timer, long writeGlobalLimit, long readGlobalLimit, long writeChannelLimit, long readChannelLimit)
timer - the Timer to use for the TrafficCounter.writeGlobalLimit - 0 or a limit in bytes/sreadGlobalLimit - 0 or a limit in bytes/swriteChannelLimit - 0 or a limit in bytes/sreadChannelLimit - 0 or a limit in bytes/spublic GlobalChannelTrafficShapingHandler(Timer timer, long checkInterval)
timer - the Timer 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 GlobalChannelTrafficShapingHandler(Timer timer)
timer - the Timer to use for the TrafficCounter.public GlobalChannelTrafficShapingHandler(ObjectSizeEstimator objectSizeEstimator, Timer timer, long writeLimit, long readLimit, long writeChannelLimit, long readChannelLimit, long checkInterval, long maxTime)
objectSizeEstimator - ObjectSizeEstimator to usetimer - the Timer to use for the TrafficCounter.writeLimit - write Global Limit
0 or a limit in bytes/sreadLimit - read Global Limit
0 or a limit in bytes/swriteChannelLimit - 0 or a limit in bytes/sreadChannelLimit - 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 GlobalChannelTrafficShapingHandler(ObjectSizeEstimator objectSizeEstimator, Timer timer, long writeLimit, long readLimit, long writeChannelLimit, long readChannelLimit, long checkInterval)
objectSizeEstimator - ObjectSizeEstimator to usetimer - the Timer to use for the TrafficCounter.writeLimit - write Global Limit
0 or a limit in bytes/sreadLimit - read Global Limit
0 or a limit in bytes/swriteChannelLimit - 0 or a limit in bytes/sreadChannelLimit - 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 GlobalChannelTrafficShapingHandler(ObjectSizeEstimator objectSizeEstimator, Timer timer, long writeLimit, long readLimit, long writeChannelLimit, long readChannelLimit)
objectSizeEstimator - ObjectSizeEstimator to usetimer - the Timer to use for the TrafficCounter.writeLimit - write Global Limit
0 or a limit in bytes/sreadLimit - read Global Limit
0 or a limit in bytes/swriteChannelLimit - 0 or a limit in bytes/sreadChannelLimit - 0 or a limit in bytes/spublic GlobalChannelTrafficShapingHandler(ObjectSizeEstimator objectSizeEstimator, Timer timer, long checkInterval)
objectSizeEstimator - ObjectSizeEstimator to usetimer - the Timer 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 GlobalChannelTrafficShapingHandler(ObjectSizeEstimator objectSizeEstimator, Timer timer)
objectSizeEstimator - ObjectSizeEstimator to usetimer - the Timer to use for the TrafficCounter.void createGlobalTrafficCounter(Timer timer)
int userDefinedWritabilityIndex()
userDefinedWritabilityIndex in class AbstractTrafficShapingHandlerpublic float maxDeviation()
public float accelerationFactor()
public float slowDownFactor()
public void setMaxDeviation(float maxDeviation,
float slowDownFactor,
float accelerationFactor)
maxDeviation - the maximum deviation to allow during computation of average, default deviation
being 0.1, so +/-10% of the desired bandwidth. Maximum being 0.4.slowDownFactor - the factor set as +x% to the too fast client (minimal value being 0, meaning no
slow down factor), default being 40% (0.4).accelerationFactor - the factor set as -x% to the too slow client (maximal value being 0, meaning no
acceleration factor), default being -10% (-0.1).private void computeDeviationCumulativeBytes()
protected void doAccounting(TrafficCounter counter)
AbstractTrafficShapingHandlerdoAccounting in class AbstractTrafficShapingHandlercounter - the TrafficCounter that computes its performanceprivate long computeBalancedWait(float maxLocal,
float maxGlobal,
long wait)
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.public long queuesSize()
public void configureChannel(long newWriteLimit,
long newReadLimit)
newWriteLimit - Channel write limitnewReadLimit - Channel read limitpublic long getWriteChannelLimit()
public void setWriteChannelLimit(long writeLimit)
writeLimit - Channel write limitpublic long getReadChannelLimit()
public void setReadChannelLimit(long readLimit)
readLimit - Channel read limitpublic final void release()
private GlobalChannelTrafficShapingHandler.PerChannel getOrSetPerChannel(ChannelHandlerContext ctx)
public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws java.lang.Exception
SimpleChannelHandlerChannel is open, bound to a local address, and
connected to a remote address.channelConnected in class AbstractTrafficShapingHandlerjava.lang.Exceptionpublic void channelClosed(ChannelHandlerContext ctx, ChannelStateEvent e) throws java.lang.Exception
SimpleChannelHandlerChannel was closed and all its related resources
were released.channelClosed in class SimpleChannelHandlerjava.lang.Exceptionpublic void messageReceived(ChannelHandlerContext ctx, MessageEvent evt) throws java.lang.Exception
SimpleChannelHandlerChannelBuffer) was received
from a remote peer.messageReceived in class AbstractTrafficShapingHandlerjava.lang.Exceptionprotected long checkWaitReadTime(ChannelHandlerContext ctx, long wait, long now)
AbstractTrafficShapingHandlercheckWaitReadTime in class AbstractTrafficShapingHandlerwait - the wait delay computed in msnow - the relative now time in msprotected void informReadOperation(ChannelHandlerContext ctx, long now)
AbstractTrafficShapingHandlerinformReadOperation in class AbstractTrafficShapingHandlernow - the relative now time in msprotected long maximumCumulativeWrittenBytes()
protected long maximumCumulativeReadBytes()
public java.util.Collection<TrafficCounter> channelTrafficCounters()
public void writeRequested(ChannelHandlerContext ctx, MessageEvent evt) throws java.lang.Exception
SimpleChannelHandlerChannel.write(Object) is called.writeRequested in class AbstractTrafficShapingHandlerjava.lang.Exceptionprotected void submitWrite(ChannelHandlerContext ctx, MessageEvent evt, long size, long writedelay, long now) throws java.lang.Exception
submitWrite in class AbstractTrafficShapingHandlerjava.lang.Exceptionprivate void sendAllValid(ChannelHandlerContext ctx, GlobalChannelTrafficShapingHandler.PerChannel perChannel, long now) throws java.lang.Exception
java.lang.Exceptionpublic java.lang.String toString()
toString in class AbstractTrafficShapingHandler