asynchronous - How to wait for free Akka actor while processing stream of data using Plays Iteratee -
i have infinite stream messages represented plays enumerator
apply iteratee
. each message processed akka actor (number of actors limited 10).
now code in iteratee
asynchronously wait free actor if 10 actors busy , not send them messages leads exception ask timed out on ...
.
how can achieve such functionality? there better way process infinite stream 10 actors without await
?
example of code talking this:
val workers = context.actorof(props[myworker].withrouter(roundrobinrouter(10))) val = iteratee.foreach[msg] { msg => workers ? msg } msgenumerator.apply(it)
use iteratee.foldm
actor ask pattern have here seems right approach. assuming don't want actors build large mailboxes (if don't care large mailboxes, use tell
, iteratee.foreach
instead of ask
) require specialized routing logic. since api making custom akka router doesn't support asynchrony, need custom actor handle logic of distributing 1 piece of work each actor in actor pool @ time.
i imagine working like:
class workdistributor extends actor { final val num_workers = 10 val workers = context.actorof(props[myworker].withrouter(roundrobinrouter(num_workers))) var numactiveworkers = 0 var queuedwork: option[work] = none def receive = { case iterateework(work) if numactiveworkers < num_workers => workers ! work; numactiveworkers += 1; sender ! sendmemorework case iterateework(work) => queuedwork = some(work) case actorfinishedwork if queuedwork.isdefined => queuedwork.foreach(workers ! _); queuedwork = none case actorfinishedwork => numactiveworkers -= 1; sender ! sendmemorework } }
where iterateework
message sent iteratee , actorfinishedwork
message sent actors in actor pool.
looking @ thing wrote, should rewritten use become
change behavior when actor pool full (rather if
filters on each case leave exercise reader.
then iteratee
iteratee.foldm[work, sendmemorework.type](sendmemorework) { case (_, work) => workdistributor ? iterateework(work) }
Comments
Post a Comment