public abstract class AbstractTrafficShapingHandler extends SimpleChannelHandler implements ExternalResourceReleasable
AbstractTrafficShapingHandler allows to limit the global bandwidth
(see GlobalTrafficShapingHandler) or per session
bandwidth (see ChannelTrafficShapingHandler), as traffic shaping.
It allows too to implement an almost real time monitoring of the bandwidth using
the monitors from TrafficCounter that will call back every checkInterval
the method doAccounting of this handler.
An ObjectSizeEstimator can be passed at construction to specify what
is the size of the object to be read or write accordingly to the type of
object. If not specified, it will used the DefaultObjectSizeEstimator implementation.
If you want for any particular reasons to stop the monitoring (accounting) or to change the read/write limit or the check interval, several methods allow that for you:
| Modifier and Type | Class and Description |
|---|---|
(package private) static class |
AbstractTrafficShapingHandler.ReadWriteStatus
Attachment of ChannelHandlerContext
|
(package private) class |
AbstractTrafficShapingHandler.ReopenReadTimerTask
Class to implement setReadable at fix time.
|
static class |
AbstractTrafficShapingHandler.SimpleObjectSizeEstimator
For simple ChannelBuffer, returns the readableBytes, else
use standard DefaultObjectSizeEstimator.
|
ChannelHandler.Sharable| Modifier and Type | Field and Description |
|---|---|
(package private) static int |
CHANNEL_DEFAULT_USER_DEFINED_WRITABILITY_INDEX |
protected long |
checkInterval
Delay between two performance snapshots
|
static long |
DEFAULT_CHECK_INTERVAL
Default delay between two checks: 1s
|
(package private) static long |
DEFAULT_MAX_SIZE
Default max size to not exceed in buffer (write only).
|
static long |
DEFAULT_MAX_TIME
Default max delay in case of traffic shaping
(during which no communication will occur).
|
(package private) static int |
GLOBAL_DEFAULT_USER_DEFINED_WRITABILITY_INDEX |
(package private) static int |
GLOBALCHANNEL_DEFAULT_USER_DEFINED_WRITABILITY_INDEX |
(package private) int |
index |
(package private) static InternalLogger |
logger
Internal logger
|
protected long |
maxTime
Max delay in wait
|
(package private) long |
maxWriteDelay
Max time to delay before proposing to stop writing new objects from next handlers
|
(package private) long |
maxWriteSize
Max size in the list before proposing to stop writing new objects from next handlers
|
(package private) static long |
MINIMAL_WAIT
Default minimal time to wait
|
private ObjectSizeEstimator |
objectSizeEstimator
ObjectSizeEstimator
|
private long |
readLimit
Limit in B/s to apply to read
|
(package private) java.util.concurrent.atomic.AtomicBoolean |
release
Boolean associated with the release of this TrafficShapingHandler.
|
(package private) Timeout |
timeout
used in releaseExternalResources() to cancel the timer
|
protected Timer |
timer
Timer associated to any TrafficCounter
|
protected TrafficCounter |
trafficCounter
Traffic Counter
|
private long |
writeLimit
Limit in B/s to apply to write
|
| Modifier | Constructor and Description |
|---|---|
protected |
AbstractTrafficShapingHandler(ObjectSizeEstimator objectSizeEstimator,
Timer timer)
|
protected |
AbstractTrafficShapingHandler(ObjectSizeEstimator objectSizeEstimator,
Timer timer,
long checkInterval)
Constructor using the specified ObjectSizeEstimator and using NO LIMIT and
default max time as delay allowed value of 15000L ms.
|
protected |
AbstractTrafficShapingHandler(ObjectSizeEstimator objectSizeEstimator,
Timer timer,
long writeLimit,
long readLimit)
|
protected |
AbstractTrafficShapingHandler(ObjectSizeEstimator objectSizeEstimator,
Timer timer,
long writeLimit,
long readLimit,
long checkInterval)
Constructor using the specified ObjectSizeEstimator and
default max time as delay allowed value of 15000L ms.
|
protected |
AbstractTrafficShapingHandler(ObjectSizeEstimator objectSizeEstimator,
Timer timer,
long writeLimit,
long readLimit,
long checkInterval,
long maxTime)
Constructor using the specified ObjectSizeEstimator.
|
protected |
AbstractTrafficShapingHandler(Timer timer)
Constructor using default
ObjectSizeEstimator and using NO LIMIT and
default Check Interval value of 1000L ms and
default max time as delay allowed value of 15000L ms. |
protected |
AbstractTrafficShapingHandler(Timer timer,
long checkInterval)
Constructor using default
ObjectSizeEstimator and using NO LIMIT and
default max time as delay allowed value of 15000L ms. |
protected |
AbstractTrafficShapingHandler(Timer timer,
long writeLimit,
long readLimit)
Constructor using default
ObjectSizeEstimator and using
default Check Interval value of 1000L ms and
default max time as delay allowed value of 15000L ms. |
protected |
AbstractTrafficShapingHandler(Timer timer,
long writeLimit,
long readLimit,
long checkInterval)
Constructor using default
ObjectSizeEstimator and
default max time as delay allowed value of 15000L ms. |
protected |
AbstractTrafficShapingHandler(Timer timer,
long writeLimit,
long readLimit,
long checkInterval,
long maxTime)
Constructor using default
ObjectSizeEstimator. |
| Modifier and Type | Method and Description |
|---|---|
protected long |
calculateSize(java.lang.Object obj) |
void |
channelConnected(ChannelHandlerContext ctx,
ChannelStateEvent e)
Invoked when a
Channel is open, bound to a local address, and
connected to a remote address. |
(package private) static AbstractTrafficShapingHandler.ReadWriteStatus |
checkAttachment(ChannelHandlerContext ctx) |
(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 |
checkWriteSuspend(ChannelHandlerContext ctx,
long delay,
long queueSize)
Check the writability according to delay and size for the channel.
|
void |
configure(long newCheckInterval)
Change the check interval.
|
void |
configure(long newWriteLimit,
long newReadLimit)
Change the underlying limitations.
|
void |
configure(long newWriteLimit,
long newReadLimit,
long newCheckInterval)
Change the underlying limitations and check interval.
|
protected void |
doAccounting(TrafficCounter counter)
Called each time the accounting is computed from the TrafficCounters.
|
long |
getCheckInterval() |
long |
getMaxTimeWait() |
long |
getMaxWriteDelay() |
long |
getMaxWriteSize() |
long |
getReadLimit() |
TrafficCounter |
getTrafficCounter() |
long |
getWriteLimit() |
(package private) void |
informReadOperation(ChannelHandlerContext ctx,
long now)
Method overridden in GTSH to take into account specific timer for the channel.
|
private void |
init(ObjectSizeEstimator newObjectSizeEstimator,
Timer newTimer,
long newWriteLimit,
long newReadLimit,
long newCheckInterval,
long newMaxTime) |
protected void |
internalSubmitWrite(ChannelHandlerContext ctx,
MessageEvent evt)
Deprecated.
|
void |
messageReceived(ChannelHandlerContext ctx,
MessageEvent evt)
Invoked when a message object (e.g:
ChannelBuffer) was received
from a remote peer. |
void |
releaseExternalResources()
Releases the external resources that this object depends on.
|
(package private) void |
releaseReadSuspended(ChannelHandlerContext ctx)
Release the Read suspension.
|
(package private) void |
releaseWriteSuspended(ChannelHandlerContext ctx)
Explicitly release the Write suspended status.
|
void |
setCheckInterval(long newCheckInterval) |
void |
setMaxTimeWait(long maxTime)
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.
|
void |
setMaxWriteDelay(long maxWriteDelay)
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.
|
void |
setMaxWriteSize(long maxWriteSize)
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.
|
void |
setReadLimit(long readLimit)
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.
|
(package private) void |
setTrafficCounter(TrafficCounter newTrafficCounter) |
(package private) void |
setWritable(ChannelHandlerContext ctx,
boolean writable) |
void |
setWriteLimit(long writeLimit)
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 void |
submitWrite(ChannelHandlerContext ctx,
MessageEvent evt,
long delay)
Deprecated.
|
(package private) abstract void |
submitWrite(ChannelHandlerContext ctx,
MessageEvent evt,
long size,
long delay,
long now) |
java.lang.String |
toString() |
(package private) int |
userDefinedWritabilityIndex() |
void |
writeRequested(ChannelHandlerContext ctx,
MessageEvent evt)
Invoked when
Channel.write(Object) is called. |
bindRequested, channelBound, channelClosed, channelDisconnected, channelInterestChanged, channelOpen, channelUnbound, childChannelClosed, childChannelOpen, closeRequested, connectRequested, disconnectRequested, exceptionCaught, handleDownstream, handleUpstream, setInterestOpsRequested, unbindRequested, writeCompletestatic InternalLogger logger
public static final long DEFAULT_CHECK_INTERVAL
public static final long DEFAULT_MAX_TIME
static final long DEFAULT_MAX_SIZE
static final long MINIMAL_WAIT
static final int CHANNEL_DEFAULT_USER_DEFINED_WRITABILITY_INDEX
static final int GLOBAL_DEFAULT_USER_DEFINED_WRITABILITY_INDEX
static final int GLOBALCHANNEL_DEFAULT_USER_DEFINED_WRITABILITY_INDEX
protected TrafficCounter trafficCounter
private ObjectSizeEstimator objectSizeEstimator
protected Timer timer
volatile Timeout timeout
private volatile long writeLimit
private volatile long readLimit
protected volatile long checkInterval
protected volatile long maxTime
volatile long maxWriteDelay
volatile long maxWriteSize
final java.util.concurrent.atomic.AtomicBoolean release
final int index
protected AbstractTrafficShapingHandler(Timer timer, long writeLimit, long readLimit, long checkInterval)
ObjectSizeEstimator and
default max time as delay allowed value of 15000L ms.timer - created once for instance like HashedWheelTimer(10, TimeUnit.MILLISECONDS, 1024).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.protected AbstractTrafficShapingHandler(ObjectSizeEstimator objectSizeEstimator, Timer timer, long writeLimit, long readLimit, long checkInterval)
objectSizeEstimator - the ObjectSizeEstimator that will be used to compute
the size of the message.timer - created once for instance like HashedWheelTimer(10, TimeUnit.MILLISECONDS, 1024).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.protected AbstractTrafficShapingHandler(Timer timer, long writeLimit, long readLimit)
ObjectSizeEstimator and using
default Check Interval value of 1000L ms and
default max time as delay allowed value of 15000L ms.timer - created once for instance like HashedWheelTimer(10, TimeUnit.MILLISECONDS, 1024).writeLimit - 0 or a limit in bytes/sreadLimit - 0 or a limit in bytes/sprotected AbstractTrafficShapingHandler(ObjectSizeEstimator objectSizeEstimator, Timer timer, long writeLimit, long readLimit)
objectSizeEstimator - the ObjectSizeEstimator that will be used to compute
the size of the message.timer - created once for instance like HashedWheelTimer(10, TimeUnit.MILLISECONDS, 1024).writeLimit - 0 or a limit in bytes/sreadLimit - 0 or a limit in bytes/sprotected AbstractTrafficShapingHandler(Timer timer)
ObjectSizeEstimator and using NO LIMIT and
default Check Interval value of 1000L ms and
default max time as delay allowed value of 15000L ms.timer - created once for instance like HashedWheelTimer(10, TimeUnit.MILLISECONDS, 1024).protected AbstractTrafficShapingHandler(ObjectSizeEstimator objectSizeEstimator, Timer timer)
objectSizeEstimator - the ObjectSizeEstimator that will be used to compute
the size of the message.timer - created once for instance like HashedWheelTimer(10, TimeUnit.MILLISECONDS, 1024).protected AbstractTrafficShapingHandler(Timer timer, long checkInterval)
ObjectSizeEstimator and using NO LIMIT and
default max time as delay allowed value of 15000L ms.timer - created once for instance like HashedWheelTimer(10, TimeUnit.MILLISECONDS, 1024).checkInterval - The delay between two computations of performances for
channels or 0 if no stats are to be computed.protected AbstractTrafficShapingHandler(ObjectSizeEstimator objectSizeEstimator, Timer timer, long checkInterval)
objectSizeEstimator - the ObjectSizeEstimator that will be used to compute
the size of the message.timer - created once for instance like HashedWheelTimer(10, TimeUnit.MILLISECONDS, 1024).checkInterval - The delay between two computations of performances for
channels or 0 if no stats are to be computed.protected AbstractTrafficShapingHandler(Timer timer, long writeLimit, long readLimit, long checkInterval, long maxTime)
ObjectSizeEstimator.timer - created once for instance like HashedWheelTimer(10, TimeUnit.MILLISECONDS, 1024).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 max time to wait in case of excess of traffic (to prevent Time Out event).
Must be positive.protected AbstractTrafficShapingHandler(ObjectSizeEstimator objectSizeEstimator, Timer timer, long writeLimit, long readLimit, long checkInterval, long maxTime)
objectSizeEstimator - the ObjectSizeEstimator that will be used to compute
the size of the message.timer - created once for instance like HashedWheelTimer(10, TimeUnit.MILLISECONDS, 1024).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 max time to wait in case of excess of traffic (to prevent Time Out event).
Must be positive.int userDefinedWritabilityIndex()
private void init(ObjectSizeEstimator newObjectSizeEstimator, Timer newTimer, long newWriteLimit, long newReadLimit, long newCheckInterval, long newMaxTime)
void setTrafficCounter(TrafficCounter newTrafficCounter)
newTrafficCounter - the TrafficCounter to setpublic void configure(long newWriteLimit,
long newReadLimit,
long newCheckInterval)
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.newWriteLimit - The new write limit (in bytes)newReadLimit - The new read limit (in bytes)newCheckInterval - The new check interval (in milliseconds)public void configure(long newWriteLimit,
long newReadLimit)
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.newWriteLimit - The new write limit (in bytes)newReadLimit - The new read limit (in bytes)public void configure(long newCheckInterval)
public long getWriteLimit()
public void setWriteLimit(long writeLimit)
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.writeLimit - the writeLimit to setpublic long getReadLimit()
public void setReadLimit(long readLimit)
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.readLimit - the readLimit to setpublic long getCheckInterval()
public void setCheckInterval(long newCheckInterval)
newCheckInterval - the checkInterval to setpublic long getMaxTimeWait()
public void setMaxTimeWait(long maxTime)
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.maxTime - Max delay in wait, shall be less than TIME OUT in related protocol.
Must be positive.public long getMaxWriteDelay()
public void setMaxWriteDelay(long maxWriteDelay)
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.maxWriteDelay - the maximum Write Delay in ms in the buffer allowed before write suspended is set.
Must be positive.public long getMaxWriteSize()
public void setMaxWriteSize(long maxWriteSize)
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.maxWriteSize - the maximum Write Size allowed in the buffer
per channel before write suspended is set,
default being 4194304L bytesprotected void doAccounting(TrafficCounter counter)
counter - the TrafficCounter that computes its performancevoid releaseReadSuspended(ChannelHandlerContext ctx)
public void messageReceived(ChannelHandlerContext ctx, MessageEvent evt) throws java.lang.Exception
SimpleChannelHandlerChannelBuffer) was received
from a remote peer.messageReceived in class SimpleChannelHandlerjava.lang.Exceptionlong checkWaitReadTime(ChannelHandlerContext ctx, long wait, long now)
wait - the wait delay computed in msnow - the relative now time in msvoid informReadOperation(ChannelHandlerContext ctx, long now)
now - the relative now time in mspublic void writeRequested(ChannelHandlerContext ctx, MessageEvent evt) throws java.lang.Exception
SimpleChannelHandlerChannel.write(Object) is called.writeRequested in class SimpleChannelHandlerjava.lang.Exception@Deprecated protected void internalSubmitWrite(ChannelHandlerContext ctx, MessageEvent evt) throws java.lang.Exception
java.lang.Exception@Deprecated protected void submitWrite(ChannelHandlerContext ctx, MessageEvent evt, long delay) throws java.lang.Exception
java.lang.Exceptionabstract void submitWrite(ChannelHandlerContext ctx, MessageEvent evt, long size, long delay, long now) throws java.lang.Exception
java.lang.Exceptionvoid setWritable(ChannelHandlerContext ctx, boolean writable)
void checkWriteSuspend(ChannelHandlerContext ctx, long delay, long queueSize)
delay - the computed delaiqueueSize - the current queueSizevoid releaseWriteSuspended(ChannelHandlerContext ctx)
public TrafficCounter getTrafficCounter()
public void releaseExternalResources()
ExternalResourceReleasablereleaseExternalResources in interface ExternalResourceReleasablestatic AbstractTrafficShapingHandler.ReadWriteStatus checkAttachment(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 SimpleChannelHandlerjava.lang.Exceptionprotected long calculateSize(java.lang.Object obj)
public java.lang.String toString()
toString in class java.lang.Object