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(TimeUnit timeUnit)
Creates a new
Delay . |
Modifier and Type | Method and Description |
---|---|
protected static long |
applyBounds(long calculatedValue,
long lower,
long upper) |
static Delay |
constant(int delay,
TimeUnit timeUnit)
Creates a new
ConstantDelay . |
abstract long |
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(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(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(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(long lower,
long upper,
long base,
TimeUnit unit)
Creates a new
FullJitterDelay . |
TimeUnit |
getTimeUnit()
|
protected static long |
randomBetween(long min,
long max)
Generates a random long value within
min and max boundaries. |
public abstract long createDelay(long attempt)
attempt
- the attempt to calculate the delay from.public static Delay constant(int delay, TimeUnit timeUnit)
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(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(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(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(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)
protected static long applyBounds(long calculatedValue, long lower, long upper)
Copyright © 2018 lettuce.io. All rights reserved.