public class EBRetryStrategyBackoff extends Object implements BackOff, EBRetryStrategy
BackOff
that increases the back off period for each retry attempt using
a randomization function that grows exponentially.
nextBackOffMillis()
is calculated using the following formula:
randomized_interval = retry_interval * (random value in range [1 - randomization_factor, 1 + randomization_factor])
In other words nextBackOffMillis()
will range between the randomization factor
percentage below and above the retry interval. For example, using 2 seconds as the base retry
interval and 0.5 as the randomization factor, the actual back off period used in the next retry
attempt will be between 1 and 3 seconds.
Note: max_interval caps the retry_interval and not the randomized_interval.
If the time elapsed since an EBRetryStrategyBackoff
instance is created goes past the
max_elapsed_time then the method nextBackOffMillis()
starts returning
BackOff.STOP
. The elapsed time can be reset by calling reset()
.
Example: The default retry_interval is .5 seconds, default randomization_factor is 0.5, default multiplier is 1.5 and the default max_interval is 1 minute. For 10 tries the sequence will be (values in seconds) and assuming we go over the max_elapsed_time on the 10th try:
request# retry_interval randomized_interval
1 0.5 [0.25, 0.75]
2 0.75 [0.375, 1.125]
3 1.125 [0.562, 1.687]
4 1.687 [0.8435, 2.53]
5 2.53 [1.265, 3.795]
6 3.795 [1.897, 5.692]
7 5.692 [2.846, 8.538]
8 8.538 [4.269, 12.807]
9 12.807 [6.403, 19.210]
10 19.210 BackOff.STOP
Implementation is not thread-safe.
Modifier and Type | Class and Description |
---|---|
static class |
EBRetryStrategyBackoff.Builder
Builder for
EBRetryStrategyBackoff . |
Modifier and Type | Field and Description |
---|---|
static int |
DEFAULT_INITIAL_INTERVAL_MILLIS
The default initial interval value in milliseconds (0.5 seconds).
|
static int |
DEFAULT_MAX_ATTEMPTS
The default maximum number of attempts.
|
static int |
DEFAULT_MAX_ELAPSED_TIME_MILLIS
The default maximum elapsed time in milliseconds (15 minutes).
|
static int |
DEFAULT_MAX_INTERVAL_MILLIS
The default maximum back off time in milliseconds (1 minute).
|
static double |
DEFAULT_MULTIPLIER
The default multiplier value (1.5 which is 50% increase per back off).
|
static double |
DEFAULT_RANDOMIZATION_FACTOR
The default randomization factor (0.5 which results in a random period ranging between 50%
below and 50% above the retry interval).
|
protected static String |
FIELD_INITIAL_INTERVAL_MILLIS |
protected static String |
FIELD_MAX_ATTEMPTS |
protected static String |
FIELD_MAX_ELAPSED_TIME_MILLIS |
protected static String |
FIELD_MAX_INTERVAL_MILLIS |
protected static String |
FIELD_MULTIPLIER |
protected static String |
FIELD_RANDOMIZATION_FACTOR |
static String |
NAME |
STOP, STOP_BACKOFF, ZERO_BACKOFF
Modifier | Constructor and Description |
---|---|
|
EBRetryStrategyBackoff()
Creates an instance of ExponentialBackOffPolicy using default values.
|
protected |
EBRetryStrategyBackoff(EBRetryStrategyBackoff.Builder builder) |
|
EBRetryStrategyBackoff(org.json.JSONObject json) |
Modifier and Type | Method and Description |
---|---|
EBRetryStrategy |
copy() |
int |
getCurrentIntervalMillis()
Returns the current retry interval in milliseconds.
|
long |
getElapsedTimeMillis()
Returns the elapsed time in milliseconds since an
EBRetryStrategyBackoff instance is
created and is reset when reset() is called. |
int |
getInitialIntervalMillis()
Returns the initial retry interval in milliseconds.
|
int |
getMaxAttempts()
Return the maximum number of tries that will be attempted for the EBRetryJob instance.
|
int |
getMaxElapsedTimeMillis()
Returns the maximum elapsed time in milliseconds.
|
int |
getMaxIntervalMillis()
Returns the maximum value of the back off period in milliseconds.
|
double |
getMultiplier()
Returns the value to multiply the current interval with for each retry attempt.
|
String |
getName() |
double |
getRandomizationFactor()
Returns the randomization factor to use for creating a range around the retry interval.
|
long |
getWaitMilli() |
long |
nextBackOffMillis()
Gets the number of milliseconds to wait before retrying the operation or
BackOff.STOP to
indicate that no retries should be made. |
long |
nextBackOffMillis(boolean inc) |
void |
onFail() |
void |
onSuccess() |
void |
reset()
Sets the interval back to the initial retry interval and restarts the timer.
|
boolean |
shouldContinue() |
org.json.JSONObject |
toJSON(org.json.JSONObject json) |
public static final String NAME
public static final int DEFAULT_INITIAL_INTERVAL_MILLIS
public static final double DEFAULT_RANDOMIZATION_FACTOR
public static final double DEFAULT_MULTIPLIER
public static final int DEFAULT_MAX_INTERVAL_MILLIS
public static final int DEFAULT_MAX_ELAPSED_TIME_MILLIS
public static final int DEFAULT_MAX_ATTEMPTS
protected static final String FIELD_INITIAL_INTERVAL_MILLIS
protected static final String FIELD_RANDOMIZATION_FACTOR
protected static final String FIELD_MULTIPLIER
protected static final String FIELD_MAX_INTERVAL_MILLIS
protected static final String FIELD_MAX_ELAPSED_TIME_MILLIS
protected static final String FIELD_MAX_ATTEMPTS
public EBRetryStrategyBackoff()
To override the defaults use EBRetryStrategyBackoff.Builder
.
initialIntervalMillis
defaults to DEFAULT_INITIAL_INTERVAL_MILLIS
randomizationFactor
defaults to DEFAULT_RANDOMIZATION_FACTOR
multiplier
defaults to DEFAULT_MULTIPLIER
maxIntervalMillis
defaults to DEFAULT_MAX_INTERVAL_MILLIS
maxElapsedTimeMillis
defaults in DEFAULT_MAX_ELAPSED_TIME_MILLIS
maxAttempts
defaults in DEFAULT_MAX_ATTEMPTS
public EBRetryStrategyBackoff(org.json.JSONObject json)
protected EBRetryStrategyBackoff(EBRetryStrategyBackoff.Builder builder)
builder
- builderpublic long nextBackOffMillis() throws IOException
BackOff.STOP
to
indicate that no retries should be made.
Example usage:
long backOffMillis = backoff.nextBackOffMillis(); if (backOffMillis == Backoff.STOP) { // do not retry operation } else { // sleep for backOffMillis milliseconds and retry operation }
This method calculates the next back off interval using the formula: randomized_interval = retry_interval +/- (randomization_factor * retry_interval)
Subclasses may override if a different algorithm is required.
nextBackOffMillis
in interface BackOff
IOException
- IO exceptions only (beyond software control)public long nextBackOffMillis(boolean inc) throws IOException
IOException
public final int getInitialIntervalMillis()
public final double getRandomizationFactor()
A randomization factor of 0.5 results in a random period ranging between 50% below and 50% above the retry interval.
public final int getCurrentIntervalMillis()
public final double getMultiplier()
public final int getMaxIntervalMillis()
public final int getMaxAttempts()
public final int getMaxElapsedTimeMillis()
If the time elapsed since an EBRetryStrategyBackoff
instance is created goes past the
max_elapsed_time then the method nextBackOffMillis()
starts returning
BackOff.STOP
. The elapsed time can be reset by calling reset()
.
public final long getElapsedTimeMillis()
EBRetryStrategyBackoff
instance is
created and is reset when reset()
is called.
The elapsed time is computed using System.nanoTime()
.
public String getName()
getName
in interface EBRetryStrategy
public void onFail()
onFail
in interface EBRetryStrategy
public void onSuccess()
onSuccess
in interface EBRetryStrategy
public final void reset()
reset
in interface BackOff
reset
in interface EBRetryStrategy
public boolean shouldContinue()
shouldContinue
in interface EBRetryStrategy
public long getWaitMilli()
getWaitMilli
in interface EBRetryStrategy
public EBRetryStrategy copy()
copy
in interface EBRetryStrategy
public org.json.JSONObject toJSON(org.json.JSONObject json)
toJSON
in interface EBRetryStrategy
Copyright © 2016. All rights reserved.