public abstract class Delay extends Object
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.
Delay.StatefulDelay
Modifier and Type | Class and Description |
---|---|
static interface |
Delay.StatefulDelay
Interface to be implemented by stateful
Delay s. |
Modifier | Constructor and Description |
---|---|
protected |
Delay()
Creates a new
Delay . |
Modifier and Type | Method and Description |
---|---|
protected static Duration |
applyBounds(Duration calculatedValue,
Duration lower,
Duration upper) |
static Delay |
constant(Duration delay)
Creates a new
ConstantDelay . |
static Delay |
constant(int delay,
TimeUnit timeUnit)
Deprecated.
since 5.0, use
constant(Duration) |
abstract Duration |
createDelay(long attempt)
Calculate a specific delay based on the attempt.
|
static Supplier<Delay> |
decorrelatedJitter()
Creates a
Supplier that constructs new DecorrelatedJitterDelay instances with default boundaries. |
static Supplier<Delay> |
decorrelatedJitter(Duration lower,
Duration upper,
long base,
TimeUnit targetTimeUnit)
Creates a
Supplier that constructs new DecorrelatedJitterDelay instances. |
static Supplier<Delay> |
decorrelatedJitter(long lower,
long upper,
long base,
TimeUnit unit)
Creates a
Supplier that constructs new DecorrelatedJitterDelay instances. |
static Delay |
equalJitter()
Creates a new
EqualJitterDelay with default boundaries. |
static Delay |
equalJitter(Duration lower,
Duration upper,
long base,
TimeUnit targetTimeUnit)
Creates a new
EqualJitterDelay . |
static Delay |
equalJitter(long lower,
long upper,
long base,
TimeUnit unit)
Creates a new
EqualJitterDelay . |
static Delay |
exponential()
Creates a new
ExponentialDelay with default boundaries and factor (1, 2, 4, 8, 16, 32...). |
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, ...). |
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, ...). |
static Delay |
fullJitter()
Creates a new
FullJitterDelay with default boundaries. |
static Delay |
fullJitter(Duration lower,
Duration upper,
long base,
TimeUnit targetTimeUnit)
Creates a new
FullJitterDelay . |
static Delay |
fullJitter(long lower,
long upper,
long base,
TimeUnit unit)
Creates a new
FullJitterDelay . |
protected static long |
randomBetween(long min,
long max)
Generates a random long value within
min and max boundaries. |
protected Delay()
Delay
.public abstract Duration createDelay(long attempt)
attempt
- the attempt to calculate the delay from.public static Delay constant(Duration delay)
ConstantDelay
.delay
- the delay, must be greater or equal to 0.ConstantDelay
.@Deprecated public static Delay constant(int delay, TimeUnit timeUnit)
constant(Duration)
ConstantDelay
.delay
- the delay, must be greater or equal to 0timeUnit
- the unit of the delay.ConstantDelay
.public static Delay exponential()
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.ExponentialDelay
.public static Delay exponential(Duration lower, Duration upper, int powersOf, TimeUnit targetTimeUnit)
ExponentialDelay
on with custom boundaries and factor (eg. with upper 9000, lower 0, powerOf 10: 1,
10, 100, 1000, 9000, 9000, 9000, ...).lower
- the lower boundary, must be non-negativeupper
- the upper boundary, must be greater than the lower boundarypowersOf
- the base for exponential growth (eg. powers of 2, powers of 10, etc...), must be non-negative and greater
than 1targetTimeUnit
- the unit of the delay.ExponentialDelay
.public static Delay exponential(long lower, long upper, TimeUnit unit, int powersOf)
ExponentialDelay
on with custom boundaries and factor (eg. with upper 9000, lower 0, powerOf 10: 1,
10, 100, 1000, 9000, 9000, 9000, ...).lower
- the lower boundary, must be non-negativeupper
- the upper boundary, must be greater than the lower boundaryunit
- 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 1ExponentialDelay
.public static Delay equalJitter()
EqualJitterDelay
with default boundaries.EqualJitterDelay
.public static Delay equalJitter(Duration lower, Duration upper, long base, TimeUnit targetTimeUnit)
EqualJitterDelay
.lower
- the lower boundary, must be non-negativeupper
- the upper boundary, must be greater than the lower boundarybase
- the base, must be greater or equal to 1targetTimeUnit
- the unit of the delay.EqualJitterDelay
.public static Delay equalJitter(long lower, long upper, long base, TimeUnit unit)
EqualJitterDelay
.lower
- the lower boundary, must be non-negativeupper
- the upper boundary, must be greater than the lower boundarybase
- the base, must be greater or equal to 1unit
- the unit of the delay.EqualJitterDelay
.public static Delay fullJitter()
FullJitterDelay
with default boundaries.FullJitterDelay
.public static Delay fullJitter(Duration lower, Duration upper, long base, TimeUnit targetTimeUnit)
FullJitterDelay
.lower
- the lower boundary, must be non-negativeupper
- the upper boundary, must be greater than the lower boundarybase
- the base, must be greater or equal to 1targetTimeUnit
- the unit of the delay.FullJitterDelay
.public static Delay fullJitter(long lower, long upper, long base, TimeUnit unit)
FullJitterDelay
.lower
- the lower boundary, must be non-negativeupper
- the upper boundary, must be greater than the lower boundarybase
- the base, must be greater or equal to 1unit
- the unit of the delay.FullJitterDelay
.public static Supplier<Delay> decorrelatedJitter()
Supplier
that constructs new DecorrelatedJitterDelay
instances with default boundaries.Supplier
of DecorrelatedJitterDelay
.public static Supplier<Delay> decorrelatedJitter(Duration lower, Duration upper, long base, TimeUnit targetTimeUnit)
Supplier
that constructs new DecorrelatedJitterDelay
instances.lower
- the lower boundary, must be non-negativeupper
- the upper boundary, must be greater than the lower boundarybase
- the base, must be greater or equal to 0targetTimeUnit
- the unit of the delay.Supplier
of DecorrelatedJitterDelay
.public static Supplier<Delay> decorrelatedJitter(long lower, long upper, long base, TimeUnit unit)
Supplier
that constructs new DecorrelatedJitterDelay
instances.lower
- the lower boundary, must be non-negativeupper
- the upper boundary, must be greater than the lower boundarybase
- the base, must be greater or equal to 0unit
- the unit of the delay.Supplier
of DecorrelatedJitterDelay
.protected static long randomBetween(long min, long max)
min
and max
boundaries.min
- max
- ThreadLocalRandom.nextLong(long, long)
Copyright © 2025 lettuce.io. All rights reserved.