exponential
fun exponential(initialDelay: Duration, maxDelay: Duration = Duration.INFINITE, multiplier: Double = 2.0, randomizationFactor: Double = 0.0): DelayStrategy(source)
A delay strategy that uses the exponential backoff algorithm to calculate the next delay duration. The algorithm is based on the formula:
(initialDelay * multiplier^(attempt - 1)) + jitter
, whereattempt
is the current delay attempt which starts at 1.
Example:
exponential(500.milliseconds, 2.0, 1.minutes)
// Delay between transitions will be as follows:
// [500ms, 1s, 2s, 4s, 8s, 16s, 32s, 1m, 1m, 1m, ...]
exponential(500.milliseconds, 2.0, 1.minutes, 0.1)
// Delay between transitions will be something like:
// [450ms, 1.1s, 2.2s, 3.9s, 8.1s, 15.8s, 32.3s, 1m, 1m, 1m, ...]
Content copied to clipboard
Note:
Because the jitter calculation is based on the newly calculated delay, the new delay could be less than the previous value.
Parameters
initialDelay
The initial delay before the first attempt.
maxDelay
The maximum delay between attempts. Used as a safety net to prevent infinite delays.
multiplier
The multiplier to increase the delay between attempts.
randomizationFactor
the randomization factor to add randomness to the calculated delay (e.g., 0.1 for +/-10%).