Created
April 1, 2014 15:59
-
-
Save sephlietz/9917137 to your computer and use it in GitHub Desktop.
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import scala.concurrent.duration._ | |
object AppThatUsesRetry extends App { | |
Retry(backoff = 30 seconds) { | |
throw new Exception("fail every 30 seconds") | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
that is lovely ... I will borrow that ;)