Skip to content

nwillc/kretry

Repository files navigation

Coverage license Build Status Download

Kretry: A retying Try

val config = Config<String>().apply {
    attempts = 10
}
val result: String = retry(config) {
    // some code that can fail, possibly with an exception
}

The above will retry the code block, up to 10 time, until it completes without an exception. If within 10 attempts it never passes a RetryExceededException will be thrown.

Or you can use the Retry class which is a retrying Try. It won't throw an exception but results in either a Success or Failure instance with the appropriate value of the result or the exception:

val result = Retry {
    // some code that can fail, possibly with an exception
}

if (result is Success) { } else { }

Wait, Kotlin Has a Try...

Kotlin introduced Result and runCatching which is effectively Try support. To support this, with retrying, retryCatching was added:

val result = retryCatching {
    // some code that can fail, possibly with an exception
}

if (result.isSuccess) { } else { }

See Also

About

Kotlin Try monad with retrying built in.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages