Class RetryPolicy

java.lang.Object
com.loomcache.client.retry.RetryPolicy

public class RetryPolicy extends Object
Configurable retry policy with exponential backoff, jitter, and statistics tracking.

Supports configurable max retries, exponential backoff with base delay, maximum delay cap, and random jitter to prevent thundering herd.

Usage:

  var policy = RetryPolicy.builder()
      .maxRetries(3)
      .baseDelayMs(100)
      .maxDelayMs(5000)
      .jitterFactor(0.25)
      .build();
  String result = policy.execute(() -> client.get("map", "key"));
  • Method Details

    • execute

      public <T> @Nullable T execute(RetryPolicy.RetryableOperation<T> operation) throws Exception
      Executes the given operation with retry logic.
      Type Parameters:
      T - the return type of the operation
      Parameters:
      operation - the operation to execute (must not be null)
      Returns:
      the result of the operation
      Throws:
      IllegalStateException - if the circuit breaker is open
      Exception - if the operation fails after all retries
      NullPointerException - if operation is null
    • calculateDelay

      public long calculateDelay(int attempt)
      Calculate the delay for the given attempt using exponential backoff with jitter.
      Parameters:
      attempt - the attempt number (0-indexed)
      Returns:
      the calculated delay in milliseconds
    • getRetryStats

      public RetryPolicy.RetryStats getRetryStats()
      Get a snapshot of retry statistics.
      Returns:
      a snapshot of current retry statistics (never null)
    • isCircuitOpen

      public boolean isCircuitOpen()
      Check if circuit breaker is currently open.
      Returns:
      true if the circuit breaker is open, false otherwise
    • resetCircuit

      public void resetCircuit()
      Manually reset circuit breaker.
    • withCircuitBreaker

      public RetryPolicy withCircuitBreaker(int failureThreshold, long resetTimeoutMs)
      Create a new RetryPolicy with circuit breaker configuration.
      Parameters:
      failureThreshold - the failure threshold for circuit breaker
      resetTimeoutMs - the timeout for circuit breaker reset in milliseconds
      Returns:
      a new RetryPolicy with circuit breaker configuration
    • withJitter

      public RetryPolicy withJitter(double jitterFactor)
      Create a new RetryPolicy with updated jitter factor.
      Parameters:
      jitterFactor - the new jitter factor (0-1)
      Returns:
      a new RetryPolicy with updated jitter factor
    • withRetryPredicate

      public RetryPolicy withRetryPredicate(Predicate<Exception> retryPredicate)
      Create a new RetryPolicy with custom retry predicate.
      Parameters:
      retryPredicate - the custom retry predicate (must not be null)
      Returns:
      a new RetryPolicy with custom retry predicate
    • noRetry

      public static RetryPolicy noRetry()
    • defaultPolicy

      public static RetryPolicy defaultPolicy()
    • builder

      public static RetryPolicy.Builder builder()