Class ExponentialBackoffStrategy

java.lang.Object
com.github.twitch4j.common.util.ExponentialBackoffStrategy
All Implemented Interfaces:
IBackoffStrategy

public final class ExponentialBackoffStrategy extends Object implements IBackoffStrategy
Thread-safe, configurable helper for applying the exponential backoff algorithm with optional jitter and/or truncation.
  • Method Details

    • get

      public long get()
      Description copied from interface: IBackoffStrategy
      Increments the failure count and computes the appropriate exponential backoff.
      Specified by:
      get in interface IBackoffStrategy
      Returns:
      the amount of milliseconds to delay before retrying.
    • reset

      public void reset()
      Description copied from interface: IBackoffStrategy
      Resets the failure count for exponential backoff calculations.
      Specified by:
      reset in interface IBackoffStrategy
    • setFailures

      public void setFailures(int failures)
    • getFailures

      public int getFailures()
      Description copied from interface: IBackoffStrategy
      Returns the current failure count
      Specified by:
      getFailures in interface IBackoffStrategy
      Returns:
      failure count
    • copy

      Returns:
      a new ExponentialBackoffStrategy instance with the same configuration settings (and no failures).
    • builder

    • toBuilder

    • getMaximumBackoff

      public long getMaximumBackoff()
      The maximum backoff value (on average), in milliseconds. If set to a negative value, the algorithm will not be of the truncated variety.
    • getMultiplier

      public double getMultiplier()
      The multiplier on back-offs that is in the base of the exponent.

      The default is 2, which results in doubling of average delays with additional failures. This generally should be set to a value greater than 1 so that delays tend to increase with more failures.

    • isImmediateFirst

      public boolean isImmediateFirst()
      Whether the first attempt after a failure should take place without delay.

      To avoid a "stampeding herd" of reconnecting clients, ExponentialBackoffStrategy.ExponentialBackoffStrategyBuilder.jitter(boolean) can be enabled and ExponentialBackoffStrategy.ExponentialBackoffStrategyBuilder.initialJitterRange(long) can optionally be configured.

    • isJitter

      public boolean isJitter()
      Whether (pseudo-)randomness should be applied when computing the exponential backoff.

      Highly useful for avoiding the thundering herd problem.

    • getInitialJitterRange

      public long getInitialJitterRange()
      The range of initial jitter amounts (in milliseconds) for when both isImmediateFirst() and isJitter() are true.
    • getBaseMillis

      public long getBaseMillis()
      The milliseconds value for the first non-zero backoff. When isJitter() is true, this becomes an average targeted value rather than a strictly enforced constant.
    • getMaxRetries

      public int getMaxRetries()
      The maximum number of retries that should be allowed.

      A negative value corresponds to no limit. A zero value corresponds to no retries allowed. A positive value enforces a specific maximum.

    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object