Class ExponentialBackoff

java.lang.Object
com.gmt2001.ratelimiters.ExponentialBackoff

public class ExponentialBackoff extends Object
Handles backoff timing using an exponentially increasing duration strategy.
Author:
gmt2001
  • Constructor Details

    • ExponentialBackoff

      public ExponentialBackoff(Duration minInterval, Duration maxInterval)
      Constructor
      Parameters:
      minInterval - Minimum backoff interval
      maxInterval - Maximum backoff interval
    • ExponentialBackoff

      public ExponentialBackoff(Duration minInterval, Duration maxInterval, Duration resetInterval)
      Constructor
      Parameters:
      minInterval - Minimum backoff interval
      maxInterval - Maximum backoff interval
      resetInterval - Time since last backoff until an auto-reset occurs; null to disable
    • ExponentialBackoff

      public ExponentialBackoff(long minIntervalMS, long maxIntervalMS)
      Constructor
      Parameters:
      minIntervalMS - Minimum backoff interval, in MS
      maxIntervalMS - Maximum backoff interval, in MS
    • ExponentialBackoff

      public ExponentialBackoff(long minIntervalMS, long maxIntervalMS, long resetIntervalMS)
      Constructor
      Parameters:
      minIntervalMS - Minimum backoff interval, in MS
      maxIntervalMS - Maximum backoff interval, in MS
      resetIntervalMS - Time since last backoff until an auto-reset occurs; -1 to disable
  • Method Details

    • BackoffOnce

      public void BackoffOnce()
      Blocks with Thread.sleep until the next interval, if not already backing off
    • Backoff

      public void Backoff()
      Blocks with Thread.sleep until the next interval
    • BackoffOnceAsync

      public void BackoffOnceAsync(Runnable command)
      Calls the specified Runnable once the next interval expires, if not already backing off
      Parameters:
      command - The Runnable to callback
    • BackoffAsync

      public void BackoffAsync(Runnable command)
      Calls the specified Runnable once the next interval expires
      Parameters:
      command - The Runnable to callback
    • Reset

      public void Reset()
      Resets the backoff to use the minimum values on the next call
    • ResetIn

      public void ResetIn(Duration duration)
      Resets the backoff to use the minimum values on the next call, but only if duration expires without CancelReset being called
      Parameters:
      duration - the duration to wait before resetting this ExponentialBackoff
    • CancelReset

      public void CancelReset()
      Cancels a scheduled reset initiated by a call to ResetIn
    • GetIsBackingOff

      public boolean GetIsBackingOff()
      Returns whether a backoff is currently in progress
      Returns:
      true if a backoff is executing; false otherwise
    • GetLastBackoff

      public Instant GetLastBackoff()
      Returns the last timestamp when a backoff was completed
      Returns:
      The timestamp
    • GetNextInterval

      public long GetNextInterval()
      Determines and returns the next interval to backoff for
      Returns:
      The next interval that will be used when Backoff() or BackoffAsync(command) is called, in milliseconds
    • GetTotalIterations

      public int GetTotalIterations()
      Returns the total number of times the backoff has been used since the last reset
      Returns:
      The total
    • IsAtMaxInterval

      public boolean IsAtMaxInterval()
      Returns true if the previous interval was equal to the max interval
      Returns:
      true if the previous interval was equal to the max interval