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 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
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interfaceDelay.StatefulDelayInterface to be implemented by statefulDelays. -
Constructor Summary
-
Method Summary
Modifier and Type Method Description protected static DurationapplyBounds(Duration calculatedValue, Duration lower, Duration upper)static Delayconstant(int delay, TimeUnit timeUnit)Deprecated.static Delayconstant(Duration delay)Creates a newConstantDelay.abstract DurationcreateDelay(long attempt)Calculate a specific delay based on the attempt.static Supplier<Delay>decorrelatedJitter()Creates aSupplierthat constructs newDecorrelatedJitterDelayinstances with default boundaries.static Supplier<Delay>decorrelatedJitter(long lower, long upper, long base, TimeUnit unit)Creates aSupplierthat constructs newDecorrelatedJitterDelayinstances.static Supplier<Delay>decorrelatedJitter(Duration lower, Duration upper, long base, TimeUnit targetTimeUnit)Creates aSupplierthat constructs newDecorrelatedJitterDelayinstances.static DelayequalJitter()Creates a newEqualJitterDelaywith default boundaries.static DelayequalJitter(long lower, long upper, long base, TimeUnit unit)Creates a newEqualJitterDelay.static DelayequalJitter(Duration lower, Duration upper, long base, TimeUnit targetTimeUnit)Creates a newEqualJitterDelay.static Delayexponential()Creates a newExponentialDelaywith default boundaries and factor (1, 2, 4, 8, 16, 32...).static Delayexponential(long lower, long upper, TimeUnit unit, int powersOf)Creates a newExponentialDelayon with custom boundaries and factor (eg.static Delayexponential(Duration lower, Duration upper, int powersOf, TimeUnit targetTimeUnit)Creates a newExponentialDelayon with custom boundaries and factor (eg.static DelayfullJitter()Creates a newFullJitterDelaywith default boundaries.static DelayfullJitter(long lower, long upper, long base, TimeUnit unit)Creates a newFullJitterDelay.static DelayfullJitter(Duration lower, Duration upper, long base, TimeUnit targetTimeUnit)Creates a newFullJitterDelay.protected static longrandomBetween(long min, long max)Generates a random long value withinminandmaxboundaries.
-
Constructor Details
-
Delay
protected Delay()Creates a newDelay.
-
-
Method Details
-
createDelay
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
Creates a newConstantDelay.- Parameters:
delay- the delay, must be greater or equal to 0.- Returns:
- a created
ConstantDelay.
-
constant
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
Creates a newExponentialDelaywith default boundaries and factor (1, 2, 4, 8, 16, 32...). The delay begins with 1 and is capped at 30TimeUnit.SECONDSafter reaching the 16th attempt.- Returns:
- a created
ExponentialDelay.
-
exponential
public static Delay exponential(Duration lower, Duration upper, int powersOf, TimeUnit targetTimeUnit)Creates a newExponentialDelayon 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
Creates a newExponentialDelayon 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
Creates a newEqualJitterDelaywith 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
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
Creates a newFullJitterDelaywith default boundaries.- Returns:
- a created
FullJitterDelay.
-
fullJitter
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
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.
-
randomBetween
protected static long randomBetween(long min, long max)Generates a random long value withinminandmaxboundaries.- Parameters:
min-max-- Returns:
- a random value
- See Also:
ThreadLocalRandom.nextLong(long, long)
-
applyBounds
-
constant(Duration)