@FunctionalInterface public interface Backoff extends Unwrappable
| Modifier and Type | Method and Description |
|---|---|
default <T> T |
as(Class<T> type)
|
static Backoff |
exponential(long initialDelayMillis,
long maxDelayMillis)
Returns a
Backoff that waits an exponentially-increasing amount of time between attempts. |
static Backoff |
exponential(long initialDelayMillis,
long maxDelayMillis,
double multiplier)
Returns a
Backoff that waits an exponentially-increasing amount of time between attempts. |
static Backoff |
fibonacci(long initialDelayMillis,
long maxDelayMillis)
Returns a
Backoff for which the backoff delay increases in line with the Fibonacci sequence
f(n) = f(n-1) + f(n-2) where f(0) = f(1) = initialDelayMillis. |
static Backoff |
fixed(long delayMillis)
Returns a
Backoff that waits a fixed delay between attempts. |
long |
nextDelayMillis(int numAttemptsSoFar)
Returns the number of milliseconds to wait for before attempting a retry.
|
static Backoff |
of(String specification)
Creates a new
Backoff that computes backoff delay using one of
exponential(long, long, double), fibonacci(long, long), fixed(long)
and random(long, long) chaining with withJitter(double, double) and
withMaxAttempts(int) from the specification string that conforms to
the following format:
exponential=[initialDelayMillis:maxDelayMillis:multiplier] is for
exponential(long, long, double) (multiplier will be 2.0 if it's omitted)
fibonacci=[initialDelayMillis:maxDelayMillis] is for
fibonacci(long, long)
fixed=[delayMillis] is for fixed(long)
random=[minDelayMillis:maxDelayMillis] is for random(long, long)
jitter=[minJitterRate:maxJitterRate] is for withJitter(double, double)
(if only one jitter value is specified, it will be used for withJitter(double)
maxAttempts=[maxAttempts] is for withMaxAttempts(int)
The order of options does not matter, and the specification needs at least one option. |
static Backoff |
ofDefault()
Returns the default
Backoff. |
static Backoff |
random(long minDelayMillis,
long maxDelayMillis)
Returns a
Backoff that computes backoff delay which is a random value between
minDelayMillis and maxDelayMillis chosen by ThreadLocalRandom. |
static Backoff |
random(long minDelayMillis,
long maxDelayMillis,
Supplier<Random> randomSupplier)
Returns a
Backoff that computes backoff delay which is a random value between
minDelayMillis and maxDelayMillis. |
default Backoff |
withJitter(double jitterRate)
Returns a
Backoff that adds a random jitter value to the original delay using
full jitter strategy. |
default Backoff |
withJitter(double minJitterRate,
double maxJitterRate)
Returns a
Backoff that adds a random jitter value to the original delay using
full jitter strategy. |
default Backoff |
withJitter(double minJitterRate,
double maxJitterRate,
Supplier<Random> randomSupplier)
Returns a
Backoff that adds a random jitter value to the original delay using
full jitter strategy. |
default Backoff |
withMaxAttempts(int maxAttempts)
Returns a
Backoff which limits the number of attempts up to the specified value. |
static Backoff |
withoutDelay()
Returns a
Backoff that will never wait between attempts. |
static Backoff withoutDelay()
Backoff that will never wait between attempts. In most cases, using back off
without delay is very dangerous. Please consider using exponential(long, long) with
withJitter(double) or fixed(long) with pre calculated delay depending on the situation.static Backoff fixed(long delayMillis)
Backoff that waits a fixed delay between attempts.static Backoff exponential(long initialDelayMillis, long maxDelayMillis)
Backoff that waits an exponentially-increasing amount of time between attempts.static Backoff exponential(long initialDelayMillis, long maxDelayMillis, double multiplier)
Backoff that waits an exponentially-increasing amount of time between attempts.static Backoff fibonacci(long initialDelayMillis, long maxDelayMillis)
Backoff for which the backoff delay increases in line with the Fibonacci sequence
f(n) = f(n-1) + f(n-2) where f(0) = f(1) = initialDelayMillis.static Backoff random(long minDelayMillis, long maxDelayMillis)
Backoff that computes backoff delay which is a random value between
minDelayMillis and maxDelayMillis chosen by ThreadLocalRandom.static Backoff random(long minDelayMillis, long maxDelayMillis, Supplier<Random> randomSupplier)
Backoff that computes backoff delay which is a random value between
minDelayMillis and maxDelayMillis.static Backoff of(String specification)
Backoff that computes backoff delay using one of
exponential(long, long, double), fibonacci(long, long), fixed(long)
and random(long, long) chaining with withJitter(double, double) and
withMaxAttempts(int) from the specification string that conforms to
the following format:
exponential=[initialDelayMillis:maxDelayMillis:multiplier] is for
exponential(long, long, double) (multiplier will be 2.0 if it's omitted)fibonacci=[initialDelayMillis:maxDelayMillis] is for
fibonacci(long, long)fixed=[delayMillis] is for fixed(long)random=[minDelayMillis:maxDelayMillis] is for random(long, long)jitter=[minJitterRate:maxJitterRate] is for withJitter(double, double)
(if only one jitter value is specified, it will be used for withJitter(double)maxAttempts=[maxAttempts] is for withMaxAttempts(int)specification needs at least one option.
If you don't specify the base option exponential backoff will be used. If you only specify
a base option, jitter and maxAttempts will be set by default values. For example:
exponential=200:10000:2.0,jitter=0.2 (default)exponential=200:10000,jitter=0.2,maxAttempts=50 (multiplier omitted)fibonacci=200:10000,jitter=0.2,maxAttempts=50fixed=100,jitter=-0.5:0.2,maxAttempts=10 (fixed backoff with jitter variation)random=200:1000 (jitter and maxAttempts will be set by default values)specification - the specification used to create a Backofflong nextDelayMillis(int numAttemptsSoFar)
numAttemptsSoFar - the number of attempts made by a client so far, including the first attempt and
its following retries.IllegalArgumentException - if numAttemptsSoFar is equal to or less than 0default <T> T as(Class<T> type)
as in interface Unwrappabletype - the type of the desired BackoffBackoff which is an instance of type if this Backoff
decorated such a Backoff, or null otherwise.Unwrappabledefault Backoff withJitter(double jitterRate)
Backoff that adds a random jitter value to the original delay using
full jitter strategy.
The jitterRate is used to calculate the lower and upper bound of the ultimate delay.
The lower bound will be ((1 - jitterRate) * originalDelay) and the upper bound will be
((1 + jitterRate) * originalDelay). For example, if the delay returned by
exponential(long, long) is 1000 milliseconds and the provided jitter value is 0.3,
the ultimate backoff delay will be chosen between 1000 * (1 - 0.3) and 1000 * (1 + 0.3)
by ThreadLocalRandom. The rate value should be between 0.0 and 1.0.jitterRate - the rate that used to calculate the lower and upper bound of the backoff delayIllegalArgumentException - if jitterRate is a negative value or greater than 1.0default Backoff withJitter(double minJitterRate, double maxJitterRate)
Backoff that adds a random jitter value to the original delay using
full jitter strategy.
The minJitterRate and maxJitterRate is used to calculate the lower and upper bound
of the ultimate delay. The lower bound will be ((1 - minJitterRate) * originalDelay) and the
upper bound will be ((1 + maxJitterRate) * originalDelay). For example, if the delay
returned by exponential(long, long) is 1000 milliseconds and the minJitterRate
is -0.2, maxJitterRate is 0.3, the ultimate backoff delay will be chosen between
1000 * (1 - 0.2) and 1000 * (1 + 0.3) by ThreadLocalRandom.
The rate values should be between -1.0 and 1.0.minJitterRate - the rate that used to calculate the lower bound of the backoff delaymaxJitterRate - the rate that used to calculate the upper bound of the backoff delayIllegalArgumentException - if minJitterRate is greater than maxJitterRate or if the
minJitterRate and maxJitterRate values are not in
between -1.0 and 1.0default Backoff withJitter(double minJitterRate, double maxJitterRate, Supplier<Random> randomSupplier)
Backoff that adds a random jitter value to the original delay using
full jitter strategy.
The minJitterRate and maxJitterRate is used to calculate the lower and upper bound
of the ultimate delay. The lower bound will be ((1 - minJitterRate) * originalDelay) and the
upper bound will be ((1 + maxJitterRate) * originalDelay). For example, if the delay
returned by exponential(long, long) is 1000 milliseconds and the minJitterRate
is -0.2, maxJitterRate is 0.3, the ultimate backoff delay will be chosen between
1000 * (1 - 0.2) and 1000 * (1 + 0.3). The rate values should be between -1.0 and 1.0.minJitterRate - the rate that used to calculate the lower bound of the backoff delaymaxJitterRate - the rate that used to calculate the upper bound of the backoff delayrandomSupplier - the supplier that provides Random in order to calculate the ultimate delayIllegalArgumentException - if minJitterRate is greater than maxJitterRate or if the
minJitterRate and maxJitterRate values are not in
between -1.0 and 1.0Copyright © 2020 LeanCloud. All rights reserved.