A basic infinite retry implementation. More extreme versions: (1) https://gist.github.com/Mortimerp9/5430595, (2) https://github.com/softprops/retry and (3) http://stackoverflow.com/questions/7930814/whats-the-scala-way-to-implement-a-retry-able-call-like-this-one
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
This comment has been minimized.
that is lovely ... I will borrow that ;)