Package io.hekate.messaging.retry
Class ExponentialBackoffPolicy
- java.lang.Object
-
- io.hekate.messaging.retry.ExponentialBackoffPolicy
-
- All Implemented Interfaces:
RetryBackoffPolicy
public class ExponentialBackoffPolicy extends Object implements RetryBackoffPolicy
A backoff policy that waits exponentially longer between each attempt (but keeps a constant delay once a maximum delay is reached).This policy uses the following formula to calculate the backoff delay:
(2 ^ (attempt -1)) * baseDelay
Note: for the very first attempt the answer is always
0
(i.e. retry immediately).Parameters of this policy are:
BaseDelay
- multiplier for each attemptMaxDelay
- maximum delay (calculated delay will never exceed this value)
Examples of calculations:
BaseDelay: 10 MaxDelay: 3000 ------------------------------- Attempt 0 -> 0 Attempt 1 -> 10 Attempt 2 -> 20 Attempt 3 -> 40 Attempt 4 -> 80 Attempt 5 -> 160 Attempt 6 -> 320 Attempt 7 -> 640 Attempt 8 -> 1280 Attempt 9 -> 2560 Attempt 10 -> 3000 ... -> 3000
BaseDelay: 500 MaxDelay: 30000 ------------------------------- Attempt 0 -> 0 Attempt 1 -> 500 Attempt 2 -> 1000 Attempt 3 -> 2000 Attempt 4 -> 4000 Attempt 5 -> 8000 Attempt 6 -> 16000 Attempt 7 -> 30000 ... -> 30000
-
-
Field Summary
Fields Modifier and Type Field Description static long
DEFAULT_BASE_DELAY
Default base delay (=50L) in milliseconds.static long
DEFAULT_MAX_DELAY
Default maximum delay (=3000L) in milliseconds.
-
Constructor Summary
Constructors Constructor Description ExponentialBackoffPolicy()
Constructs a new instance with default values.ExponentialBackoffPolicy(long baseDelay, long maxDelay)
Constructs a new instance.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description long
baseDelay()
Returns the base delay of this policy in milliseconds.long
delayBeforeRetry(int attempt)
Returns a delay in milliseconds before the next retry attempt.long
maxDelay()
Returns the maximum delay of this policy in milliseconds.String
toString()
-
-
-
Field Detail
-
DEFAULT_BASE_DELAY
public static final long DEFAULT_BASE_DELAY
Default base delay (=50L) in milliseconds.- See Also:
- Constant Field Values
-
DEFAULT_MAX_DELAY
public static final long DEFAULT_MAX_DELAY
Default maximum delay (=3000L) in milliseconds.- See Also:
- Constant Field Values
-
-
Constructor Detail
-
ExponentialBackoffPolicy
public ExponentialBackoffPolicy()
Constructs a new instance with default values.- See Also:
DEFAULT_BASE_DELAY
,DEFAULT_MAX_DELAY
-
ExponentialBackoffPolicy
public ExponentialBackoffPolicy(long baseDelay, long maxDelay)
Constructs a new instance.- Parameters:
baseDelay
- Multiplier for each attempt (in milliseconds).maxDelay
- Maximum delay in milliseconds (calculated delay will never exceed this value).
-
-
Method Detail
-
baseDelay
public long baseDelay()
Returns the base delay of this policy in milliseconds.- Returns:
- Base delay in milliseconds.
-
maxDelay
public long maxDelay()
Returns the maximum delay of this policy in milliseconds.- Returns:
- Maximum delay in milliseconds.
-
delayBeforeRetry
public long delayBeforeRetry(int attempt)
Description copied from interface:RetryBackoffPolicy
Returns a delay in milliseconds before the next retry attempt.- Specified by:
delayBeforeRetry
in interfaceRetryBackoffPolicy
- Parameters:
attempt
- Attempt (starting with zero for the very first attempt).- Returns:
- Delay in milliseconds.
-
-