Created
September 7, 2013 01:22
-
-
Save arschles/6471991 to your computer and use it in GitHub Desktop.
how to time out a scala future
This file contains hidden or 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
val realFuture = Future(doRealStuff()) | |
//timeoutFuture does a spin wait by sleeping on the thread on which it runs, so make sure the ExecutionContext | |
//on which it runs is multi-threaded. In an akka based environment, use a scheduler to fulfill a promise with a failure | |
//when the timeout duration is complete, and avoid the spin wait | |
val timeoutFuture = Future { | |
Thread.sleep(timeoutMilliseconds) | |
throw new TimeoutException("timeout") | |
} | |
Future.firstCompletedOf(realFuture :: timeoutFuture :: Nil) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment