Package io.lettuce.core.resource
Class Delay
- java.lang.Object
-
- io.lettuce.core.resource.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 with1
.Delays are usually stateless instances that can be shared amongst multiple users (such as connections). Stateful
Delay
implementations must implementDelay.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
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interface
Delay.StatefulDelay
Interface to be implemented by statefulDelay
s.
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Deprecated Methods Modifier and Type Method Description protected static Duration
applyBounds(Duration calculatedValue, Duration lower, Duration upper)
static Delay
constant(int delay, TimeUnit timeUnit)
Deprecated.since 5.0, useconstant(Duration)
static Delay
constant(Duration delay)
Creates a newConstantDelay
.abstract Duration
createDelay(long attempt)
Calculate a specific delay based on the attempt.static Supplier<Delay>
decorrelatedJitter()
Creates aSupplier
that constructs newDecorrelatedJitterDelay
instances with default boundaries.static Supplier<Delay>
decorrelatedJitter(long lower, long upper, long base, TimeUnit unit)
Creates aSupplier
that constructs newDecorrelatedJitterDelay
instances.static Supplier<Delay>
decorrelatedJitter(Duration lower, Duration upper, long base, TimeUnit targetTimeUnit)
Creates aSupplier
that constructs newDecorrelatedJitterDelay
instances.static Delay
equalJitter()
Creates a newEqualJitterDelay
with default boundaries.static Delay
equalJitter(long lower, long upper, long base, TimeUnit unit)
Creates a newEqualJitterDelay
.static Delay
equalJitter(Duration lower, Duration upper, long base, TimeUnit targetTimeUnit)
Creates a newEqualJitterDelay
.static Delay
exponential()
Creates a newExponentialDelay
with default boundaries and factor (1, 2, 4, 8, 16, 32...).static Delay
exponential(long lower, long upper, TimeUnit unit, int powersOf)
Creates a newExponentialDelay
on with custom boundaries and factor (eg.static Delay
exponential(Duration lower, Duration upper, int powersOf, TimeUnit targetTimeUnit)
Creates a newExponentialDelay
on with custom boundaries and factor (eg.static Delay
fullJitter()
Creates a newFullJitterDelay
with default boundaries.static Delay
fullJitter(long lower, long upper, long base, TimeUnit unit)
Creates a newFullJitterDelay
.static Delay
fullJitter(Duration lower, Duration upper, long base, TimeUnit targetTimeUnit)
Creates a newFullJitterDelay
.protected static long
randomBetween(long min, long max)
Generates a random long value withinmin
andmax
boundaries.
-
-
-
Constructor Detail
-
Delay
protected Delay()
Creates a newDelay
.
-
-
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 newConstantDelay
.- 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, useconstant(Duration)
Creates a newConstantDelay
.- Parameters:
delay
- the delay, must be greater or equal to 0timeUnit
- the unit of the delay.- Returns:
- a created
ConstantDelay
.
-
exponential
public static Delay exponential()
Creates a newExponentialDelay
with default boundaries and factor (1, 2, 4, 8, 16, 32...). The delay begins with 1 and is capped at 30TimeUnit.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 newExponentialDelay
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-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.- Returns:
- a created
ExponentialDelay
. - Since:
- 5.0
-
exponential
public static Delay exponential(long lower, long upper, TimeUnit unit, int powersOf)
Creates a newExponentialDelay
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-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 1- Returns:
- a created
ExponentialDelay
.
-
equalJitter
public static Delay equalJitter()
Creates a newEqualJitterDelay
with default boundaries.- Returns:
- a created
EqualJitterDelay
.
-
equalJitter
public static Delay equalJitter(Duration lower, Duration upper, long base, TimeUnit targetTimeUnit)
Creates a newEqualJitterDelay
.- Parameters:
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.- Returns:
- a created
EqualJitterDelay
. - Since:
- 5.0
-
equalJitter
public static Delay equalJitter(long lower, long upper, long base, TimeUnit unit)
Creates a newEqualJitterDelay
.- Parameters:
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.- Returns:
- a created
EqualJitterDelay
.
-
fullJitter
public static Delay fullJitter()
Creates a newFullJitterDelay
with default boundaries.- Returns:
- a created
FullJitterDelay
.
-
fullJitter
public static Delay fullJitter(Duration lower, Duration upper, long base, TimeUnit targetTimeUnit)
Creates a newFullJitterDelay
.- Parameters:
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.- Returns:
- a created
FullJitterDelay
. - Since:
- 5.0
-
fullJitter
public static Delay fullJitter(long lower, long upper, long base, TimeUnit unit)
Creates a newFullJitterDelay
.- Parameters:
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.- Returns:
- a created
FullJitterDelay
.
-
decorrelatedJitter
public static Supplier<Delay> decorrelatedJitter()
Creates aSupplier
that constructs newDecorrelatedJitterDelay
instances with default boundaries.- Returns:
- a
Supplier
ofDecorrelatedJitterDelay
.
-
decorrelatedJitter
public static Supplier<Delay> decorrelatedJitter(Duration lower, Duration upper, long base, TimeUnit targetTimeUnit)
Creates aSupplier
that constructs newDecorrelatedJitterDelay
instances.- Parameters:
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.- Returns:
- a new
Supplier
ofDecorrelatedJitterDelay
. - Since:
- 5.0
-
decorrelatedJitter
public static Supplier<Delay> decorrelatedJitter(long lower, long upper, long base, TimeUnit unit)
Creates aSupplier
that constructs newDecorrelatedJitterDelay
instances.- Parameters:
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.- Returns:
- a new
Supplier
ofDecorrelatedJitterDelay
.
-
randomBetween
protected static long randomBetween(long min, long max)
Generates a random long value withinmin
andmax
boundaries.- Parameters:
min
-max
-- Returns:
- a random value
- See Also:
ThreadLocalRandom.nextLong(long, long)
-
-