Class RetryPolicy

java.lang.Object
express.mvp.myra.transport.error.RetryPolicy

public final class RetryPolicy extends Object
Defines retry behavior for failed operations.

This class encapsulates retry policy decisions including whether to retry, delay calculation, and maximum attempt limits. It supports exponential backoff with jitter for distributed systems.

Built-in Policies

Usage Example

RetryPolicy policy = RetryPolicy.exponentialBackoffWithJitter(
    5,                      // max 5 attempts
    Duration.ofMillis(100), // start with 100ms
    Duration.ofSeconds(30), // cap at 30s
    0.2                     // 20% jitter
);

RetryContext context = new RetryContext("connect", policy.getMaxAttempts());

while (true) {
    try {
        context.startAttempt();
        connect();
        break;
    } catch (Exception e) {
        context.recordFailure(e);
        if (policy.shouldRetry(context)) {
            long delay = policy.calculateDelay(context);
            Thread.sleep(delay);
            context.recordDelay(delay);
        } else {
            throw new ConnectionException("Failed after retries", e);
        }
    }
}
See Also:
  • Method Details

    • getMaxAttempts

      public int getMaxAttempts()
      Returns the maximum number of attempts.
      Returns:
      max attempts
    • shouldRetry

      public boolean shouldRetry(RetryContext context)
      Determines if a retry should be attempted.
      Parameters:
      context - the retry context
      Returns:
      true if retry should be attempted
    • calculateDelay

      public long calculateDelay(RetryContext context)
      Calculates the delay before the next retry.
      Parameters:
      context - the retry context
      Returns:
      delay in milliseconds
    • noRetry

      public static RetryPolicy noRetry()
      Returns a policy that never retries.
      Returns:
      no-retry policy
    • immediate

      public static RetryPolicy immediate(int maxAttempts)
      Returns a policy that retries immediately without delay.
      Parameters:
      maxAttempts - maximum attempts
      Returns:
      immediate retry policy
    • fixedDelay

      public static RetryPolicy fixedDelay(int maxAttempts, Duration delay)
      Returns a policy with fixed delay between retries.
      Parameters:
      maxAttempts - maximum attempts
      delay - delay between retries
      Returns:
      fixed delay policy
    • exponentialBackoff

      public static RetryPolicy exponentialBackoff(int maxAttempts, Duration initialDelay, Duration maxDelay)
      Returns a policy with exponential backoff.
      Parameters:
      maxAttempts - maximum attempts
      initialDelay - initial delay
      maxDelay - maximum delay cap
      Returns:
      exponential backoff policy
    • exponentialBackoffWithJitter

      public static RetryPolicy exponentialBackoffWithJitter(int maxAttempts, Duration initialDelay, Duration maxDelay, double jitterFactor)
      Returns a policy with exponential backoff and jitter.

      Jitter helps prevent thundering herd problems when many clients retry simultaneously.

      Parameters:
      maxAttempts - maximum attempts
      initialDelay - initial delay
      maxDelay - maximum delay cap
      jitterFactor - jitter factor (0.0-1.0, e.g., 0.2 for ±20%)
      Returns:
      exponential backoff with jitter policy
    • builder

      public static RetryPolicy.Builder builder()
      Returns a builder for custom policy configuration.
      Returns:
      new builder