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

public Class GlobalChannelTrafficShapingHandler

extends AbstractTrafficShapingHandler
Class Inheritance
Imports
io.netty5.channel.Channel, .ChannelHandlerContext, .ChannelOption, io.netty5.util.Attribute, .Resource, io.netty5.util.concurrent.EventExecutor, .EventExecutorGroup, .Future, .Promise, io.netty5.util.internal.logging.InternalLogger, .InternalLoggerFactory, java.util.AbstractCollection, .ArrayDeque, .Collection, .Iterator, java.util.concurrent.ConcurrentHashMap, .ConcurrentMap, .TimeUnit, java.util.concurrent.atomic.AtomicLong

This implementation of the 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.

This version shall not be in the same pipeline than other TrafficShapingHandler.

The general use should be as follow:

Be sure to call release() once this handler is not needed anymore to release all internal resources. This will not shutdown the EventExecutor as it may be shared, so you need to do this by your own.

Nested and Inner Type Summary

Modifier and TypeClass and Description
pack-priv static class
private static class

Field Summary

Modifier and TypeField and Description
private volatile float
pack-priv final ConcurrentMap<Integer, GlobalChannelTrafficShapingHandler.PerChannel>
channelQueues

All queues per channel

private final AtomicLong
cumulativeReadBytes

Maximum cumulative read bytes for one channel among all (as long as channels stay the same)

private final AtomicLong
cumulativeWrittenBytes

Maximum cumulative writing bytes for one channel among all (as long as channels stay the same)

private static final float
private static final float
private static final float
private static final InternalLogger
private static final float
private volatile float
pack-priv volatile long
maxGlobalWriteSize

Max size in the list before proposing to stop writing new objects from next handlers for all channel (global)

private final AtomicLong
queuesSize

Global queues size

private volatile long
readChannelLimit

Limit in B/s to apply to read

private volatile boolean
private volatile float
private volatile long
writeChannelLimit

Limit in B/s to apply to write

private volatile boolean
Inherited from io.netty5.handler.traffic.AbstractTrafficShapingHandler:
CHANNEL_DEFAULT_USER_DEFINED_WRITABILITY_INDEXcheckIntervalDEFAULT_CHECK_INTERVALDEFAULT_MAX_SIZEDEFAULT_MAX_TIMEGLOBAL_DEFAULT_USER_DEFINED_WRITABILITY_INDEXGLOBALCHANNEL_DEFAULT_USER_DEFINED_WRITABILITY_INDEXmaxTimemaxWriteDelaymaxWriteSizeMINIMAL_WAITREAD_SUSPENDEDREOPEN_TASKtrafficCounteruserDefinedWritabilityIndex

Constructor Summary

AccessConstructor and Description
public
GlobalChannelTrafficShapingHandler(EventExecutorGroup executor, long
0 or a limit in bytes/s
writeGlobalLimit
,
long
0 or a limit in bytes/s
readGlobalLimit
,
long
0 or a limit in bytes/s
writeChannelLimit
,
long
0 or a limit in bytes/s
readChannelLimit
,
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.
maxTime
)

Create a new instance.

public
GlobalChannelTrafficShapingHandler(EventExecutorGroup executor, long
0 or a limit in bytes/s
writeGlobalLimit
,
long
0 or a limit in bytes/s
readGlobalLimit
,
long
0 or a limit in bytes/s
writeChannelLimit
,
long
0 or a limit in bytes/s
readChannelLimit
,
long
The delay between two computations of performances for channels or 0 if no stats are to be computed.
checkInterval
)

Create a new instance.

public
GlobalChannelTrafficShapingHandler(EventExecutorGroup executor, long
0 or a limit in bytes/s
writeGlobalLimit
,
long
0 or a limit in bytes/s
readGlobalLimit
,
long
0 or a limit in bytes/s
writeChannelLimit
,
long
0 or a limit in bytes/s
readChannelLimit
)

Create a new instance.

public
GlobalChannelTrafficShapingHandler(EventExecutorGroup executor, long
The delay between two computations of performances for channels or 0 if no stats are to be computed.
checkInterval
)

Create a new instance.

public

Method Summary

Modifier and TypeMethod and Description
public float

Returns:

the current acceleration factor
accelerationFactor
()

public void
channelRead(final ChannelHandlerContext ctx, final Object msg)

Overrides io.netty5.handler.traffic.AbstractTrafficShapingHandler.channelRead.

Overrides default io.netty5.channel.ChannelHandler.channelRead.

Invoked when the current Channel has read a message from the peer.
public Collection<TrafficCounter>

Returns:

the list of TrafficCounters that exists at the time of the call.
channelTrafficCounters
()

To allow for instance doAccounting to use the TrafficCounter per channel.

protected long
checkWaitReadTime(final ChannelHandlerContext ctx, long
the wait delay computed in ms
wait
,
final long
the relative now time in ms
now
)

Overrides io.netty5.handler.traffic.AbstractTrafficShapingHandler.checkWaitReadTime.

Method overridden in GTSH to take into account specific timer for the channel.
private long
computeBalancedWait(float maxLocal, float maxGlobal, long wait)

private void
public void
configureChannel(long
Channel write limit
newWriteLimit
,
long
Channel read limit
newReadLimit
)

pack-priv void
createGlobalTrafficCounter(EventExecutorGroup executor)

Create the global TrafficCounter

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

Overrides io.netty5.handler.traffic.AbstractTrafficShapingHandler.doAccounting.

Called each time the accounting is computed from the TrafficCounters.
public long

Returns:

the maxGlobalWriteSize
getMaxGlobalWriteSize
()

private GlobalChannelTrafficShapingHandler.PerChannel
public long

Returns:

Channel read limit
getReadChannelLimit
()

public long

Returns:

Channel write limit
getWriteChannelLimit
()

public void
handlerAdded(ChannelHandlerContext ctx)

Overrides default io.netty5.channel.ChannelHandler.handlerAdded.

Gets called after the ChannelHandler was added to the actual context and it's ready to handle events.
public void
handlerRemoved(ChannelHandlerContext ctx)

Overrides io.netty5.handler.traffic.AbstractTrafficShapingHandler.handlerRemoved.

Overrides default io.netty5.channel.ChannelHandler.handlerRemoved.

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

Overrides io.netty5.handler.traffic.AbstractTrafficShapingHandler.informReadOperation.

Method overridden in GTSH to take into account specific timer for the channel.
public boolean
isSharable()

Overrides default io.netty5.channel.ChannelHandler.isSharable.

Returns true if this handler is sharable and thus can be added to more than one ChannelPipeline.
public float

Returns:

the current max deviation
maxDeviation
()

protected long
protected long
public long

Returns:

the global size of the buffers for all queues.
queuesSize
()

public final void
release()

Release all internal resources of this instance.

private void
public void
setMaxDeviation(float
the maximum deviation to allow during computation of average, default deviation being 0.1, so +/-10% of the desired bandwidth. Maximum being 0.4.
maxDeviation
,
float
the factor set as +x% to the too fast client (minimal value being 0, meaning no slow down factor), default being 40% (0.4).
slowDownFactor
,
float
the factor set as -x% to the too slow client (maximal value being 0, meaning no acceleration factor), default being -10% (-0.1).
accelerationFactor
)

public void
setMaxGlobalWriteSize(long
the maximum Global Write Size allowed in the buffer globally for all channels before write suspended is set.
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.

public void
setReadChannelLimit(long
Channel read limit
readLimit
)

public void
setWriteChannelLimit(long
Channel write limit
writeLimit
)

public float

Returns:

the current slow down factor
slowDownFactor
()

protected void
submitWrite(final ChannelHandlerContext ctx, final Object msg, final long size, final long writedelay, final long now, final Promise<Void> promise)

Implements abstract io.netty5.handler.traffic.AbstractTrafficShapingHandler.submitWrite.

public String
toString()

Overrides io.netty5.handler.traffic.AbstractTrafficShapingHandler.toString.

Returns a string representation of the object.
protected int
public Future<Void>
write(final ChannelHandlerContext
the ChannelHandlerContext for which the write operation is made
ctx
,
final Object
the message to write
msg
)

Overrides io.netty5.handler.traffic.AbstractTrafficShapingHandler.write.

Overrides default io.netty5.channel.ChannelHandler.write.

Called once a write operation is made.
Inherited from io.netty5.handler.traffic.AbstractTrafficShapingHandler:
calculateSizechannelRegisteredcheckWriteSuspendconfigureconfigureconfiguregetCheckIntervalgetMaxTimeWaitgetMaxWriteDelaygetMaxWriteSizegetReadLimitgetWriteLimitisHandlerActivereadreleaseReadSuspendedreleaseWriteSuspendedsetCheckIntervalsetMaxTimeWaitsetMaxWriteDelaysetMaxWriteSizesetReadLimitsetTrafficCountersetUserDefinedWritabilitysetWriteLimitsubmitWritetrafficCounter

Field Detail

accelerationFactorback to summary
private volatile float accelerationFactor
channelQueuesback to summary
pack-priv final ConcurrentMap<Integer, GlobalChannelTrafficShapingHandler.PerChannel> channelQueues

All queues per channel

cumulativeReadBytesback to summary
private final AtomicLong cumulativeReadBytes

Maximum cumulative read bytes for one channel among all (as long as channels stay the same)

cumulativeWrittenBytesback to summary
private final AtomicLong cumulativeWrittenBytes

Maximum cumulative writing bytes for one channel among all (as long as channels stay the same)

DEFAULT_ACCELERATIONback to summary
private static final float DEFAULT_ACCELERATION
DEFAULT_DEVIATIONback to summary
private static final float DEFAULT_DEVIATION
DEFAULT_SLOWDOWNback to summary
private static final float DEFAULT_SLOWDOWN
loggerback to summary
private static final InternalLogger logger

Hides io.netty5.handler.traffic.AbstractTrafficShapingHandler.logger.

MAX_DEVIATIONback to summary
private static final float MAX_DEVIATION
maxDeviationback to summary
private volatile float maxDeviation
maxGlobalWriteSizeback to summary
pack-priv volatile long maxGlobalWriteSize

Max size in the list before proposing to stop writing new objects from next handlers for all channel (global)

queuesSizeback to summary
private final AtomicLong queuesSize

Global queues size

readChannelLimitback to summary
private volatile long readChannelLimit

Limit in B/s to apply to read

readDeviationActiveback to summary
private volatile boolean readDeviationActive
slowDownFactorback to summary
private volatile float slowDownFactor
writeChannelLimitback to summary
private volatile long writeChannelLimit

Limit in B/s to apply to write

writeDeviationActiveback to summary
private volatile boolean writeDeviationActive

Constructor Detail

GlobalChannelTrafficShapingHandlerback to summary
public GlobalChannelTrafficShapingHandler(EventExecutorGroup executor, long writeGlobalLimit, long readGlobalLimit, long writeChannelLimit, long readChannelLimit, long checkInterval, long maxTime)

Create a new instance.

Parameters
executor:EventExecutorGroup

the EventExecutorGroup to use for the TrafficCounter.

writeGlobalLimit:long

0 or a limit in bytes/s

readGlobalLimit:long

0 or a limit in bytes/s

writeChannelLimit:long

0 or a limit in bytes/s

readChannelLimit: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.

GlobalChannelTrafficShapingHandlerback to summary
public GlobalChannelTrafficShapingHandler(EventExecutorGroup executor, long writeGlobalLimit, long readGlobalLimit, long writeChannelLimit, long readChannelLimit, long checkInterval)

Create a new instance.

Parameters
executor:EventExecutorGroup

the EventExecutorGroup to use for the TrafficCounter.

writeGlobalLimit:long

0 or a limit in bytes/s

readGlobalLimit:long

0 or a limit in bytes/s

writeChannelLimit:long

0 or a limit in bytes/s

readChannelLimit: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.

GlobalChannelTrafficShapingHandlerback to summary
public GlobalChannelTrafficShapingHandler(EventExecutorGroup executor, long writeGlobalLimit, long readGlobalLimit, long writeChannelLimit, long readChannelLimit)

Create a new instance.

Parameters
executor:EventExecutorGroup

the EventExecutorGroup to use for the TrafficCounter.

writeGlobalLimit:long

0 or a limit in bytes/s

readGlobalLimit:long

0 or a limit in bytes/s

writeChannelLimit:long

0 or a limit in bytes/s

readChannelLimit:long

0 or a limit in bytes/s

GlobalChannelTrafficShapingHandlerback to summary
public GlobalChannelTrafficShapingHandler(EventExecutorGroup executor, long checkInterval)

Create a new instance.

Parameters
executor:EventExecutorGroup

the EventExecutorGroup to use for the TrafficCounter.

checkInterval:long

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

GlobalChannelTrafficShapingHandlerback to summary
public GlobalChannelTrafficShapingHandler(EventExecutorGroup executor)

Create a new instance.

Parameters
executor:EventExecutorGroup

the EventExecutorGroup to use for the TrafficCounter.

Method Detail

accelerationFactorback to summary
public float accelerationFactor()
Returns:float

the current acceleration factor

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

Overrides io.netty5.handler.traffic.AbstractTrafficShapingHandler.channelRead.

Overrides default io.netty5.channel.ChannelHandler.channelRead.

Doc from io.netty5.channel.ChannelHandler.channelRead.

Invoked when the current Channel has read a message from the peer.

Annotations
@Override
channelTrafficCountersback to summary
public Collection<TrafficCounter> channelTrafficCounters()

To allow for instance doAccounting to use the TrafficCounter per channel.

Returns:Collection<TrafficCounter>

the list of TrafficCounters that exists at the time of the call.

checkWaitReadTimeback to summary
protected long checkWaitReadTime(final ChannelHandlerContext ctx, long wait, final long now)

Overrides io.netty5.handler.traffic.AbstractTrafficShapingHandler.checkWaitReadTime.

Doc from io.netty5.handler.traffic.AbstractTrafficShapingHandler.checkWaitReadTime.

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

Annotations
@Override
computeBalancedWaitback to summary
private long computeBalancedWait(float maxLocal, float maxGlobal, long wait)
computeDeviationCumulativeBytesback to summary
private void computeDeviationCumulativeBytes()
configureChannelback to summary
public void configureChannel(long newWriteLimit, long newReadLimit)
Parameters
newWriteLimit:long

Channel write limit

newReadLimit:long

Channel read limit

createGlobalTrafficCounterback to summary
pack-priv void createGlobalTrafficCounter(EventExecutorGroup executor)

Create the global TrafficCounter

doAccountingback to summary
protected void doAccounting(TrafficCounter counter)

Overrides io.netty5.handler.traffic.AbstractTrafficShapingHandler.doAccounting.

Doc from io.netty5.handler.traffic.AbstractTrafficShapingHandler.doAccounting.

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

Annotations
@Override
getMaxGlobalWriteSizeback to summary
public long getMaxGlobalWriteSize()
Returns:long

the maxGlobalWriteSize

getOrSetPerChannelback to summary
private GlobalChannelTrafficShapingHandler.PerChannel getOrSetPerChannel(ChannelHandlerContext ctx)
getReadChannelLimitback to summary
public long getReadChannelLimit()
Returns:long

Channel read limit

getWriteChannelLimitback to summary
public long getWriteChannelLimit()
Returns:long

Channel write limit

handlerAddedback to summary
public void handlerAdded(ChannelHandlerContext ctx) throws Exception

Overrides default io.netty5.channel.ChannelHandler.handlerAdded.

Doc from io.netty5.channel.ChannelHandler.handlerAdded.

Gets called after the ChannelHandler was added to the actual context and it's ready to handle events.

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

Overrides io.netty5.handler.traffic.AbstractTrafficShapingHandler.handlerRemoved.

Overrides default io.netty5.channel.ChannelHandler.handlerRemoved.

Doc from io.netty5.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
protected void informReadOperation(final ChannelHandlerContext ctx, final long now)

Overrides io.netty5.handler.traffic.AbstractTrafficShapingHandler.informReadOperation.

Doc from io.netty5.handler.traffic.AbstractTrafficShapingHandler.informReadOperation.

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

Parameters
now:long

the relative now time in ms

Annotations
@Override
isSharableback to summary
public boolean isSharable()

Overrides default io.netty5.channel.ChannelHandler.isSharable.

Doc from io.netty5.channel.ChannelHandler.isSharable.

Returns true if this handler is sharable and thus can be added to more than one ChannelPipeline. By default, this method returns false. If this method returns false, you have to create a new handler instance every time you add it to a pipeline because it has unshared state such as member variables.

Annotations
@Override
maxDeviationback to summary
public float maxDeviation()
Returns:float

the current max deviation

maximumCumulativeReadBytesback to summary
protected long maximumCumulativeReadBytes()
maximumCumulativeWrittenBytesback to summary
protected long maximumCumulativeWrittenBytes()
queuesSizeback to summary
public long queuesSize()
Returns:long

the global size of the buffers for all queues.

releaseback to summary
public final void release()

Release all internal resources of this instance.

sendAllValidback to summary
private void sendAllValid(final ChannelHandlerContext ctx, final GlobalChannelTrafficShapingHandler.PerChannel perChannel, final long now)
setMaxDeviationback to summary
public void setMaxDeviation(float maxDeviation, float slowDownFactor, float accelerationFactor)
Parameters
maxDeviation:float

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:float

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:float

the factor set as -x% to the too slow client (maximal value being 0, meaning no acceleration factor), default being -10% (-0.1).

setMaxGlobalWriteSizeback to summary
public 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.

Parameters
maxGlobalWriteSize:long

the maximum Global Write Size allowed in the buffer globally for all channels before write suspended is set.

setReadChannelLimitback to summary
public void setReadChannelLimit(long readLimit)
Parameters
readLimit:long

Channel read limit

setWriteChannelLimitback to summary
public void setWriteChannelLimit(long writeLimit)
Parameters
writeLimit:long

Channel write limit

slowDownFactorback to summary
public float slowDownFactor()
Returns:float

the current slow down factor

submitWriteback to summary
protected void submitWrite(final ChannelHandlerContext ctx, final Object msg, final long size, final long writedelay, final long now, final Promise<Void> promise)

Implements abstract io.netty5.handler.traffic.AbstractTrafficShapingHandler.submitWrite.

Annotations
@Override
toStringback to summary
public String toString()

Overrides io.netty5.handler.traffic.AbstractTrafficShapingHandler.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object.

Returns:String

a string representation of the object

Annotations
@Override
userDefinedWritabilityIndexback to summary
protected int userDefinedWritabilityIndex()

Overrides io.netty5.handler.traffic.AbstractTrafficShapingHandler.userDefinedWritabilityIndex.

Returns:int

Doc from io.netty5.handler.traffic.AbstractTrafficShapingHandler.userDefinedWritabilityIndex.

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.

Annotations
@Override
writeback to summary
public Future<Void> write(final ChannelHandlerContext ctx, final Object msg)

Overrides io.netty5.handler.traffic.AbstractTrafficShapingHandler.write.

Overrides default io.netty5.channel.ChannelHandler.write.

Doc from io.netty5.channel.ChannelHandler.write.

Called once a write operation is made. The write operation will write the messages through the ChannelPipeline. Those are then ready to be flushed to the actual Channel once Channel#flush() is called.

Parameters
ctx:ChannelHandlerContext

the ChannelHandlerContext for which the write operation is made

msg:Object

the message to write

Returns:Future<Void>

the Future which will be notified once the operation completes.

Annotations
@Override
io.netty5.handler.traffic back to summary

pack-priv final Class GlobalChannelTrafficShapingHandler.PerChannel

extends Object
Class Inheritance
  • java.lang.Object
  • io.netty5.handler.traffic.GlobalChannelTrafficShapingHandler.PerChannel

Field Summary

Modifier and TypeField and Description
pack-priv TrafficCounter
pack-priv long
pack-priv long
pack-priv ArrayDeque<GlobalChannelTrafficShapingHandler.ToSend>
pack-priv long

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

channelTrafficCounterback to summary
pack-priv TrafficCounter channelTrafficCounter
lastReadTimestampback to summary
pack-priv long lastReadTimestamp
lastWriteTimestampback to summary
pack-priv long lastWriteTimestamp
messagesQueueback to summary
pack-priv ArrayDeque<GlobalChannelTrafficShapingHandler.ToSend> messagesQueue
queueSizeback to summary
pack-priv long queueSize

Constructor Detail

PerChannelback to summary
pack-priv PerChannel()
io.netty5.handler.traffic back to summary

private final Class GlobalChannelTrafficShapingHandler.ToSend

extends Object
Class Inheritance
  • java.lang.Object
  • io.netty5.handler.traffic.GlobalChannelTrafficShapingHandler.ToSend

Field Summary

Modifier and TypeField and Description
pack-priv final Promise<Void>
pack-priv final long
pack-priv final long
pack-priv final Object

Constructor Summary

AccessConstructor and Description
private
ToSend(final long delay, final Object toSend, final long size, final Promise<Void> promise)

Method Summary

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

promiseback to summary
pack-priv final Promise<Void> promise
relativeTimeActionback to summary
pack-priv final long relativeTimeAction
sizeback to summary
pack-priv final long size
toSendback to summary
pack-priv final Object toSend

Constructor Detail

ToSendback to summary
private ToSend(final long delay, final Object toSend, final long size, final Promise<Void> promise)