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 ?
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: