Skip to content

Instantly share code, notes, and snippets.

@sephlietz
Created April 1, 2014 15:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sephlietz/9917137 to your computer and use it in GitHub Desktop.
Save sephlietz/9917137 to your computer and use it in GitHub Desktop.
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