Do I understand this correctly: The 'def endpoint' is a synchronous blocking method, and you cannot change that fact, since it dictated by a framework?
Now, that means you have to block there waiting for the computation / IO to come back to you. That also means you use one thread up there.
I think the best you can do it to prevent 'overloading' that endpoint with to many waiting threads. So:
- Like you did, specify a timeout.
- Think about how many 'waiting threads' you're willing to accept. Then shed off more requests if to many are waiting. Like:
val waiting = new AtomicInteger(0)
val maxThreadsWaiting = 200
def endpoint() ={
try{
val numberThreadsWaiting = waiting.incrementAndGet()
if(numberThreadsWaiting > maxThreadsWaiting) {
// Return 'overload' failure. Like HTTP 503
} else{
Await.result(slowComputation(), Duration(1, SECONDS))
}
} finally {
waiting.decrementAndGet()
}
}