Skip to content
This repository was archived by the owner on Feb 13, 2020. It is now read-only.
This repository was archived by the owner on Feb 13, 2020. It is now read-only.

Retry function #127

Description

@jcornaz

Although it is not really related to coroutines or channels I think it fits this lib because:

  • Suspending functions are good fit for calling external API, disk access etc. and all sort of things for which having retry mechanism is often useful.
  • One purpose of this project is to make actually easier and pleasant to use coroutines and channels instead of RxJava or equivalent reactive library. And theses reactive library usually provide a retry mechanism.

Implementation example:

suspend inline fun <R> retry(maxAttempts: Int, delayBeforeRetry: Long = 0L, action: () -> R): R {
  require(maxAttempts > 0) { "Invalid max attempts: $maxAttempts" }

  lateinit var exception: Throwable

  repeat(maxAttempts) {
    try {
      return action()
    } catch (t: Throwable) {
      exception = t
      delay(delayBeforeRetry)
    }
  }

  throw exception
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    featureAdd a new feature

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions