Reputation: 4116
Exploring the HTTP client binder Feign and I see that it does not support callback results (DeferredResults). How then would I handle creating a scalable endpoint for performing many time intensive tasks in parallel? Trying to avoid something like this:
val slowComputation : Future[Array[Bytes] = ???
def endpoint = {
Await.result(slowComputation(), Duration(1, SECONDS))
}
Upvotes: 0
Views: 85
Reputation: 13258
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:
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() } }
Upvotes: 1