This trait is used to provide a non-blocking timeout on an operation that returns a Future.
Please note that the play.api.Application default ActorSystem should be used as input here, as the actorSystem.scheduler is responsible for scheduling the timeout, using akka.pattern.actor under the hood.
You can dependency inject the ActorSystem as follows to create a Future that will timeout after a certain period of time:
class MyService(val actorSystem: ActorSystem) extends Timeout { def calculateWithTimeout(timeoutDuration: FiniteDuration): Future[Int] = { timeout(actorSystem, timeoutDuration)(rawCalculation()) } def rawCalculation(): Future[Int] = { import akka.pattern.after implicit val ec = actorSystem.dispatcher akka.pattern.after(300 millis, actorSystem.scheduler)(Future(42))(actorSystem.dispatcher) } }
You should check for timeout by using Future.recover() or Future.recoverWith() and checking for TimeoutException:
val future = myService.calculateWithTimeout(100 millis).recover { case _: TimeoutException => -1 }
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4