TwitterServer (Finch/Finagle) seems to block api call till it finishes its calculation

292 Views Asked by At

I'm setting up a new rest server using TwitterServer, and it seems to block a new api call, till a previous one be finished.

Here is a simple modification of the basic code taken from the documentation at https://twitter.github.io/twitter-server :

import com.twitter.finagle.{Http, Service}
import com.twitter.finagle.http.{Request, Response, Status}
import com.twitter.server.TwitterServer
import com.twitter.util.{Await, Future, FuturePool}

object BasicServer extends TwitterServer {
  val service = new Service[Request, Response] {
    def apply(request: Request): Future[Response] = {
      FuturePool.unboundedPool {
        Thread.sleep(10000)

        val response = Response(request.version, Status.Ok)
        response.contentString = "hello"

        response
      }
    }
  }

  def main(): Unit = {
    val server = Http.serve(":8888", service)
    onExit {
      server.close()
      ()
    }
    Await.ready(server)

    ()
  }
}

if i try to do multiple calls to http://localhost:8888, the first call blocks the second one, for some reason. Any idea why is this happening ?

1

There are 1 best solutions below

0
Carlos Saltos On

You should NOT use Thread.sleep with Finagle (nor with any other non-blocking solution) ... most of those solutions count with the fact that the threads are not blocked long.

Thread.sleep is using any local thread to sleep on it and that will block the poor server without further notice.

As a soluion for a sleep, please use the Future.sleep like in:

import com.twitter.conversions.DurationOps._ // for the 5.seconds part to work
import com.twitter.util._

implicit val twitterTimer = new ScheduledThreadPoolTimer // for the sleep time to work

Future.sleep(5.seconds) flatMap FuturePool.unboundedPool { ...(and remove the Thread.sleep inside) ... }