retryWithBackoff

fun <T> Flow<T>.retryWithBackoff(retries: Int, initialDelay: Duration, factor: Double = 2.0, maxDelay: Duration = Duration.INFINITE, retryIf: (Throwable) -> Boolean = { true }): Flow<T>

Retries a failed upstream up to retries times with exponentially growing delays.

The delay before retry n (0-based) is initialDelay * factor.pow(n), capped at maxDelay. A failure is retried only while retryIf returns true for it; CancellationException is never retried. Once retries are exhausted the last failure is rethrown to the collector.

Throws

if retries is negative, initialDelay is negative, or factor is not positive.