Top Description Inners Fields Constructors Methods
io.netty.handler.traffic

public abstract Class AbstractTrafficShapingHandler

extends ChannelDuplexHandler
Class Inheritance
Known Direct Subclasses
io.netty.handler.traffic.ChannelTrafficShapingHandler, io.netty.handler.traffic.GlobalChannelTrafficShapingHandler, io.netty.handler.traffic.GlobalTrafficShapingHandler
Static Imports
io.netty.util.internal.ObjectUtil.checkPositive

AbstractTrafficShapingHandler allows to limit the global bandwidth (see GlobalTrafficShapingHandler) or per session bandwidth (see ChannelTrafficShapingHandler), as traffic shaping. It allows you 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.

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:

Nested and Inner Type Summary

Modifier and TypeClass and Description
pack-priv static class
AbstractTrafficShapingHandler.ReopenReadTimerTask

Class to implement setReadable at fix time

Field Summary

Modifier and TypeField and Description
pack-priv static final int
CHANNEL_DEFAULT_USER_DEFINED_WRITABILITY_INDEX

Default value for Channel UserDefinedWritability index

protected volatile long
checkInterval

Delay between two performance snapshots

public static final long
DEFAULT_CHECK_INTERVAL

Default delay between two checks: 1s

pack-priv static final long
DEFAULT_MAX_SIZE

Default max size to not exceed in buffer (write only).

public static final long
DEFAULT_MAX_TIME

Default max delay in case of traffic shaping (during which no communication will occur).

pack-priv static final int
GLOBAL_DEFAULT_USER_DEFINED_WRITABILITY_INDEX

Default value for Global UserDefinedWritability index

pack-priv static final int
GLOBALCHANNEL_DEFAULT_USER_DEFINED_WRITABILITY_INDEX

Default value for GlobalChannel UserDefinedWritability index

private static final InternalLogger
protected volatile long
maxTime

Max delay in wait

pack-priv volatile long
maxWriteDelay

Max time to delay before proposing to stop writing new objects from next handlers

pack-priv volatile long
maxWriteSize

Max size in the list before proposing to stop writing new objects from next handlers

pack-priv static final long
MINIMAL_WAIT

Default minimal time to wait: 10ms

pack-priv static final AttributeKey<Boolean>
private volatile long
readLimit

Limit in B/s to apply to read

pack-priv static final AttributeKey<Runnable>
protected TrafficCounter
trafficCounter

Traffic Counter

pack-priv final int
userDefinedWritabilityIndex

Rank in UserDefinedWritability (1 for Channel, 2 for Global TrafficShapingHandler).

private volatile long
writeLimit

Limit in B/s to apply to write

Constructor Summary

AccessConstructor and Description
protected
AbstractTrafficShapingHandler(long
0 or a limit in bytes/s
writeLimit
,
long
0 or a limit in bytes/s
readLimit
,
long
The delay between two computations of performances for channels or 0 if no stats are to be computed.
checkInterval
,
long
The maximum delay to wait in case of traffic excess. Must be positive.
maxTime
)

protected
AbstractTrafficShapingHandler(long
0 or a limit in bytes/s
writeLimit
,
long
0 or a limit in bytes/s
readLimit
,
long
The delay between two computations of performances for channels or 0 if no stats are to be computed.
checkInterval
)

Constructor using default max time as delay allowed value of 15000 ms.

protected
AbstractTrafficShapingHandler(long
0 or a limit in bytes/s
writeLimit
,
long
0 or a limit in bytes/s
readLimit
)

Constructor using default Check Interval value of 1000 ms and default max time as delay allowed value of 15000 ms.

protected
AbstractTrafficShapingHandler()

Constructor using NO LIMIT, default Check Interval value of 1000 ms and default max time as delay allowed value of 15000 ms.

protected
AbstractTrafficShapingHandler(long
The delay between two computations of performances for channels or 0 if no stats are to be computed.
checkInterval
)

Constructor using NO LIMIT and default max time as delay allowed value of 15000 ms.

Method Summary

Modifier and TypeMethod and Description
protected long

Returns:

size the size of the msg or -1 if unknown.
calculateSize
(Object
the msg for which the size should be calculated.
msg
)

Calculate the size of the given Object.

public void
public void
pack-priv long

Returns:

the wait to use according to the context
checkWaitReadTime
(final ChannelHandlerContext ctx, long
the wait delay computed in ms
wait
,
final long
the relative now time in ms
now
)

Method overridden in GTSH to take into account specific timer for the channel.

pack-priv void
checkWriteSuspend(ChannelHandlerContext ctx, long
the computed delay
delay
,
long
the current queueSize
queueSize
)

Check the writability according to delay and size for the channel.

public void
configure(long
The new write limit (in bytes)
newWriteLimit
,
long
The new read limit (in bytes)
newReadLimit
,
long
The new check interval (in milliseconds)
newCheckInterval
)

Change the underlying limitations and check interval.

public void
configure(long
The new write limit (in bytes)
newWriteLimit
,
long
The new read limit (in bytes)
newReadLimit
)

Change the underlying limitations.

public void
configure(long
The new check interval (in milliseconds)
newCheckInterval
)

Change the check interval.

protected void
doAccounting(TrafficCounter
the TrafficCounter that computes its performance
counter
)

Called each time the accounting is computed from the TrafficCounters.

public long

Returns:

the checkInterval
getCheckInterval
()

public long

Returns:

the max delay in wait to prevent TIME OUT
getMaxTimeWait
()

public long

Returns:

the maxWriteDelay
getMaxWriteDelay
()

public long

Returns:

the maxWriteSize default being 4194304 bytes.
getMaxWriteSize
()

public long

Returns:

the readLimit
getReadLimit
()

public long

Returns:

the writeLimit
getWriteLimit
()

public void
handlerRemoved(ChannelHandlerContext ctx)

Overrides io.netty.channel.ChannelHandlerAdapter.handlerRemoved.

Implements io.netty.channel.ChannelHandler.handlerRemoved.

Gets called after the ChannelHandler was removed from the actual context and it doesn't handle events anymore.
pack-priv void
informReadOperation(final ChannelHandlerContext ctx, final long
the relative now time in ms
now
)

Method overridden in GTSH to take into account specific timer for the channel.

protected static boolean
public void
pack-priv void
releaseReadSuspended(ChannelHandlerContext ctx)

Release the Read suspension

pack-priv void
releaseWriteSuspended(ChannelHandlerContext ctx)

Explicitly release the Write suspended status.

public void
setCheckInterval(long
the interval in ms between each step check to set, default value being 1000 ms.
checkInterval
)

public void
setMaxTimeWait(long
Max delay in wait, shall be less than TIME OUT in related protocol. Must be positive.
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.

public void
setMaxWriteDelay(long
the maximum Write Delay in ms in the buffer allowed before write suspension is set. Must be positive.
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.

public void
setMaxWriteSize(long
the maximum Write Size allowed in the buffer per channel before write suspended is set, default being 4194304 bytes.
maxWriteSize
)

Note that this limit is a best effort on memory limitation to prevent Out Of Memory Exception.

public void
setReadLimit(long
the readLimit to set
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.

pack-priv void
setTrafficCounter(TrafficCounter
the TrafficCounter to set
newTrafficCounter
)

pack-priv void
public void
setWriteLimit(long
the writeLimit to set
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(final ChannelHandlerContext ctx, final Object msg, final long delay, final ChannelPromise promise)

Deprecated
pack-priv abstract void
submitWrite(ChannelHandlerContext ctx, Object msg, long size, long delay, long now, ChannelPromise promise)

public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.
public TrafficCounter

Returns:

the current TrafficCounter (if channel is still connected)
trafficCounter
()

protected int

Returns:

the index to be used by the TrafficShapingHandler to manage the user defined writability. For Channel TSH it is defined as 1, for Global TSH it is defined as 2, for GlobalChannel TSH it is defined as 3.
userDefinedWritabilityIndex
()

public void
write(final ChannelHandlerContext
the ChannelHandlerContext for which the write operation is made
ctx
,
final Object
the message to write
msg
,
final ChannelPromise
the ChannelPromise to notify once the operation completes
promise
)

Overrides io.netty.channel.ChannelDuplexHandler.write.

Implements io.netty.channel.ChannelOutboundHandler.write.

Calls ChannelHandlerContext#write(Object, ChannelPromise) to forward to the next ChannelOutboundHandler in the ChannelPipeline.
Inherited from io.netty.channel.ChannelDuplexHandler:
bindcloseconnectderegisterdisconnectflush

Field Detail

CHANNEL_DEFAULT_USER_DEFINED_WRITABILITY_INDEXback to summary
pack-priv static final int CHANNEL_DEFAULT_USER_DEFINED_WRITABILITY_INDEX

Default value for Channel UserDefinedWritability index

checkIntervalback to summary
protected volatile long checkInterval

Delay between two performance snapshots

DEFAULT_CHECK_INTERVALback to summary
public static final long DEFAULT_CHECK_INTERVAL

Default delay between two checks: 1s

DEFAULT_MAX_SIZEback to summary
pack-priv static final long DEFAULT_MAX_SIZE

Default max size to not exceed in buffer (write only).

DEFAULT_MAX_TIMEback to summary
public static final long DEFAULT_MAX_TIME

Default max delay in case of traffic shaping (during which no communication will occur). Shall be less than TIMEOUT. Here half of "standard" 30s

GLOBAL_DEFAULT_USER_DEFINED_WRITABILITY_INDEXback to summary
pack-priv static final int GLOBAL_DEFAULT_USER_DEFINED_WRITABILITY_INDEX

Default value for Global UserDefinedWritability index

GLOBALCHANNEL_DEFAULT_USER_DEFINED_WRITABILITY_INDEXback to summary
pack-priv static final int GLOBALCHANNEL_DEFAULT_USER_DEFINED_WRITABILITY_INDEX

Default value for GlobalChannel UserDefinedWritability index

loggerback to summary
private static final InternalLogger logger
maxTimeback to summary
protected volatile long maxTime

Max delay in wait

maxWriteDelayback to summary
pack-priv volatile long maxWriteDelay

Max time to delay before proposing to stop writing new objects from next handlers

maxWriteSizeback to summary
pack-priv volatile long maxWriteSize

Max size in the list before proposing to stop writing new objects from next handlers

MINIMAL_WAITback to summary
pack-priv static final long MINIMAL_WAIT

Default minimal time to wait: 10ms

READ_SUSPENDEDback to summary
pack-priv static final AttributeKey<Boolean> READ_SUSPENDED
readLimitback to summary
private volatile long readLimit

Limit in B/s to apply to read

REOPEN_TASKback to summary
pack-priv static final AttributeKey<Runnable> REOPEN_TASK
trafficCounterback to summary
protected TrafficCounter trafficCounter

Traffic Counter

userDefinedWritabilityIndexback to summary
pack-priv final int userDefinedWritabilityIndex

Rank in UserDefinedWritability (1 for Channel, 2 for Global TrafficShapingHandler). Set in final constructor. Must be between 1 and 31

writeLimitback to summary
private volatile long writeLimit

Limit in B/s to apply to write

Constructor Detail

AbstractTrafficShapingHandlerback to summary
protected AbstractTrafficShapingHandler(long writeLimit, long readLimit, long checkInterval, long maxTime)
Parameters
writeLimit:long

0 or a limit in bytes/s

readLimit:long

0 or a limit in bytes/s

checkInterval:long

The delay between two computations of performances for channels or 0 if no stats are to be computed.

maxTime:long

The maximum delay to wait in case of traffic excess. Must be positive.

AbstractTrafficShapingHandlerback to summary
protected AbstractTrafficShapingHandler(long writeLimit, long readLimit, long checkInterval)

Constructor using default max time as delay allowed value of 15000 ms.

Parameters
writeLimit:long

0 or a limit in bytes/s

readLimit:long

0 or a limit in bytes/s

checkInterval:long

The delay between two computations of performances for channels or 0 if no stats are to be computed.

AbstractTrafficShapingHandlerback to summary
protected AbstractTrafficShapingHandler(long writeLimit, long readLimit)

Constructor using default Check Interval value of 1000 ms and default max time as delay allowed value of 15000 ms.

Parameters
writeLimit:long

0 or a limit in bytes/s

readLimit:long

0 or a limit in bytes/s

AbstractTrafficShapingHandlerback to summary
protected AbstractTrafficShapingHandler()

Constructor using NO LIMIT, default Check Interval value of 1000 ms and default max time as delay allowed value of 15000 ms.

AbstractTrafficShapingHandlerback to summary
protected AbstractTrafficShapingHandler(long checkInterval)

Constructor using NO LIMIT and default max time as delay allowed value of 15000 ms.

Parameters
checkInterval:long

The delay between two computations of performances for channels or 0 if no stats are to be computed.

Method Detail

calculateSizeback to summary
protected long calculateSize(Object msg)

Calculate the size of the given Object. This implementation supports ByteBuf, ByteBufHolder and FileRegion. Sub-classes may override this.

Parameters
msg:Object

the msg for which the size should be calculated.

Returns:long

size the size of the msg or -1 if unknown.

channelReadback to summary
public void channelRead(final ChannelHandlerContext ctx, final Object msg) throws Exception

Overrides io.netty.channel.ChannelInboundHandlerAdapter.channelRead.

Implements io.netty.channel.ChannelInboundHandler.channelRead.

Doc from io.netty.channel.ChannelInboundHandlerAdapter.channelRead.

Calls ChannelHandlerContext#fireChannelRead(Object) to forward to the next ChannelInboundHandler in the ChannelPipeline. Sub-classes may override this method to change behavior.

Annotations
@Override
channelRegisteredback to summary
public void channelRegistered(ChannelHandlerContext ctx) throws Exception

Overrides io.netty.channel.ChannelInboundHandlerAdapter.channelRegistered.

Implements io.netty.channel.ChannelInboundHandler.channelRegistered.

Doc from io.netty.channel.ChannelInboundHandlerAdapter.channelRegistered.

Calls ChannelHandlerContext#fireChannelRegistered() to forward to the next ChannelInboundHandler in the ChannelPipeline. Sub-classes may override this method to change behavior.

Annotations
@Override
checkWaitReadTimeback to summary
pack-priv long checkWaitReadTime(final ChannelHandlerContext ctx, long wait, final long now)

Method overridden in GTSH to take into account specific timer for the channel.

Parameters
wait:long

the wait delay computed in ms

now:long

the relative now time in ms

Returns:long

the wait to use according to the context

checkWriteSuspendback to summary
pack-priv void checkWriteSuspend(ChannelHandlerContext ctx, long delay, long queueSize)

Check the writability according to delay and size for the channel. Set if necessary setUserDefinedWritability status.

Parameters
delay:long

the computed delay

queueSize:long

the current queueSize

configureback to summary
public void configure(long newWriteLimit, long newReadLimit, long newCheckInterval)

Change the underlying limitations and check interval.

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.

Parameters
newWriteLimit:long

The new write limit (in bytes)

newReadLimit:long

The new read limit (in bytes)

newCheckInterval:long

The new check interval (in milliseconds)

configureback to summary
public void configure(long newWriteLimit, long newReadLimit)

Change the underlying limitations.

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.

Parameters
newWriteLimit:long

The new write limit (in bytes)

newReadLimit:long

The new read limit (in bytes)

configureback to summary
public void configure(long newCheckInterval)

Change the check interval.

Parameters
newCheckInterval:long

The new check interval (in milliseconds)

doAccountingback to summary
protected void doAccounting(TrafficCounter counter)

Called each time the accounting is computed from the TrafficCounters. This method could be used for instance to implement almost real time accounting.

Parameters
counter:TrafficCounter

the TrafficCounter that computes its performance

getCheckIntervalback to summary
public long getCheckInterval()
Returns:long

the checkInterval

getMaxTimeWaitback to summary
public long getMaxTimeWait()
Returns:long

the max delay in wait to prevent TIME OUT

getMaxWriteDelayback to summary
public long getMaxWriteDelay()
Returns:long

the maxWriteDelay

getMaxWriteSizeback to summary
public long getMaxWriteSize()
Returns:long

the maxWriteSize default being 4194304 bytes.

getReadLimitback to summary
public long getReadLimit()
Returns:long

the readLimit

getWriteLimitback to summary
public long getWriteLimit()
Returns:long

the writeLimit

handlerRemovedback to summary
public void handlerRemoved(ChannelHandlerContext ctx) throws Exception

Overrides io.netty.channel.ChannelHandlerAdapter.handlerRemoved.

Implements io.netty.channel.ChannelHandler.handlerRemoved.

Doc from io.netty.channel.ChannelHandler.handlerRemoved.

Gets called after the ChannelHandler was removed from the actual context and it doesn't handle events anymore.

Annotations
@Override
informReadOperationback to summary
pack-priv void informReadOperation(final ChannelHandlerContext ctx, final long now)

Method overridden in GTSH to take into account specific timer for the channel.

Parameters
now:long

the relative now time in ms

isHandlerActiveback to summary
protected static boolean isHandlerActive(ChannelHandlerContext ctx)
readback to summary
public void read(ChannelHandlerContext ctx)

Overrides io.netty.channel.ChannelDuplexHandler.read.

Implements io.netty.channel.ChannelOutboundHandler.read.

Doc from io.netty.channel.ChannelDuplexHandler.read.

Calls ChannelHandlerContext#read() to forward to the next ChannelOutboundHandler in the ChannelPipeline. Sub-classes may override this method to change behavior.

Annotations
@Override
releaseReadSuspendedback to summary
pack-priv void releaseReadSuspended(ChannelHandlerContext ctx)

Release the Read suspension

releaseWriteSuspendedback to summary
pack-priv void releaseWriteSuspended(ChannelHandlerContext ctx)

Explicitly release the Write suspended status.

setCheckIntervalback to summary
public void setCheckInterval(long checkInterval)
Parameters
checkInterval:long

the interval in ms between each step check to set, default value being 1000 ms.

setMaxTimeWaitback to summary
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.

Parameters
maxTime:long

Max delay in wait, shall be less than TIME OUT in related protocol. Must be positive.

setMaxWriteDelayback to summary
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.

Parameters
maxWriteDelay:long

the maximum Write Delay in ms in the buffer allowed before write suspension is set. Must be positive.

setMaxWriteSizeback to summary
public void setMaxWriteSize(long maxWriteSize)

Note that this limit is a best effort on memory limitation to prevent Out Of Memory Exception. To ensure it works, the handler generating the write should use one of the way provided by Netty to handle the capacity:

- the Channel.isWritable() property and the corresponding channelWritabilityChanged()

- the ChannelFuture.addListener(new GenericFutureListener())

Parameters
maxWriteSize:long

the maximum Write Size allowed in the buffer per channel before write suspended is set, default being 4194304 bytes.

setReadLimitback to summary
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.

Parameters
readLimit:long

the readLimit to set

setTrafficCounterback to summary
pack-priv void setTrafficCounter(TrafficCounter newTrafficCounter)
Parameters
newTrafficCounter:TrafficCounter

the TrafficCounter to set

setUserDefinedWritabilityback to summary
pack-priv void setUserDefinedWritability(ChannelHandlerContext ctx, boolean writable)
setWriteLimitback to summary
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.

Parameters
writeLimit:long

the writeLimit to set

submitWriteback to summary
protected void submitWrite(final ChannelHandlerContext ctx, final Object msg, final long delay, final ChannelPromise promise)

Deprecated

Annotations
@Deprecated
submitWriteback to summary
pack-priv abstract void submitWrite(ChannelHandlerContext ctx, Object msg, long size, long delay, long now, ChannelPromise promise)
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object.

Returns:String

a string representation of the object

Annotations
@Override
trafficCounterback to summary
public TrafficCounter trafficCounter()
Returns:TrafficCounter

the current TrafficCounter (if channel is still connected)

userDefinedWritabilityIndexback to summary
protected int userDefinedWritabilityIndex()
Returns:int

the index to be used by the TrafficShapingHandler to manage the user defined writability. For Channel TSH it is defined as 1, for Global TSH it is defined as 2, for GlobalChannel TSH it is defined as 3.

writeback to summary
public void write(final ChannelHandlerContext ctx, final Object msg, final ChannelPromise promise) throws Exception

Overrides io.netty.channel.ChannelDuplexHandler.write.

Implements io.netty.channel.ChannelOutboundHandler.write.

Doc from io.netty.channel.ChannelDuplexHandler.write.

Calls ChannelHandlerContext#write(Object, ChannelPromise) to forward to the next ChannelOutboundHandler in the ChannelPipeline. Sub-classes may override this method to change behavior.

Parameters
ctx:ChannelHandlerContext

Doc from io.netty.channel.ChannelOutboundHandler.write.

the ChannelHandlerContext for which the write operation is made

msg:Object

Doc from io.netty.channel.ChannelOutboundHandler.write.

the message to write

promise:ChannelPromise

Doc from io.netty.channel.ChannelOutboundHandler.write.

the ChannelPromise to notify once the operation completes

Annotations
@Override
Exceptions
Exception:

Doc from io.netty.channel.ChannelOutboundHandler.write.

thrown if an error occurs

io.netty.handler.traffic back to summary

pack-priv final Class AbstractTrafficShapingHandler.ReopenReadTimerTask

extends Object
implements Runnable
Class Inheritance
  • java.lang.Object
  • io.netty.handler.traffic.AbstractTrafficShapingHandler.ReopenReadTimerTask
All Implemented Interfaces
java.lang.Runnable

Class to implement setReadable at fix time

Field Summary

Modifier and TypeField and Description
pack-priv final ChannelHandlerContext

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
public void
run()

Implements java.lang.Runnable.run.

Runs this operation.
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

ctxback to summary
pack-priv final ChannelHandlerContext ctx

Constructor Detail

ReopenReadTimerTaskback to summary
pack-priv ReopenReadTimerTask(ChannelHandlerContext ctx)

Method Detail

runback to summary
public void run()

Implements java.lang.Runnable.run.

Doc from java.lang.Runnable.run.

Runs this operation.

Annotations
@Override