Class Backoff

java.lang.Object
com.tccc.kos.commons.util.Backoff

public class Backoff extends Object
General purpose backoff algorithm class. This is used to convert a count to delay for various delay based backoff use cases. This class supports multiple backoff algorithms based on the values in the BackoffConfig bean. This bean can be stored in the kOS config system allowing external system to adjust backoff parameters and even algorithms without needing to change code.

This class can be used multiple ways:

  • Internal count: Create an instance using BackoffConfig and let the bean track the counter. Use inc(), clear() and associated methods to manage the count and simply call getDelay() at any time to get the associated delay based on the count.
  • External count: Create an instance using BackoffConfig and call getDelay(count) with an externally managed count to return the associated delay. Useful in multi-threaded environments where there are multiple counts but a single config.
  • Static accessor: Call the static getDelay(config, count) method to get the associated delay. Avoids the need to create a Backoff object if a config and count are already available.

The following types use config properties as described below:

  • FIXED: A fixed delay that doesn't depend on the count provided. Simply returns baseDelayMs.
  • LINEAR: A delay that grows linearly based on the count. Returns baseDelayMs plus count * unitDelayMs where count is first limited to maxCount and the final delay is limited to maxDelayMs. If maxCount is zero, the default limit of 25 will be used. Max delay is only applied if greater than zero.
  • EXPONENTIAL: A delay that grows exponentially based on the count. Returns baseDelayMs plus unitDelayMs * Math.pow(unitMultipilier, count) where count is limited to maxCount and the final delay is limited to maxDelayMs. If maxCount is zero, the default limit of 10 will be used. A hard limit of 40 will be enforced. The power of the multiplier will stop if the value exceeds 30 bits or maxDelayMs to prevent overflow. If unitMultiplier is less than 1.0 then the default value of 1.5 will be used to avoid accelerating timeouts. Max delay is only applied if greater than zero.
Since:
1.0
  • Field Details

    • DEFAULT_LINEAR_MAX_COUNT

      public static final int DEFAULT_LINEAR_MAX_COUNT
      See Also:
    • DEFAULT_EXPONENTIAL_MAX_COUNT

      public static final int DEFAULT_EXPONENTIAL_MAX_COUNT
      See Also:
    • DEFAULT_EXPONENTIAL_MULTIPLIER

      public static final double DEFAULT_EXPONENTIAL_MULTIPLIER
      See Also:
  • Constructor Details

    • Backoff

      public Backoff()
      Create a backoff using a default config.
    • Backoff

      public Backoff(BackoffConfig config)
      Create a backoff using the specified config.
      Parameters:
      config - the config used to compute the delay
  • Method Details

    • setConfig

      public void setConfig(BackoffConfig config)
      Set the BackoffConfig to use. This can be set at any time.
    • getCount

      public int getCount()
      Return the value of the internal count.
    • inc

      public void inc()
      Increment the internal count.
    • setCount

      public void setCount(int count)
      Set the internal count to a specific value.
    • clear

      public void clear()
      Clear the internal count.
    • getDelay

      public long getDelay()
      Return the delay for the internal count.
    • getDelay

      public long getDelay(int count)
      Return the delay for the specified count.
    • getDelay

      public static long getDelay(BackoffConfig config, int count)
      Return the delay for the specified config and count.
      Parameters:
      config - the backoff configuration
      count - the failure count
    • getConfig

      public BackoffConfig getConfig()