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

public Class GlobalTrafficShapingHandler

extends AbstractTrafficShapingHandler
Class Inheritance
Annotations
@Sharable
Imports
io.netty.buffer.ByteBuf, io.netty.channel.ChannelHandler.Sharable, .Channel, .ChannelHandlerContext, .ChannelPromise, io.netty.util.concurrent.EventExecutor, io.netty.util.internal.ObjectUtil, .PlatformDependent, java.util.ArrayDeque, java.util.concurrent.ConcurrentMap, .ScheduledExecutorService, .TimeUnit, java.util.concurrent.atomic.AtomicLong

This implementation of the AbstractTrafficShapingHandler is for global traffic shaping, that is to say a global limitation of the bandwidth, whatever the number of opened channels.

Note the index used in OutboundBuffer.setUserDefinedWritability(index, boolean) is 2.

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
private static class
private static class

Field Summary

Modifier and TypeField and Description
private final ConcurrentMap<Integer, GlobalTrafficShapingHandler.PerChannel>
channelQueues

All queues per channel

pack-priv 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

Inherited from io.netty.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
GlobalTrafficShapingHandler(ScheduledExecutorService executor, 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.
maxTime
)

Create a new instance.

public
GlobalTrafficShapingHandler(ScheduledExecutorService executor, 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
)

Create a new instance using default max time as delay allowed value of 15000 ms.

public
GlobalTrafficShapingHandler(ScheduledExecutorService executor, long
0 or a limit in bytes/s
writeLimit
,
long
0 or a limit in bytes/s
readLimit
)

Create a new instance using default Check Interval value of 1000 ms and default max time as delay allowed value of 15000 ms.

public
GlobalTrafficShapingHandler(ScheduledExecutorService 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 using default max time as delay allowed value of 15000 ms and no limit.

public
GlobalTrafficShapingHandler(EventExecutor
the EventExecutor to use for the TrafficCounter.
executor
)

Create a new instance using default Check Interval value of 1000 ms and default max time as delay allowed value of 15000 ms and no limit.

Method Summary

Modifier and TypeMethod and Description
pack-priv long
checkWaitReadTime(final ChannelHandlerContext ctx, long
the wait delay computed in ms
wait
,
final long
the relative now time in ms
now
)

Overrides io.netty.handler.traffic.AbstractTrafficShapingHandler.checkWaitReadTime.

Method overridden in GTSH to take into account specific timer for the channel.
pack-priv void
createGlobalTrafficCounter(ScheduledExecutorService executor)

Create the global TrafficCounter.

public long

Returns:

the maxGlobalWriteSize default value being 400 MB.
getMaxGlobalWriteSize
()

private GlobalTrafficShapingHandler.PerChannel
public void
handlerAdded(ChannelHandlerContext ctx)

Overrides io.netty.channel.ChannelHandlerAdapter.handlerAdded.

Implements io.netty.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.netty.handler.traffic.AbstractTrafficShapingHandler.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
)

Overrides io.netty.handler.traffic.AbstractTrafficShapingHandler.informReadOperation.

Method overridden in GTSH to take into account specific timer for the channel.
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
sendAllValid(final ChannelHandlerContext ctx, final GlobalTrafficShapingHandler.PerChannel perChannel, final long now)

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

pack-priv void
submitWrite(final ChannelHandlerContext ctx, final Object msg, final long size, final long writedelay, final long now, final ChannelPromise promise)

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

protected int
Inherited from io.netty.handler.traffic.AbstractTrafficShapingHandler:
calculateSizechannelReadchannelRegisteredcheckWriteSuspendconfigureconfigureconfiguredoAccountinggetCheckIntervalgetMaxTimeWaitgetMaxWriteDelaygetMaxWriteSizegetReadLimitgetWriteLimitisHandlerActivereadreleaseReadSuspendedreleaseWriteSuspendedsetCheckIntervalsetMaxTimeWaitsetMaxWriteDelaysetMaxWriteSizesetReadLimitsetTrafficCountersetUserDefinedWritabilitysetWriteLimitsubmitWritetoStringtrafficCounterwrite

Field Detail

channelQueuesback to summary
private final ConcurrentMap<Integer, GlobalTrafficShapingHandler.PerChannel> channelQueues

All queues per channel

maxGlobalWriteSizeback to summary
pack-priv 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

Constructor Detail

GlobalTrafficShapingHandlerback to summary
public GlobalTrafficShapingHandler(ScheduledExecutorService executor, long writeLimit, long readLimit, long checkInterval, long maxTime)

Create a new instance.

Parameters
executor:ScheduledExecutorService

the ScheduledExecutorService to use for the TrafficCounter.

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.

GlobalTrafficShapingHandlerback to summary
public GlobalTrafficShapingHandler(ScheduledExecutorService executor, long writeLimit, long readLimit, long checkInterval)

Create a new instance using default max time as delay allowed value of 15000 ms.

Parameters
executor:ScheduledExecutorService

the ScheduledExecutorService to use for the TrafficCounter.

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.

GlobalTrafficShapingHandlerback to summary
public GlobalTrafficShapingHandler(ScheduledExecutorService executor, long writeLimit, long readLimit)

Create a new instance using default Check Interval value of 1000 ms and default max time as delay allowed value of 15000 ms.

Parameters
executor:ScheduledExecutorService

the ScheduledExecutorService to use for the TrafficCounter.

writeLimit:long

0 or a limit in bytes/s

readLimit:long

0 or a limit in bytes/s

GlobalTrafficShapingHandlerback to summary
public GlobalTrafficShapingHandler(ScheduledExecutorService executor, long checkInterval)

Create a new instance using default max time as delay allowed value of 15000 ms and no limit.

Parameters
executor:ScheduledExecutorService

the ScheduledExecutorService 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.

GlobalTrafficShapingHandlerback to summary
public GlobalTrafficShapingHandler(EventExecutor executor)

Create a new instance using default Check Interval value of 1000 ms and default max time as delay allowed value of 15000 ms and no limit.

Parameters
executor:EventExecutor

the EventExecutor to use for the TrafficCounter.

Method Detail

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

Overrides io.netty.handler.traffic.AbstractTrafficShapingHandler.checkWaitReadTime.

Doc from io.netty.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
createGlobalTrafficCounterback to summary
pack-priv void createGlobalTrafficCounter(ScheduledExecutorService executor)

Create the global TrafficCounter.

getMaxGlobalWriteSizeback to summary
public long getMaxGlobalWriteSize()
Returns:long

the maxGlobalWriteSize default value being 400 MB.

getOrSetPerChannelback to summary
private GlobalTrafficShapingHandler.PerChannel getOrSetPerChannel(ChannelHandlerContext ctx)
handlerAddedback to summary
public void handlerAdded(ChannelHandlerContext ctx) throws Exception

Overrides io.netty.channel.ChannelHandlerAdapter.handlerAdded.

Implements io.netty.channel.ChannelHandler.handlerAdded.

Doc from io.netty.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.netty.handler.traffic.AbstractTrafficShapingHandler.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)

Overrides io.netty.handler.traffic.AbstractTrafficShapingHandler.informReadOperation.

Doc from io.netty.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
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 GlobalTrafficShapingHandler.PerChannel perChannel, final long now)
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, default value being 400 MB.

submitWriteback to summary
pack-priv void submitWrite(final ChannelHandlerContext ctx, final Object msg, final long size, final long writedelay, final long now, final ChannelPromise promise)

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

Annotations
@Override
userDefinedWritabilityIndexback to summary
protected int userDefinedWritabilityIndex()

Overrides io.netty.handler.traffic.AbstractTrafficShapingHandler.userDefinedWritabilityIndex.

Returns:int

Doc from io.netty.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
io.netty.handler.traffic back to summary

private final Class GlobalTrafficShapingHandler.PerChannel

extends Object
Class Inheritance
  • java.lang.Object
  • io.netty.handler.traffic.GlobalTrafficShapingHandler.PerChannel

Field Summary

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

Constructor Summary

AccessConstructor and Description
private

Method Summary

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

lastReadTimestampback to summary
pack-priv long lastReadTimestamp
lastWriteTimestampback to summary
pack-priv long lastWriteTimestamp
messagesQueueback to summary
pack-priv ArrayDeque<GlobalTrafficShapingHandler.ToSend> messagesQueue
queueSizeback to summary
pack-priv long queueSize

Constructor Detail

PerChannelback to summary
private PerChannel()
io.netty.handler.traffic back to summary

private final Class GlobalTrafficShapingHandler.ToSend

extends Object
Class Inheritance

Field Summary

Modifier and TypeField and Description
pack-priv final ChannelPromise
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 ChannelPromise promise)

Method Summary

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

promiseback to summary
pack-priv final ChannelPromise 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 ChannelPromise promise)