Record Class RetryOptions

java.lang.Object
java.lang.Record
co.cryptoservice.sdk.RetryOptions
Record Components:
maxRetries - the retries after the first attempt (0 or more)
initialDelay - the cap on the first retry's wait (positive)
maxDelay - the cap on any wait, and the longest Retry-After honoured (no shorter than initialDelay)

public record RetryOptions(int maxRetries, Duration initialDelay, Duration maxDelay) extends Record
Automatic retries of transient failures; off by default (maxRetries = 0).

With retries on, the client sends a call again after HTTP 429, 503 or 504, or after a lost connection or an attempt timeout, up to maxRetries() more times. Routes that create or delete server state (key generation, /v2/keys/delete, /v2/kms/create-key, /v2/opaque/register/*) are retried only after 429 or 503, when the server certainly did not act; never after a 504 or a lost connection.

The wait before retry n (from 1) is full jitter: a random point in [0, min(maxDelay, initialDelay * 2^(n-1))). A Retry-After header (seconds or an HTTP date) replaces it; when it asks for longer than maxDelay() the client stops and raises that answer rather than retry early.


 CryptoServiceClient client = CryptoServiceClient.builder()
     .baseUrl("http://127.0.0.1:3101")
     .apiKey(System.getenv("CRYPTO_SERVICE_API_KEY"))
     .retry(RetryOptions.of(3))
     .build();
 
  • Field Details

    • DEFAULT_INITIAL_DELAY

      public static final Duration DEFAULT_INITIAL_DELAY
      The default initial delay, 200 ms.
    • DEFAULT_MAX_DELAY

      public static final Duration DEFAULT_MAX_DELAY
      The default maximum delay, 2 s.
    • NONE

      public static final RetryOptions NONE
      No retries: every call is sent once.
  • Constructor Details

    • RetryOptions

      public RetryOptions(int maxRetries, Duration initialDelay, Duration maxDelay)
      Checks the values.
      Throws:
      IllegalArgumentException - when maxRetries is negative, a delay is missing or not positive, or maxDelay is shorter than initialDelay
  • Method Details

    • of

      public static RetryOptions of(int maxRetries)
      maxRetries retries with the default delays (200 ms initial, 2 s maximum).
      Parameters:
      maxRetries - the retries after the first attempt
      Returns:
      the options
    • toString

      public final String toString()
      Returns a string representation of this record class. The representation contains the name of the class, followed by the name and value of each of the record components.
      Specified by:
      toString in class Record
      Returns:
      a string representation of this object
    • hashCode

      public final int hashCode()
      Returns a hash code value for this object. The value is derived from the hash code of each of the record components.
      Specified by:
      hashCode in class Record
      Returns:
      a hash code value for this object
    • equals

      public final boolean equals(Object o)
      Indicates whether some other object is "equal to" this one. The objects are equal if the other object is of the same class and if all the record components are equal. Reference components are compared with Objects::equals(Object,Object); primitive components are compared with '=='.
      Specified by:
      equals in class Record
      Parameters:
      o - the object with which to compare
      Returns:
      true if this object is the same as the o argument; false otherwise.
    • maxRetries

      public int maxRetries()
      Returns the value of the maxRetries record component.
      Returns:
      the value of the maxRetries record component
    • initialDelay

      public Duration initialDelay()
      Returns the value of the initialDelay record component.
      Returns:
      the value of the initialDelay record component
    • maxDelay

      public Duration maxDelay()
      Returns the value of the maxDelay record component.
      Returns:
      the value of the maxDelay record component