Class TrafficCounter

java.lang.Object
io.netty.handler.traffic.TrafficCounter
Direct Known Subclasses:
GlobalChannelTrafficCounter

public class TrafficCounter extends java.lang.Object
Counts the number of read and written bytes for rate-limiting traffic.

It computes the statistics for both inbound and outbound traffic periodically at the given checkInterval, and calls the AbstractTrafficShapingHandler.doAccounting(TrafficCounter) method back. If the checkInterval is 0, no accounting will be done and statistics will only be computed at each receive or write operation.

  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    private final class 
    Class to implement monitoring at fix delay
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    (package private) final java.util.concurrent.atomic.AtomicLong
    Delay between two captures
    private final java.util.concurrent.atomic.AtomicLong
    Long life read bytes
    private final java.util.concurrent.atomic.AtomicLong
    Long life written bytes
    private final java.util.concurrent.atomic.AtomicLong
    Current read bytes
    private final java.util.concurrent.atomic.AtomicLong
    Current written bytes
    (package private) final java.util.concurrent.ScheduledExecutorService
    Executor that will run the monitor
    private long
    Last Time where cumulative bytes where reset to zero: this time is a real EPOC time (informative only)
    private long
    Last read bytes number during last check interval
    private long
    Last reading time during last check interval
    private long
    Last reading bandwidth
    (package private) final java.util.concurrent.atomic.AtomicLong
    Last Time Check taken
    private long
    Last writing bandwidth
    private long
    Last future writing time during last check interval
    private long
    Last written bytes number during last check interval
    private static final InternalLogger
     
    (package private) java.lang.Runnable
    Monitor created once in start()
    (package private) boolean
    Is Monitor active
    (package private) final java.lang.String
    Name of this Monitor
    private long
    Last reading delay during current check interval
    private long
    Real writing bandwidth
    private final java.util.concurrent.atomic.AtomicLong
    Real written bytes
    (package private) java.util.concurrent.ScheduledFuture<?>
    used in stop() to cancel the timer
    (package private) final AbstractTrafficShapingHandler
    The associated TrafficShapingHandler
    private long
    Last writing time during current check interval
  • Constructor Summary

    Constructors
    Constructor
    Description
    TrafficCounter(AbstractTrafficShapingHandler trafficShapingHandler, java.util.concurrent.ScheduledExecutorService executor, java.lang.String name, long checkInterval)
    Constructor with the AbstractTrafficShapingHandler that hosts it, the Timer to use, its name, the checkInterval between two computations in millisecond.
    TrafficCounter(java.util.concurrent.ScheduledExecutorService executor, java.lang.String name, long checkInterval)
    Constructor with the AbstractTrafficShapingHandler that hosts it, the ScheduledExecutorService to use, its name, the checkInterval between two computations in milliseconds.
  • Method Summary

    Modifier and Type
    Method
    Description
    (package private) void
    Computes counters for Real Write.
    (package private) void
    Computes counters for Read.
    (package private) void
    Computes counters for Write.
    long
     
    void
    configure(long newCheckInterval)
    Change checkInterval between two computations in millisecond.
    long
     
    long
     
    long
     
    long
     
    long
     
    java.util.concurrent.atomic.AtomicLong
     
    private void
    init(long checkInterval)
     
    long
     
    long
     
    long
     
    long
     
    long
     
    long
     
    static long
     
    java.lang.String
     
    long
    readTimeToWait(long size, long limitTraffic, long maxTime)
    Deprecated.
    long
    readTimeToWait(long size, long limitTraffic, long maxTime, long now)
    Returns the time to wait (if any) for the given length message, using the given limitTraffic and the max wait time.
    (package private) void
    resetAccounting(long newLastTime)
    Reset the accounting on Read and Write.
    void
    Reset both read and written cumulative bytes counters and the associated absolute time from System.currentTimeMillis().
    void
    Start the monitoring process.
    void
    Stop the monitoring process.
    java.lang.String
     
    long
    writeTimeToWait(long size, long limitTraffic, long maxTime)
    Deprecated.
    long
    writeTimeToWait(long size, long limitTraffic, long maxTime, long now)
    Returns the time to wait (if any) for the given length message, using the given limitTraffic and the max wait time.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Field Details

    • logger

      private static final InternalLogger logger
    • currentWrittenBytes

      private final java.util.concurrent.atomic.AtomicLong currentWrittenBytes
      Current written bytes
    • currentReadBytes

      private final java.util.concurrent.atomic.AtomicLong currentReadBytes
      Current read bytes
    • writingTime

      private long writingTime
      Last writing time during current check interval
    • readingTime

      private long readingTime
      Last reading delay during current check interval
    • cumulativeWrittenBytes

      private final java.util.concurrent.atomic.AtomicLong cumulativeWrittenBytes
      Long life written bytes
    • cumulativeReadBytes

      private final java.util.concurrent.atomic.AtomicLong cumulativeReadBytes
      Long life read bytes
    • lastCumulativeTime

      private long lastCumulativeTime
      Last Time where cumulative bytes where reset to zero: this time is a real EPOC time (informative only)
    • lastWriteThroughput

      private long lastWriteThroughput
      Last writing bandwidth
    • lastReadThroughput

      private long lastReadThroughput
      Last reading bandwidth
    • lastTime

      final java.util.concurrent.atomic.AtomicLong lastTime
      Last Time Check taken
    • lastWrittenBytes

      private volatile long lastWrittenBytes
      Last written bytes number during last check interval
    • lastReadBytes

      private volatile long lastReadBytes
      Last read bytes number during last check interval
    • lastWritingTime

      private volatile long lastWritingTime
      Last future writing time during last check interval
    • lastReadingTime

      private volatile long lastReadingTime
      Last reading time during last check interval
    • realWrittenBytes

      private final java.util.concurrent.atomic.AtomicLong realWrittenBytes
      Real written bytes
    • realWriteThroughput

      private long realWriteThroughput
      Real writing bandwidth
    • checkInterval

      final java.util.concurrent.atomic.AtomicLong checkInterval
      Delay between two captures
    • name

      final java.lang.String name
      Name of this Monitor
    • trafficShapingHandler

      final AbstractTrafficShapingHandler trafficShapingHandler
      The associated TrafficShapingHandler
    • executor

      final java.util.concurrent.ScheduledExecutorService executor
      Executor that will run the monitor
    • monitor

      java.lang.Runnable monitor
      Monitor created once in start()
    • scheduledFuture

      volatile java.util.concurrent.ScheduledFuture<?> scheduledFuture
      used in stop() to cancel the timer
    • monitorActive

      volatile boolean monitorActive
      Is Monitor active
  • Constructor Details

    • TrafficCounter

      public TrafficCounter(java.util.concurrent.ScheduledExecutorService executor, java.lang.String name, long checkInterval)
      Constructor with the AbstractTrafficShapingHandler that hosts it, the ScheduledExecutorService to use, its name, the checkInterval between two computations in milliseconds.
      Parameters:
      executor - the underlying executor service for scheduling checks, might be null when used from GlobalChannelTrafficCounter.
      name - the name given to this monitor.
      checkInterval - the checkInterval in millisecond between two computations.
    • TrafficCounter

      public TrafficCounter(AbstractTrafficShapingHandler trafficShapingHandler, java.util.concurrent.ScheduledExecutorService executor, java.lang.String name, long checkInterval)
      Constructor with the AbstractTrafficShapingHandler that hosts it, the Timer to use, its name, the checkInterval between two computations in millisecond.
      Parameters:
      trafficShapingHandler - the associated AbstractTrafficShapingHandler.
      executor - the underlying executor service for scheduling checks, might be null when used from GlobalChannelTrafficCounter.
      name - the name given to this monitor.
      checkInterval - the checkInterval in millisecond between two computations.
  • Method Details

    • milliSecondFromNano

      public static long milliSecondFromNano()
      Returns:
      the time in ms using nanoTime, so not real EPOCH time but elapsed time in ms.
    • start

      public void start()
      Start the monitoring process.
    • stop

      public void stop()
      Stop the monitoring process.
    • resetAccounting

      void resetAccounting(long newLastTime)
      Reset the accounting on Read and Write.
      Parameters:
      newLastTime - the milliseconds unix timestamp that we should be considered up-to-date for.
    • init

      private void init(long checkInterval)
    • configure

      public void configure(long newCheckInterval)
      Change checkInterval between two computations in millisecond.
      Parameters:
      newCheckInterval - The new check interval (in milliseconds)
    • bytesRecvFlowControl

      void bytesRecvFlowControl(long recv)
      Computes counters for Read.
      Parameters:
      recv - the size in bytes to read
    • bytesWriteFlowControl

      void bytesWriteFlowControl(long write)
      Computes counters for Write.
      Parameters:
      write - the size in bytes to write
    • bytesRealWriteFlowControl

      void bytesRealWriteFlowControl(long write)
      Computes counters for Real Write.
      Parameters:
      write - the size in bytes to write
    • checkInterval

      public long checkInterval()
      Returns:
      the current checkInterval between two computations of traffic counter in millisecond.
    • lastReadThroughput

      public long lastReadThroughput()
      Returns:
      the Read Throughput in bytes/s computes in the last check interval.
    • lastWriteThroughput

      public long lastWriteThroughput()
      Returns:
      the Write Throughput in bytes/s computes in the last check interval.
    • lastReadBytes

      public long lastReadBytes()
      Returns:
      the number of bytes read during the last check Interval.
    • lastWrittenBytes

      public long lastWrittenBytes()
      Returns:
      the number of bytes written during the last check Interval.
    • currentReadBytes

      public long currentReadBytes()
      Returns:
      the current number of bytes read since the last checkInterval.
    • currentWrittenBytes

      public long currentWrittenBytes()
      Returns:
      the current number of bytes written since the last check Interval.
    • lastTime

      public long lastTime()
      Returns:
      the Time in millisecond of the last check as of System.currentTimeMillis().
    • cumulativeWrittenBytes

      public long cumulativeWrittenBytes()
      Returns:
      the cumulativeWrittenBytes
    • cumulativeReadBytes

      public long cumulativeReadBytes()
      Returns:
      the cumulativeReadBytes
    • lastCumulativeTime

      public long lastCumulativeTime()
      Returns:
      the lastCumulativeTime in millisecond as of System.currentTimeMillis() when the cumulative counters were reset to 0.
    • getRealWrittenBytes

      public java.util.concurrent.atomic.AtomicLong getRealWrittenBytes()
      Returns:
      the realWrittenBytes
    • getRealWriteThroughput

      public long getRealWriteThroughput()
      Returns:
      the realWriteThroughput
    • resetCumulativeTime

      public void resetCumulativeTime()
      Reset both read and written cumulative bytes counters and the associated absolute time from System.currentTimeMillis().
    • name

      public java.lang.String name()
      Returns:
      the name of this TrafficCounter.
    • readTimeToWait

      @Deprecated public long readTimeToWait(long size, long limitTraffic, long maxTime)
      Deprecated.
      Returns the time to wait (if any) for the given length message, using the given limitTraffic and the max wait time.
      Parameters:
      size - the recv size
      limitTraffic - the traffic limit in bytes per second.
      maxTime - the max time in ms to wait in case of excess of traffic.
      Returns:
      the current time to wait (in ms) if needed for Read operation.
    • readTimeToWait

      public long readTimeToWait(long size, long limitTraffic, long maxTime, long now)
      Returns the time to wait (if any) for the given length message, using the given limitTraffic and the max wait time.
      Parameters:
      size - the recv size
      limitTraffic - the traffic limit in bytes per second
      maxTime - the max time in ms to wait in case of excess of traffic.
      now - the current time
      Returns:
      the current time to wait (in ms) if needed for Read operation.
    • writeTimeToWait

      @Deprecated public long writeTimeToWait(long size, long limitTraffic, long maxTime)
      Deprecated.
      Returns the time to wait (if any) for the given length message, using the given limitTraffic and the max wait time.
      Parameters:
      size - the write size
      limitTraffic - the traffic limit in bytes per second.
      maxTime - the max time in ms to wait in case of excess of traffic.
      Returns:
      the current time to wait (in ms) if needed for Write operation.
    • writeTimeToWait

      public long writeTimeToWait(long size, long limitTraffic, long maxTime, long now)
      Returns the time to wait (if any) for the given length message, using the given limitTraffic and the max wait time.
      Parameters:
      size - the write size
      limitTraffic - the traffic limit in bytes per second.
      maxTime - the max time in ms to wait in case of excess of traffic.
      now - the current time
      Returns:
      the current time to wait (in ms) if needed for Write operation.
    • toString

      public java.lang.String toString()
      Overrides:
      toString in class java.lang.Object