Skip to content

Instantly share code, notes, and snippets.

@stephennancekivell
Created February 22, 2015 22:22
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 stephennancekivell/e778101317cc55b6720a to your computer and use it in GitHub Desktop.
Save stephennancekivell/e778101317cc55b6720a to your computer and use it in GitHub Desktop.
WS executeRecovering
import play.api.libs.ws._
def get(url: String): Future[WSResponse] = {
val split = url.split("/")
val hostname = split(0)
val path = "/"+split(1)
def getHosts = Try(InetAddress.getAllByName(hostname).map(_.getHostAddress).toSeq)
def executeRecovering(triedHosts: Seq[String] = Nil): Future[WSResponse] = {
getHosts match {
case Failure(e) => Future.failed(e)
case Success(hosts) =>
val unTriedHosts = hosts.filterNot(triedHosts.contains)
unTriedHosts.size match {
case 0 => Future.failed(new RuntimeException("no can do"))
case 1 => WS.url(unTriedHosts.head+path).get()
case _ => WS.url(unTriedHosts.head+path).get().recoverWith {
case e => executeRecovering(triedHosts ++ Seq(unTriedHosts.head))
}
}
}
}
executeRecovering()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment