Class Delay


  • public abstract class Delay
    extends Object
    Base class for delays and factory class to create particular instances. Delay can be subclassed to create custom delay implementations based on attempts. Attempts start with 1.

    Delays are usually stateless instances that can be shared amongst multiple users (such as connections). Stateful Delay implementations must implement Delay.StatefulDelay to reset their internal state after the delay is not required anymore.

    Since:
    4.2
    Author:
    Mark Paluch, Jongyeol Choi
    See Also:
    Delay.StatefulDelay
    • Constructor Detail

      • Delay

        protected Delay()
        Creates a new Delay.
    • Method Detail

      • createDelay

        public abstract Duration createDelay​(long attempt)
        Calculate a specific delay based on the attempt. This method is to be implemented by the implementations and depending on the params that were set during construction time.
        Parameters:
        attempt - the attempt to calculate the delay from.
        Returns:
        the calculated delay.
      • constant

        public static Delay constant​(Duration delay)
        Creates a new ConstantDelay.
        Parameters:
        delay - the delay, must be greater or equal to 0.
        Returns:
        a created ConstantDelay.
      • constant

        @Deprecated
        public static Delay constant​(int delay,
                                     TimeUnit timeUnit)
        Deprecated.
        since 5.0, use constant(Duration)
        Creates a new ConstantDelay.
        Parameters:
        delay - the delay, must be greater or equal to 0
        timeUnit - the unit of the delay.
        Returns:
        a created ConstantDelay.
      • exponential

        public static Delay exponential()
        Creates a new ExponentialDelay with default boundaries and factor (1, 2, 4, 8, 16, 32...). The delay begins with 1 and is capped at 30 TimeUnit.SECONDS after reaching the 16th attempt.
        Returns:
        a created ExponentialDelay.
      • exponential

        public static Delay exponential​(Duration lower,
                                        Duration upper,
                                        int powersOf,
                                        TimeUnit targetTimeUnit)
        Creates a new ExponentialDelay on with custom boundaries and factor (eg. with upper 9000, lower 0, powerOf 10: 1, 10, 100, 1000, 9000, 9000, 9000, ...).
        Parameters:
        lower - the lower boundary, must be non-negative
        upper - the upper boundary, must be greater than the lower boundary
        powersOf - the base for exponential growth (eg. powers of 2, powers of 10, etc...), must be non-negative and greater than 1
        targetTimeUnit - the unit of the delay.
        Returns:
        a created ExponentialDelay.
        Since:
        5.0
      • exponential

        public static Delay exponential​(long lower,
                                        long upper,
                                        TimeUnit unit,
                                        int powersOf)
        Creates a new ExponentialDelay on with custom boundaries and factor (eg. with upper 9000, lower 0, powerOf 10: 1, 10, 100, 1000, 9000, 9000, 9000, ...).
        Parameters:
        lower - the lower boundary, must be non-negative
        upper - the upper boundary, must be greater than the lower boundary
        unit - the unit of the delay.
        powersOf - the base for exponential growth (eg. powers of 2, powers of 10, etc...), must be non-negative and greater than 1
        Returns:
        a created ExponentialDelay.
      • equalJitter

        public static Delay equalJitter()
        Creates a new EqualJitterDelay with default boundaries.
        Returns:
        a created EqualJitterDelay.
      • equalJitter

        public static Delay equalJitter​(Duration lower,
                                        Duration upper,
                                        long base,
                                        TimeUnit targetTimeUnit)
        Creates a new EqualJitterDelay.
        Parameters:
        lower - the lower boundary, must be non-negative
        upper - the upper boundary, must be greater than the lower boundary
        base - the base, must be greater or equal to 1
        targetTimeUnit - the unit of the delay.
        Returns:
        a created EqualJitterDelay.
        Since:
        5.0
      • equalJitter

        public static Delay equalJitter​(long lower,
                                        long upper,
                                        long base,
                                        TimeUnit unit)
        Creates a new EqualJitterDelay.
        Parameters:
        lower - the lower boundary, must be non-negative
        upper - the upper boundary, must be greater than the lower boundary
        base - the base, must be greater or equal to 1
        unit - the unit of the delay.
        Returns:
        a created EqualJitterDelay.
      • fullJitter

        public static Delay fullJitter()
        Creates a new FullJitterDelay with default boundaries.
        Returns:
        a created FullJitterDelay.
      • fullJitter

        public static Delay fullJitter​(Duration lower,
                                       Duration upper,
                                       long base,
                                       TimeUnit targetTimeUnit)
        Creates a new FullJitterDelay.
        Parameters:
        lower - the lower boundary, must be non-negative
        upper - the upper boundary, must be greater than the lower boundary
        base - the base, must be greater or equal to 1
        targetTimeUnit - the unit of the delay.
        Returns:
        a created FullJitterDelay.
        Since:
        5.0
      • fullJitter

        public static Delay fullJitter​(long lower,
                                       long upper,
                                       long base,
                                       TimeUnit unit)
        Creates a new FullJitterDelay.
        Parameters:
        lower - the lower boundary, must be non-negative
        upper - the upper boundary, must be greater than the lower boundary
        base - the base, must be greater or equal to 1
        unit - the unit of the delay.
        Returns:
        a created FullJitterDelay.
      • decorrelatedJitter

        public static Supplier<Delay> decorrelatedJitter()
        Creates a Supplier that constructs new DecorrelatedJitterDelay instances with default boundaries.
        Returns:
        a Supplier of DecorrelatedJitterDelay.
      • decorrelatedJitter

        public static Supplier<Delay> decorrelatedJitter​(Duration lower,
                                                         Duration upper,
                                                         long base,
                                                         TimeUnit targetTimeUnit)
        Creates a Supplier that constructs new DecorrelatedJitterDelay instances.
        Parameters:
        lower - the lower boundary, must be non-negative
        upper - the upper boundary, must be greater than the lower boundary
        base - the base, must be greater or equal to 0
        targetTimeUnit - the unit of the delay.
        Returns:
        a new Supplier of DecorrelatedJitterDelay.
        Since:
        5.0
      • decorrelatedJitter

        public static Supplier<Delay> decorrelatedJitter​(long lower,
                                                         long upper,
                                                         long base,
                                                         TimeUnit unit)
        Creates a Supplier that constructs new DecorrelatedJitterDelay instances.
        Parameters:
        lower - the lower boundary, must be non-negative
        upper - the upper boundary, must be greater than the lower boundary
        base - the base, must be greater or equal to 0
        unit - the unit of the delay.
        Returns:
        a new Supplier of DecorrelatedJitterDelay.
      • randomBetween

        protected static long randomBetween​(long min,
                                            long max)
        Generates a random long value within min and max boundaries.
        Parameters:
        min -
        max -
        Returns:
        a random value
        See Also:
        ThreadLocalRandom.nextLong(long, long)