I am writing an application on the play 2.0 framework. The purpose of this app is to handle image uploads and resize them in the background.
The upload is handled via a standard post handler that saves the file to the filesystem. Right now this is just done on the event or IO thread. Once the file is successfully saved, an AKKA actor is sent a message to resize the original image to a number of different sizes (currently 4). These resize operations are CPU demanding and take a bit of time.
This all works well, until I test the app under some load. Under load, the resize operations are effectively consuming all of the available CPU time and so the incoming HTTP requests are somewhat starved for CPU time and we see a backlog of these requests to the point that eventually the requests start timing out.
So the question is, how do I configure the system (or re-write it) to give the incoming HTTP requests a greater priority than the resize requests?
You should investigate dispatcher in Akka. Basically they abstract thread/worker pool that each actor uses for processing. You can define one global dispatcher or have several ones.
Define dispatcher having only limited number of threads (1 or 2):
resizing-thread-pool-dispatcher {
type = Dispatcher
executor = "thread-pool-executor"
thread-pool-executor {
core-pool-size-max = 1
}
}
and associate it with your resizing actor:
context.
actorOf(
Props[ResizingActor].withDispatcher("resizing-thread-pool-dispatcher"),
"resizingActor"
)
Of course the idea is that your resizing-thread-pool-dispatcher
uses less thread that available cores, so your HTTP threads are still responsive.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With