tgk
tgk

Reputation: 4116

Creating scalable solution without Futures?

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

Answers (1)

Gamlor
Gamlor

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:

  • 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()
    }
}

Upvotes: 1

Related Questions