0

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))
}
tgk
  • 3,857
  • 2
  • 27
  • 42
  • You can have a look to the scala parallel collections – alifirat Apr 22 '16 at 08:38
  • Thanks for your comment. I'm not sure I can because each tasks is independent, mutating an audio file for example per request. They aren't aggregated -- but processed and each returned to their respective client. – tgk Apr 22 '16 at 15:41

1 Answers1

1

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()
    }
}
Gamlor
  • 12,978
  • 7
  • 43
  • 70
  • Thanks Gamlor, awesome notes, yeah exactly i'm limited by the framework so it's nice to hear that I'll have to manage the threads in some way and your example helps. – tgk Apr 25 '16 at 22:13