repeatWithExponentialBackoff

suspend fun <R> repeatWithExponentialBackoff(times: Long = 3, base: Duration = 1.seconds, factor: Double = 2.0, block: suspend () -> R): R(source)

Repeats a successful operation with exponential backoff delay between repetitions.

This is useful for operations that need to be executed periodically with increasing intervals, such as periodic polling or gradual backoff scenarios.

Return

The result of the last successful execution

Parameters

times

The number of times to repeat after the initial execution (defaults to 3)

base

The base duration for exponential backoff (defaults to 1 second)

factor

The exponential factor (defaults to 2.0)

block

The suspend operation to repeat

Type Parameters

R

The return type of the suspend operation

Throws

if any execution fails

Example usage:

val lastPoll = repeatWithExponentialBackoff(times = 5) {
pollService.check()
}