Reputation: 119
We are building REST using spray and akka. For this we need to read more than 10k files from disk (Mostly static, updates might come twice per day). Reading from disk for each request is giving performance hit, we put all required information in DataMap (Map object). Using akka scheduler updating DataMap for each 15min(Needs to be up-to date with disk data).
class SampleScheduler extends Actor with ActorLogging {
import context._
val tick = context.system.scheduler.schedule(1.second,15.minute, self,"mytick")
override def postStop() = tick.cancel()
override def receive: Receive = {
case "mytick" => {
println(s"Yes got the tick now ${new Date().toGMTString}")
Test.setDataMap()
}
}
}
object Test {
var DataMap:Map[String,List[String]]=Map()
def setDataMap()={
DataMap = //Read files from disk
}
}
object Main extends App {
//For each new request look into DataMap
if(Test.DataMap.isEmpty) {
//How to handle this, can i use like this
Thread.sleep(1000)
}
}
So when the new request comes, it searches required data from map and get information, process accordingly.
How to achieve below requirements with above said design.
Upvotes: 0
Views: 73
Reputation: 1019
First and most important of all, you must avoid (3). Storing state outside of actor is evil! Besides, storing state and maintaining it is what actors are good at.
And after you put state inside of actor and share it via messages, (2) will be obsolete. The request actor will ask state actor and if state actor is busy with re-reading files, it will answer after its job done.
Lastly, there are 2 different strategies you can follow to solve (1). State actor will process each messages in order so it can (a) reply message with last know state or (b) hold messages and reply with newly populated state if it thinks it should re-read files.
Upvotes: 0