3

I'm new at the web develope and I have a small problem. I'm using scala akka with spray to make a website. My leader give me a question : I have a response like this

case HttpRequest(GET, Uri.Path("/ping"), _, _, _) => {
    Thread.sleep(10)
    sender ! HttpResponse(entity = "this is t2")
}

the Thread.sleep(10) is represent a heavy thread with constanst processing time is 10ms, so it make the whole program run slower 1000times, so how do I make it retrieve 10times more HTTPRequest ? The hint was using actors but somehow I read the whole documents but still missing something

  • Take a look at the Scheduler concept of Akka: http://doc.akka.io/docs/akka/snapshot/scala/scheduler.html, being a far better alternative to `Thread.sleep`. A scheduler will run in the background, so won't impact your default Thread pool handling web requests. – Mik378 Apr 28 '14 at 08:20
  • A webSocket associated to scheduler is then better for handling the Ping function. Here's an example (independent from Heroku): https://devcenter.heroku.com/articles/play-java-websockets – Mik378 Apr 28 '14 at 08:29
  • thanks for your support but I mean "the Thread.sleep(10) is represent a heavy thread with constanst processing time is 10ms" so how do you speed up the website in that problem ? – user3571374 Apr 28 '14 at 08:36
  • I don't figure out why you can't get rid of this `Thread.sleep`. If you want to execute your process each 10seconds, (even once), you should implement an Akka Scheduler – Mik378 Apr 28 '14 at 08:45
  • no no, you get it wrong, i mean the thread.sleep represent a function that take 10ms to process, and we need the result of that function, consider the function is optimized with constanst processing time of 10ms !! – user3571374 Apr 28 '14 at 08:51
  • ahhh ok, why don't specify some "heavyProcess()"instead, it would be far less confusing – Mik378 Apr 28 '14 at 09:03
  • sorry for that and thanks you, hope you come up with something new :D – user3571374 Apr 28 '14 at 09:32
  • @user3571374, you have to realize that you can't make this faster using only a single thread if part of the execution path always involves that constant 10ms of processing time. You could farm that work off to another actor (or even better a pool of other actors) to free up this request handler to handle more requests, but when you do that, those actors will more than likely run on other threads within the dispatcher (ExecutionContext). You can't make this faster as defined and use only a single thread. – cmbaxter Apr 28 '14 at 14:38
  • yes, that's what I thought in the first place !! so I try split it in two future and call the .onComplete and I think it already make the program run faster but it was wrong somehow btw, I'm doing a project about lightweight http if that was a hint thanks you so much for supporting me :D – user3571374 Apr 28 '14 at 17:44

1 Answers1

1

One way to do it:

case HttpRequest(GET, Uri.Path("/ping"), _, _, _) => {
    val replyTo = sender
    Future {
      someCPUIntensiveFunction()
      replyTo ! HttpResponse(entity = "this is t2")
    }
}

Basically, you move the "heavy function" call into a Future. That will make the current thread, which receives the HttpRequest, proceed as soon as it creates the future, and is able to process the next HttpRequest. The future will execute its code in a separate thread. So, you'll have two "paths" in your program. One, which will process HttpRequest, and another which will execute someCPUIntensiveFunction()

Haris Osmanagić
  • 1,249
  • 12
  • 28
  • thanks for your support:)) I did use Future but that's not the answer, he told that I was overthinking and I only have to use single thread only. I think I miss something with the actor so I try to read it all over again – user3571374 Apr 28 '14 at 09:26