Record Class Retry.Policy

java.lang.Object
java.lang.Record
dev.fforj.Retry.Policy
Record Components:
maxAttempts - total attempts including the first call. 1 = no retry.
initialDelay - delay before the first retry.
backoffFactor - delay multiplier between attempts. 1.0 = fixed delay.
maxDelay - upper bound on any single delay after backoff; uncapped exponential backoff gets silly fast. The three-argument constructor defaults it to effectively unbounded.
jitter - fraction in [0, 1): each sleep is scaled by a uniform random factor in [1 - jitter, 1 + jitter] to spread synchronized clients ("thundering herds"). 0 (the default) means fully deterministic delays. Keep it 0 in tests.
Enclosing class:
Retry

public static record Retry.Policy(int maxAttempts, Duration initialDelay, double backoffFactor, Duration maxDelay, double jitter) extends Record
Policy parameters.
  • Constructor Details

    • Policy

      public Policy(int maxAttempts, Duration initialDelay, double backoffFactor, Duration maxDelay, double jitter)
      Creates an instance of a Policy record class.
      Parameters:
      maxAttempts - the value for the maxAttempts record component
      initialDelay - the value for the initialDelay record component
      backoffFactor - the value for the backoffFactor record component
      maxDelay - the value for the maxDelay record component
      jitter - the value for the jitter record component
    • Policy

      public Policy(int maxAttempts, Duration initialDelay, double backoffFactor)
      Uncapped, jitter-free policy; the common starting point.
  • Method Details

    • fixed

      public static Retry.Policy fixed(int maxAttempts, Duration delay)
      Constant delay between retries.
    • exponential

      public static Retry.Policy exponential(int maxAttempts, Duration initialDelay)
      Exponential backoff (doubling) between retries.
    • withMaxDelay

      public Retry.Policy withMaxDelay(Duration maxDelay)
      Same policy with every delay capped at maxDelay.
    • withJitter

      public Retry.Policy withJitter(double jitter)
      Same policy with the given jitter fraction (see jitter).
    • delayBefore

      public Duration delayBefore(int attempt)
      The planned delay before the given attempt (the first retry is attempt 2): initialDelay scaled by backoffFactor once per prior retry, capped at maxDelay. Pure and deterministic (jitter is applied at sleep time by Retry.run(Retry.Policy, Predicate, IntFunction), never here), so retry timing can be asserted in tests without sleeping through it.
    • 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 the compare method from their corresponding wrapper classes.
      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
    • initialDelay

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

      public double backoffFactor()
      Returns the value of the backoffFactor record component.
      Returns:
      the value of the backoffFactor record component
    • maxDelay

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

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