Class RetryPolicy

java.lang.Object
com.shaft.api.RetryPolicy

public final class RetryPolicy extends Object
Opt-in retry policy for RequestBuilder.perform().

Retries are limited to idempotent HTTP methods by default. Use allowNonIdempotentRequests() only when retrying POST or PATCH is safe for the target API.

  • Method Details

    • transientFailures

      public static RetryPolicy transientFailures()
      Retries transient network failures and common transient HTTP statuses: 408, 429, 500, 502, 503, and 504.
      Returns:
      a retry policy with safe transient defaults
    • statusCodes

      public static RetryPolicy statusCodes(int... statusCodes)
      Retries the supplied HTTP status codes.
      Parameters:
      statusCodes - HTTP statuses to retry
      Returns:
      a retry policy for the supplied statuses
      Throws:
      IllegalArgumentException - if no status codes are supplied or a code is outside 100-599
    • maxAttempts

      public RetryPolicy maxAttempts(int maxAttempts)
      Sets the total number of attempts, including the first request.
      Parameters:
      maxAttempts - total attempts; must be greater than zero
      Returns:
      this retry policy
    • allowNonIdempotentRequests

      public RetryPolicy allowNonIdempotentRequests()
      Allows retries for POST and PATCH requests.
      Returns:
      this retry policy
    • fixedBackoff

      public RetryPolicy fixedBackoff(Duration delay)
      Uses the same delay before each retry.
      Parameters:
      delay - delay before the next attempt
      Returns:
      this retry policy
    • exponentialBackoff

      public RetryPolicy exponentialBackoff(Duration initialDelay, Duration maxDelay)
      Doubles the delay after each failed attempt until maxDelay.
      Parameters:
      initialDelay - delay before the first retry
      maxDelay - upper delay bound
      Returns:
      this retry policy
    • jitteredBackoff

      public RetryPolicy jitteredBackoff(Duration initialDelay, Duration maxDelay)
      Uses exponential backoff with a random delay between zero and the current exponential delay.
      Parameters:
      initialDelay - maximum delay before the first retry
      maxDelay - upper delay bound
      Returns:
      this retry policy