Class RetryUtils.RetryPolicy

java.lang.Object
dev.langchain4j.internal.RetryUtils.RetryPolicy
Enclosing class:
RetryUtils

public static final class RetryUtils.RetryPolicy extends Object
This class encapsulates a retry policy.
  • Constructor Details

    • RetryPolicy

      public RetryPolicy(int maxRetries, int delayMillis, double jitterScale, double backoffExp)
      Construct a RetryPolicy.
      Parameters:
      maxRetries - The maximum number of retries. The action can be executed up to maxRetries + 1 times.
      delayMillis - The delay in milliseconds.
      jitterScale - The jitter scale.
      backoffExp - The backoff exponent.
  • Method Details

    • rawDelayMs

      public double rawDelayMs(int retry)
      This method returns the raw delay in milliseconds after a given retry.
      Parameters:
      retry - The retry number.
      Returns:
      The raw delay in milliseconds.
    • jitterDelayMillis

      public int jitterDelayMillis(int retry)
      This method returns the jitter delay in milliseconds after a given retry.
      Parameters:
      retry - The retry number.
      Returns:
      The jitter delay in milliseconds.
    • sleep

      public void sleep(int retry)
      This method sleeps after a given retry.
      Parameters:
      retry - The retry number.
    • withRetry

      public <T> T withRetry(Callable<T> action)
      This method attempts to execute a given action up to 3 times with an exponential backoff. If the action fails on all attempts, it throws a RuntimeException.
      Type Parameters:
      T - The type of the result of the action.
      Parameters:
      action - The action to be executed.
      Returns:
      The result of the action if it is successful.
      Throws:
      RuntimeException - if the action fails on all attempts.
    • withRetry

      public <T> T withRetry(Callable<T> action, int maxRetries)
      This method attempts to execute a given action up to a specified number of times with an exponential backoff. If the action fails on all attempts, it throws a RuntimeException.
      Type Parameters:
      T - The type of the result of the action.
      Parameters:
      action - The action to be executed.
      maxRetries - The maximum number of retries. The action can be executed up to maxRetries + 1 times.
      Returns:
      The result of the action if it is successful.
      Throws:
      RuntimeException - if the action fails on all attempts.
    • withRetryAsync

      public <T> CompletableFuture<T> withRetryAsync(Supplier<CompletableFuture<T>> action, int maxRetries, ExceptionMapper exceptionMapper)
      Non-blocking counterpart of withRetry(Callable, int) for actions that return a CompletableFuture. On a failed attempt, the failure is mapped with exceptionMapper and, if the mapped exception is not a NonRetriableException and retries remain, the action is re-invoked after the same exponential backoff + jitter delay as the synchronous path — but the wait is scheduled (via CompletableFuture.delayedExecutor(long, TimeUnit, Executor)), so no thread is blocked during the backoff. A CancellationException is never retried, and cancelling the returned future cancels the in-flight attempt and stops any further retries.
      Type Parameters:
      T - the result type.
      Parameters:
      action - supplies a fresh CompletableFuture for each attempt.
      maxRetries - the maximum number of retries (the action can run up to maxRetries + 1 times).
      exceptionMapper - maps an attempt's failure; the mapped exception drives the retriable decision and is what the returned future fails with.
      Returns:
      a future completing with the first successful attempt, or failing with the mapped exception of the last attempt.
      Since:
      1.20.0