Class Retryer.Delay<E>

java.lang.Object
com.google.mu.util.concurrent.Retryer.Delay<E>
All Implemented Interfaces:
Comparable<Retryer.Delay<E>>
Enclosing class:
Retryer

public abstract static class Retryer.Delay<E> extends Object implements Comparable<Retryer.Delay<E>>
Represents a delay upon an event of type E prior to the retry attempt.
  • Constructor Details

    • Delay

      public Delay()
  • Method Details

    • duration

      public abstract Duration duration()
      Returns the delay interval.
    • ofMillis

      public static <E> Retryer.Delay<E> ofMillis(long millis)
      Shorthand for of(Duration.ofMillis(millis)).
      Parameters:
      millis - must not be negative
    • of

      public static <E> Retryer.Delay<E> of(Duration duration)
      Returns a Delay of duration.
      Parameters:
      duration - must not be negative
    • timed

      public final <T extends Retryer.Delay<?>> List<T> timed(List<T> list, Clock clock)
      Returns a view of list that while not modifiable, will become empty when duration() has elapsed since the time the view was created as if another thread had just concurrently removed all elements from it.

      Useful for setting a retry deadline to avoid long response time. For example:

      
         Delay<?> deadline = Delay.ofMillis(500);
         new Retryer()
             .upon(RpcException.class,
                   deadline.timed(Delay.ofMillis(30).exponentialBackoff(2, 5), clock))
             .retry(this::getAccount, executor);
       

      The returned List view's state is dependent on the current time. Beware of copying the list, because when you do, time is frozen as far as the copy is concerned. Passing the copy to upon() no longer respects "timed" semantics.

      Note that if the timed deadline would have been exceeded after the current delay, that delay will be considered "removed" and hence cause the retry to stop.

      clock is used to measure time.

    • timed

      public final <T extends Retryer.Delay<?>> List<T> timed(List<T> list)
      Returns a view of list that while not modifiable, will become empty when duration() has elapsed since the time the view was created as if another thread had just concurrently removed all elements from it.

      Useful for setting a retry deadline to avoid long response time. For example:

      
         Delay<?> deadline = Delay.ofMillis(500);
         new Retryer()
             .upon(RpcException.class, deadline.timed(Delay.ofMillis(30).exponentialBackoff(2, 5)))
             .retry(this::getAccount, executor);
       

      The returned List view's state is dependent on the current time. Beware of copying the list, because when you do, time is frozen as far as the copy is concerned. Passing the copy to upon() no longer respects "timed" semantics.

      Note that if the timed deadline would have been exceeded after the current delay, that delay will be considered "removed" and hence cause the retry to stop.

    • exponentialBackoff

      public final List<Retryer.Delay<E>> exponentialBackoff(double multiplier, int size)
      Returns an immutable List of delays with size. The first delay (if size > 0) is this and the following delays are exponentially multiplied using multiplier.
      Parameters:
      multiplier - must be positive
      size - must not be negative
    • multipliedBy

      public final Retryer.Delay<E> multipliedBy(double multiplier)
      Returns a new Delay with duration multiplied by multiplier.
      Parameters:
      multiplier - must not be negative
    • randomized

      public final Retryer.Delay<E> randomized(Random random, double randomness)
      Returns a new Delay with some extra randomness. To randomize a list of Delays, for example:
      
         Random random = new Random();
         List<Delay> randomized = Delay.ofMillis(100).exponentialBackoff(2, 5).stream()
             .map(d -> d.randomized(random, 0.5))
             .collect(toList());
       
      Parameters:
      random - random generator
      randomness - Must be in the range of [0, 1]. 0 means no randomness; and 1 means the delay randomly ranges from 0x to 2x.
    • fibonacci

      public final List<Retryer.Delay<E>> fibonacci(int size)
      Returns a fibonacci list of delays of size, as in 1, 1, 2, 3, 5, 8, ... with this delay being the multiplier.
    • beforeDelay

      public void beforeDelay(E event)
      Called if event will be retried after the delay. Logs the event by default.
    • afterDelay

      public void afterDelay(E event)
      Called after the delay, immediately before the retry. Logs the event by default.
    • compareTo

      public int compareTo(Retryer.Delay<E> that)
      Specified by:
      compareTo in interface Comparable<E>
    • equals

      public boolean equals(Object obj)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object