Skip to content

Instantly share code, notes, and snippets.

@astray1988
Created March 27, 2016 20:55
Show Gist options
  • Select an option

  • Save astray1988/d740fe7be2445ef79681 to your computer and use it in GitHub Desktop.

Select an option

Save astray1988/d740fe7be2445ef79681 to your computer and use it in GitHub Desktop.
//Define Retry Function
def withRetry[T](retries: Int = 5)(f: => Future[T]): Future[T] = f.recoverWith {
case t:Throwable if (retries > 0) => withRetry(retries - 1)(f)
}
// Use thrid party lib
libraryDependencies += "net.databinder.dispatch" % "dispatch-core_2.10" % "0.11.2"
import dispatch._, Defaults._
//Use retry mechanism if network fails, try 5 seconds after network failure, maximum try 4 times
retry.Backoff(max = 5, delay = 5.seconds)(() => MockPriceService.price(prodInfo.productId)).map {
case None => NotFound("The price of this product is not exist")
case Some(prodPrice) =>
Ok(views.html.productDetail(prodInfo, prodPrice))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment