retry With Exponential Backoff
Executes a suspend operation with retry capabilities using exponential backoff and jitter.
This is the primary retry function for most use cases. It uses exponential backoff strategy with automatic jitter (10%) to prevent thundering herd problems.
Retry Behavior:
Total attempts = initial attempt + retries
Delay grows exponentially: base * factor^attempt
Jitter (±10%) randomizes delays to avoid synchronized retries
All exceptions are retried (except CancellationException)
Return
The result of the successful execution
Parameters
The maximum number of retry attempts after the initial attempt (defaults to 3)
The base duration for exponential backoff (defaults to 200 milliseconds)
The exponential factor (defaults to 2.0)
The suspend operation to execute
Type Parameters
The return type of the suspend operation
Throws
if all retry attempts fail
Example usage:
val user = retryWithExponentialBackoff(retries = 5) {
userService.fetchUser("user123")
}