retry With Exponential Backoff
fun <T> ERROR CLASS: Symbol not found for Flow<T>.retryWithExponentialBackoff(maxAttempts: Int, initialDelay: ERROR CLASS: Symbol not found for Duration, multiplier: Double = 2.0, retryOn: (Throwable) -> Boolean = { true }): ERROR CLASS: Symbol not found for Flow<T>(source)
Retries the flow when it fails, while increasing the delay between retries.
The wait time grows exponentially: initialDelay * multiplier^attempt.
The flow stops retrying after maxAttempts or if retryOn returns false.
Example:
flow {
emit(fetchData())
}
.retryWithExponentialBackoff(
maxAttempts = 5,
initialDelay = 100.milliseconds,
multiplier = 2.0
)
.collect { data ->
println("Got $data")
}Content copied to clipboard
In this example, if fetchData() fails, it will retry up to 5 times, waiting 100ms, 200ms, 400ms, 800ms, and 1600ms between tries.