Last active
February 10, 2017 14:08
-
-
Save l15k4/b10fc52ab1285440edec1e8fbc254847 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Router(routeeProps: Props, parallelism: Int) extends Actor with ActorLogging { | |
private def partitionCampaign(cid: String): Int = { | |
val rawMod = cid.hashCode % parallelism | |
rawMod + (if (rawMod < 0) parallelism else 0) | |
} | |
override def receive: Receive = initialized(ListMap.empty[Int, ActorRef]) | |
def initialized(routees: Map[Int, ActorRef]): Receive = { | |
case cacheCmd: CacheCmd => | |
val partition = partitionCampaign(cacheCmd.cid) | |
val (newRoutees, updated) = routees.updateIfMissing(partition)(context.actorOf(routeeProps)) | |
newRoutees(partition).tell(cacheCmd, sender()) | |
if (updated) | |
context.become(initialized(newRoutees)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment