Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
import scala.concurrent.duration._
object AppThatUsesRetry extends App {
Retry(backoff = 30 seconds) {
throw new Exception("fail every 30 seconds")
}
}
import scala.util.{Failure, Success, Try}
import scala.concurrent.duration._
object Retry {
@annotation.tailrec
def apply[A](backoff: Duration = 5 seconds)(f: => A): A = {
Try(f) match {
case Success(x) => x
case Failure(e) =>
Thread.sleep(backoff.toMillis)
Retry(backoff) {
f
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment