Class Backoff

java.lang.Object
io.aiven.commons.timing.Backoff

public class Backoff extends Object
Performs a delay based on the number of successive delay() or cleanDelay() calls without a reset(). Delay increases exponentially but never exceeds the time remaining by more than 0.512 seconds. There are several use cases: Case 1: There is a hard limit to the total amount of time spent waiting and processing. In this case a Timer is initialized to the maximum time that the processing may take. The timer will specify the amount of time that remains for the process. During processing
  • As long as the timer returns a value > 0 the backoff may sleep.
  • If the backoff sleep time is greater than the time remaining the timer is aborted and backoff does not sleep. In this use case the Timer.isExpired() method should be checked after backoff returns to see if the timer has expired or been aborted.
Case 2: Delays of up to X milliseconds are desired. After X is reached subsequent delays should be for X milliseconds until the backoff is reset. In this case the BackoffConfig.getSupplierOfTimeRemaining() should return X on every call and the BackoffConfig.applyTimerRule() should return false. During processing Backoff will always sleep, except in very early backoff calls where jitter overcomes the delay. Case 3: An action should be taken as many times as possible within the allowed time. A timer is set and used to specify the time remaining as in Case 1 above. When the action is successful the reset() method is called. This resets the internal max delay to the current limit specified by the timer.
  • Field Details

    • MAX_JITTER

      public static final int MAX_JITTER
      The maximum jitter random number. Should be a power of 2 for speed.
      See Also:
    • JITTER_SUBTRAHEND

      public static final int JITTER_SUBTRAHEND
      The value subtracted from the jitter to center it.
      See Also:
    • timeRemaining

      protected final SupplierOfLong timeRemaining
      A supplier of the time remaining (in milliseconds) on the overriding timer.
    • abortTrigger

      protected final AbortTrigger abortTrigger
      A function to call to abort the timer.
  • Constructor Details

    • Backoff

      public Backoff(BackoffConfig config)
      Constructor.
      Parameters:
      config - The configuration for the backoff.
  • Method Details

    • reset

      public final void reset()
      Reset the backoff time so that delay is again at the minimum.
    • setMinimumDelay

      public void setMinimumDelay(Duration duration)
      Set the minimum wait time. Actual delay will be the closest power of 2 such that 2^x >= duration.
      Parameters:
      duration - the minimum wait time.
    • estimatedDelay

      public long estimatedDelay()
      Calculates the delay without jitter.
      Returns:
      the number of milliseconds the delay will be.
    • getMaxJitter

      public int getMaxJitter()
      Calculates the range of jitter in milliseconds.
      Returns:
      the maximum jitter in milliseconds. jitter is +/- maximum jitter.
    • delay

      public void delay() throws InterruptedException
      Delay execution based on the number of times this method has been called.
      Throws:
      InterruptedException - If any thread interrupts this thread.
    • cleanDelay

      public void cleanDelay()
      Like delay() but swallows the InterruptedException.
    • toString

      public String toString()
      Overrides:
      toString in class Object