Class TrafficCounter

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

public class TrafficCounter extends 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.

  • Constructor Details

    • TrafficCounter

      public TrafficCounter(ScheduledExecutorService executor, 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, ScheduledExecutorService executor, 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.
    • configure

      public void configure(long newCheckInterval)
      Change checkInterval between two computations in millisecond.
      Parameters:
      newCheckInterval - The new check interval (in milliseconds)
    • 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 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 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 String toString()
      Overrides:
      toString in class Object