KRetryRunner
A utility that retries a suspending operation with configurable exponential back-off and jitter.
This is a Kotlin implementation of the useRetry pattern. It manages the lifecycle of asynchronous attempts, providing reactive state for loading status, current attempt count, successful values, and the last encountered error.
Example usage:
val runner = KRetryRunner<User>(
scope = viewModelScope,
retries = 3,
delay = 500.milliseconds,
backoffFactor = 2.0,
onSuccess = { user -> println("User fetched: ${user.name}") },
onError = { err -> println("Attempt failed: ${err.message}") }
)
// Trigger manually:
runner.launch { api.getUser(id) }
// Observe in UI:
val isLoading by runner.isLoading.collectAsState()
val error by runner.error.collectAsState()
val user by runner.value.collectAsState()
if (isLoading) CircularProgressIndicator()
error?.let { Text("Error: ${it.message}") }
user?.let { UserProfile(it) }Parameters
The CoroutineScope that owns the retry jobs.
The maximum number of additional attempts after the first failure. Default: 3.
The initial wait duration between the first and second attempt. Default: 250ms.
The multiplier applied to the wait duration after each failure. Default: 2.0.
The maximum allowed delay between attempts. Default: 10s.
Optional callback invoked when an attempt succeeds.
Optional callback invoked after each failed attempt.
Type Parameters
The type of the result produced by the asynchronous operation.