1

Suppose there is a method that return future def getSomeResult(): Future[SomeResult]

it may return different results depending when is it called, in particular I'm interested in success result from this method and want to call that method again if result is failure. I have a strong assurance that eventually that method will return success result So, I wrote next recursive function

def rec(): Future[SomeResult] = {
   getSomeResult().recoverWith{case e => rec()}
}

What bad can happen using method rec()? I suspect that StackOverflowException is not the problem here, because future is running on different thread with its own stack. Another problem that I also suspect is that all threads in the thread pool may be busy trying to execute this future, so this is a kind of starvation, is it true?

maks
  • 5,911
  • 17
  • 79
  • 123
  • As of your code, new `rec()` call will be executed (and respective Runnable/Callable will be submitted to the pool) only after previous one is finished (failed), so I expect no Thread starvation in that simple case. – dk14 Feb 02 '17 at 16:36
  • @dk14 so, there are no any problems with that function? – maks Feb 02 '17 at 16:51
  • except your service might always fail (you'll get halt problem even without starvation), so you might need some limited number of retries, logging, etc... same problems as with any recursive function, tail-optimized or not. The best strategy to check those edge cases is to write a simple load test - it would provide you with all necessary info – dk14 Feb 02 '17 at 20:14

0 Answers0