Skip to content

Instantly share code, notes, and snippets.

@sephlietz
Created April 1, 2014 15:59
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
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
}
}
}
}
@AnirudhVyas
Copy link

that is lovely ... I will borrow that ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment