All Downloads are FREE. Search and download functionalities are using the official Maven repository.

jvmMain.com.steamstreet.exceptions.Retry.kt Maven / Gradle / Ivy

There is a newer version: 2.0.25
Show newest version
package com.steamstreet.exceptions

import kotlinx.coroutines.delay
import kotlin.reflect.KClass

/**
 * Retry if an exception is encountered.
 *
 * @param throwHandler called whenever an exception is thrown, even if we're still retrying, allowing for logging.
 */
public suspend fun  retry(
    times: Int,
    exceptionType: KClass? = null,
    delay: Long = 0,
    throwHandler: (Throwable) -> Unit = {},
    block: suspend () -> R
): R {
    for (i in 0 until times) {
        try {
            return block()
        } catch (e: Throwable) {
            if ((exceptionType == null || e.javaClass.kotlin == exceptionType) && i < times - 1) {
                throwHandler(e)
                if (delay > 0) {
                    delay(delay)
                }
            } else {
                throw e
            }
        }
    }
    // we'll never get here, so just throw
    throw IllegalStateException("This should never happen in retry")
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy