Record Class RetryPolicy

java.lang.Object
java.lang.Record
xyz.tcheeric.jev.RetryPolicy

public record RetryPolicy(int maxAttempts, Duration initialBackoff, double multiplier, Duration maxBackoff, double jitterFactor) extends Record
How hard, and how politely, to retry.

Only rate limiting (429), evaluator overload (529) and a failed connection are retried. A 401 or a 422 is a statement about the request itself, and repeating it merely spends the caller's rate limit to be told the same thing again.

backoffFor(int, double) is a pure function of the attempt number and a random fraction supplied by the caller. Keeping the randomness out of the policy is what lets a unit test assert the schedule without a clock and without a sleep.

  • Constructor Summary

    Constructors
    Constructor
    Description
    RetryPolicy(int maxAttempts, Duration initialBackoff, double multiplier, Duration maxBackoff, double jitterFactor)
    Creates an instance of a RetryPolicy record class.
  • Method Summary

    Modifier and Type
    Method
    Description
    backoffFor(int attempt, double randomFraction)
    The wait before the attempt numbered attempt (1 means the wait after the first failure).
     
    final boolean
    Indicates whether some other object is "equal to" this one.
    final int
    Returns a hash code value for this object.
    Returns the value of the initialBackoff record component.
    double
    Returns the value of the jitterFactor record component.
    int
    Returns the value of the maxAttempts record component.
    Returns the value of the maxBackoff record component.
    double
    Returns the value of the multiplier record component.
    nextWait(int attempt, double randomFraction, Optional<Duration> serverAsked)
    The wait before the next attempt when the evaluator may have said how long to wait, or empty when the caller should stop retrying.
    static boolean
    retryable(int status)
    Whether a status is worth repeating the request for.
    final String
    Returns a string representation of this record class.

    Methods inherited from class java.lang.Object

    getClass, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • RetryPolicy

      public RetryPolicy(int maxAttempts, Duration initialBackoff, double multiplier, Duration maxBackoff, double jitterFactor)
      Creates an instance of a RetryPolicy record class.
      Parameters:
      maxAttempts - the value for the maxAttempts record component
      initialBackoff - the value for the initialBackoff record component
      multiplier - the value for the multiplier record component
      maxBackoff - the value for the maxBackoff record component
      jitterFactor - the value for the jitterFactor record component
  • Method Details

    • defaults

      public static RetryPolicy defaults()
    • retryable

      public static boolean retryable(int status)
      Whether a status is worth repeating the request for. Deliberately narrow: an unknown 5xx is not retried, because a client that retries everything turns an evaluator's bad day into an outage.
    • backoffFor

      public Duration backoffFor(int attempt, double randomFraction)
      The wait before the attempt numbered attempt (1 means the wait after the first failure). randomFraction is in 0..1 and comes from the caller's random source.
    • nextWait

      public Optional<Duration> nextWait(int attempt, double randomFraction, Optional<Duration> serverAsked)
      The wait before the next attempt when the evaluator may have said how long to wait, or empty when the caller should stop retrying.

      A wait the server asked for replaces the computed backoff, as the official SDKs do: the server knows when its rate window reopens and the client can only guess. It is used without jitter, since the server has already chosen the moment. A requested wait longer than maxBackoff() ends the retries rather than being cut short, because retrying before the server said to only earns another 429, and the caller learns the requested wait from JevApiException.requestedWait() instead.

    • 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.
    • maxAttempts

      public int maxAttempts()
      Returns the value of the maxAttempts record component.
      Returns:
      the value of the maxAttempts record component
    • initialBackoff

      public Duration initialBackoff()
      Returns the value of the initialBackoff record component.
      Returns:
      the value of the initialBackoff record component
    • multiplier

      public double multiplier()
      Returns the value of the multiplier record component.
      Returns:
      the value of the multiplier record component
    • maxBackoff

      public Duration maxBackoff()
      Returns the value of the maxBackoff record component.
      Returns:
      the value of the maxBackoff record component
    • jitterFactor

      public double jitterFactor()
      Returns the value of the jitterFactor record component.
      Returns:
      the value of the jitterFactor record component