Interface BackoffStrategy

Functional Interface:
This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.

@FunctionalInterface public interface BackoffStrategy
Strategy for computing the delay between retry attempts.

Strategies are composable via decorator methods:


 BackoffStrategy strategy = BackoffStrategy.exponential(Duration.ofMillis(100))
     .withMax(Duration.ofSeconds(30))
     .withJitter(0.5);
 
  • Method Summary

    Modifier and Type
    Method
    Description
    delay(int attempt)
    Computes the delay before the given attempt.
    exponential(Duration baseDelay)
    Returns a strategy that doubles the base delay on each attempt: baseDelay * 2^(attempt - 1).
    fixed(Duration delay)
    Returns a strategy that always returns the same delay.
    withJitter(double factor)
    Wraps this strategy, adding random jitter up to factor * delay.
    Wraps this strategy, capping the delay at the given maximum.
  • Method Details

    • delay

      Duration delay(int attempt)
      Computes the delay before the given attempt.
      Parameters:
      attempt - the 1-based attempt number
      Returns:
      the delay (must not be negative)
    • fixed

      static BackoffStrategy fixed(Duration delay)
      Returns a strategy that always returns the same delay.
      Parameters:
      delay - the fixed delay
      Returns:
      a fixed-delay backoff strategy
    • exponential

      static BackoffStrategy exponential(Duration baseDelay)
      Returns a strategy that doubles the base delay on each attempt: baseDelay * 2^(attempt - 1).
      Parameters:
      baseDelay - the delay for the first attempt
      Returns:
      an exponential backoff strategy
    • withMax

      default BackoffStrategy withMax(Duration max)
      Wraps this strategy, capping the delay at the given maximum.
      Parameters:
      max - the maximum delay
      Returns:
      a capped backoff strategy
    • withJitter

      default BackoffStrategy withJitter(double factor)
      Wraps this strategy, adding random jitter up to factor * delay.
      Parameters:
      factor - the jitter factor (0.0 = no jitter, 1.0 = up to 100% of the delay)
      Returns:
      a jittered backoff strategy