Skip to content

Instantly share code, notes, and snippets.

@JohnMurray
Last active January 15, 2018 08:43
Show Gist options
  • Save JohnMurray/d43be3d683d4496b6663c5a9ef2da9bb to your computer and use it in GitHub Desktop.
Save JohnMurray/d43be3d683d4496b6663c5a9ef2da9bb to your computer and use it in GitHub Desktop.
Simple retries with future-based networking code in Scala (with back-off)
def networkRequestWithRetries()(implicit as: ActorSystem): Future[String] = {
networkRequest().recoverWith {
case NetworkException =>
println("retrying")
after(2.seconds, as.scheduler, global, Future.successful(1)).flatMap { _ =>
networkRequestWithRetries()
}
case t: Throwable => throw t
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment