Class AbstractTrafficShapingHandler

  • All Implemented Interfaces:
    io.netty5.channel.ChannelHandler
    Direct Known Subclasses:
    ChannelTrafficShapingHandler, GlobalChannelTrafficShapingHandler, GlobalTrafficShapingHandler

    public abstract class AbstractTrafficShapingHandler
    extends Object
    implements io.netty5.channel.ChannelHandler

    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:

    • configure allows you to change read or write limits, or the checkInterval
    • getTrafficCounter allows you to have access to the TrafficCounter and so to stop or start the monitoring, to change the checkInterval directly, or to have access to its values.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods 
      Modifier and Type Method Description
      protected long calculateSize​(Object msg)
      Calculate the size of the given Object.
      void channelRead​(io.netty5.channel.ChannelHandlerContext ctx, Object msg)  
      void channelRegistered​(io.netty5.channel.ChannelHandlerContext ctx)  
      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()  
      long getWriteLimit()  
      void handlerRemoved​(io.netty5.channel.ChannelHandlerContext ctx)  
      protected static boolean isHandlerActive​(io.netty5.channel.ChannelHandlerContext ctx)  
      void read​(io.netty5.channel.ChannelHandlerContext ctx)  
      void setCheckInterval​(long checkInterval)  
      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 that this limit is a best effort on memory limitation to prevent Out Of Memory Exception.
      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.
      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​(io.netty5.channel.ChannelHandlerContext ctx, Object msg, long delay, io.netty5.util.concurrent.Promise<Void> promise)
      Deprecated.
      String toString()  
      TrafficCounter trafficCounter()  
      protected int userDefinedWritabilityIndex()  
      io.netty5.util.concurrent.Future<Void> write​(io.netty5.channel.ChannelHandlerContext ctx, Object msg)  
      • Methods inherited from interface io.netty5.channel.ChannelHandler

        bind, channelActive, channelExceptionCaught, channelInactive, channelInboundEvent, channelReadComplete, channelShutdown, channelUnregistered, channelWritabilityChanged, close, connect, deregister, disconnect, flush, handlerAdded, isSharable, pendingOutboundBytes, register, sendOutboundEvent, shutdown
    • Field Detail

      • DEFAULT_CHECK_INTERVAL

        public static final long DEFAULT_CHECK_INTERVAL
        Default delay between two checks: 1s
        See Also:
        Constant Field Values
      • DEFAULT_MAX_TIME

        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
        See Also:
        Constant Field Values
      • trafficCounter

        protected TrafficCounter trafficCounter
        Traffic Counter
      • maxTime

        protected volatile long maxTime
        Max delay in wait
      • checkInterval

        protected volatile long checkInterval
        Delay between two performance snapshots
    • Constructor Detail

      • AbstractTrafficShapingHandler

        protected AbstractTrafficShapingHandler​(long writeLimit,
                                                long readLimit,
                                                long checkInterval,
                                                long maxTime)
        Parameters:
        writeLimit - 0 or a limit in bytes/s
        readLimit - 0 or a limit in bytes/s
        checkInterval - 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. Must be positive.
      • AbstractTrafficShapingHandler

        protected AbstractTrafficShapingHandler​(long writeLimit,
                                                long readLimit,
                                                long checkInterval)
        Constructor using default max time as delay allowed value of 15000L ms.
        Parameters:
        writeLimit - 0 or a limit in bytes/s
        readLimit - 0 or a limit in bytes/s
        checkInterval - The delay between two computations of performances for channels or 0 if no stats are to be computed.
      • AbstractTrafficShapingHandler

        protected AbstractTrafficShapingHandler​(long writeLimit,
                                                long readLimit)
        Constructor using default Check Interval value of 1000L ms and default max time as delay allowed value of 15000L ms.
        Parameters:
        writeLimit - 0 or a limit in bytes/s
        readLimit - 0 or a limit in bytes/s
      • AbstractTrafficShapingHandler

        protected AbstractTrafficShapingHandler()
        Constructor using NO LIMIT, default Check Interval value of 1000L ms and default max time as delay allowed value of 15000L ms.
      • AbstractTrafficShapingHandler

        protected AbstractTrafficShapingHandler​(long checkInterval)
        Constructor using NO LIMIT and default max time as delay allowed value of 15000L ms.
        Parameters:
        checkInterval - The delay between two computations of performances for channels or 0 if no stats are to be computed.
    • Method Detail

      • userDefinedWritabilityIndex

        protected int userDefinedWritabilityIndex()
        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.
      • configure

        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 - The new write limit (in bytes)
        newReadLimit - The new read limit (in bytes)
        newCheckInterval - The new check interval (in milliseconds)
      • configure

        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 - The new write limit (in bytes)
        newReadLimit - The new read limit (in bytes)
      • configure

        public void configure​(long newCheckInterval)
        Change the check interval.
        Parameters:
        newCheckInterval - The new check interval (in milliseconds)
      • getWriteLimit

        public long getWriteLimit()
        Returns:
        the writeLimit
      • setWriteLimit

        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 - the writeLimit to set
      • getReadLimit

        public long getReadLimit()
        Returns:
        the readLimit
      • setReadLimit

        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 - the readLimit to set
      • getCheckInterval

        public long getCheckInterval()
        Returns:
        the checkInterval
      • setCheckInterval

        public void setCheckInterval​(long checkInterval)
        Parameters:
        checkInterval - the interval in ms between each step check to set, default value being 1000 ms.
      • setMaxTimeWait

        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 - Max delay in wait, shall be less than TIME OUT in related protocol. Must be positive.
      • getMaxTimeWait

        public long getMaxTimeWait()
        Returns:
        the max delay in wait to prevent TIME OUT
      • getMaxWriteDelay

        public long getMaxWriteDelay()
        Returns:
        the maxWriteDelay
      • setMaxWriteDelay

        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 - the maximum Write Delay in ms in the buffer allowed before write suspension is set. Must be positive.
      • getMaxWriteSize

        public long getMaxWriteSize()
        Returns:
        the maxWriteSize default being 4194304L bytes.
      • setMaxWriteSize

        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 Future.addListener(future -> ...)

        Parameters:
        maxWriteSize - the maximum Write Size allowed in the buffer per channel before write suspended is set, default being 4194304L bytes.
      • doAccounting

        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 - the TrafficCounter that computes its performance
      • channelRead

        public void channelRead​(io.netty5.channel.ChannelHandlerContext ctx,
                                Object msg)
                         throws Exception
        Specified by:
        channelRead in interface io.netty5.channel.ChannelHandler
        Throws:
        Exception
      • handlerRemoved

        public void handlerRemoved​(io.netty5.channel.ChannelHandlerContext ctx)
                            throws Exception
        Specified by:
        handlerRemoved in interface io.netty5.channel.ChannelHandler
        Throws:
        Exception
      • isHandlerActive

        protected static boolean isHandlerActive​(io.netty5.channel.ChannelHandlerContext ctx)
      • read

        public void read​(io.netty5.channel.ChannelHandlerContext ctx)
        Specified by:
        read in interface io.netty5.channel.ChannelHandler
      • write

        public io.netty5.util.concurrent.Future<Void> write​(io.netty5.channel.ChannelHandlerContext ctx,
                                                            Object msg)
        Specified by:
        write in interface io.netty5.channel.ChannelHandler
      • submitWrite

        @Deprecated
        protected void submitWrite​(io.netty5.channel.ChannelHandlerContext ctx,
                                   Object msg,
                                   long delay,
                                   io.netty5.util.concurrent.Promise<Void> promise)
        Deprecated.
      • channelRegistered

        public void channelRegistered​(io.netty5.channel.ChannelHandlerContext ctx)
                               throws Exception
        Specified by:
        channelRegistered in interface io.netty5.channel.ChannelHandler
        Throws:
        Exception
      • trafficCounter

        public TrafficCounter trafficCounter()
        Returns:
        the current TrafficCounter (if channel is still connected)
      • calculateSize

        protected long calculateSize​(Object msg)
        Calculate the size of the given Object. This implementation supports Buffer and FileRegion. Sub-classes may override this.
        Parameters:
        msg - the msg for which the size should be calculated.
        Returns:
        size the size of the msg or -1 if unknown.