Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Akka avoiding wrapping future when responding to non-Actor code

Tags:

scala

akka

future

I'm making a small caching actor with Akka 2 and to make the actor not block I perform all calculations inside futures. However a problem is that this actor also need to interact with with code that is not itself in an actor, so that I need to use the "ask" pattern to get a value.

My question is, how do I avoid wrapping the Future of my calculation inside another Future when using the ask pattern?

For example

val f = myCache ? GetOrCalc("myKey", myCalculation) // this will be a Future[Future[...]] but I would like a Future[...]

// meanwhile, inside the actor
def receive = {
    case GetOrCalc(key, calculation) =>
        if (keyNotExists) sender ! Future { calculation() } // calculation() is long-running
        else sender ! cacheMap(key)
}

Ideally I could use the Future.pipeTo function but I'm afraid this doesn't get counted as a "response" for non-actor code

like image 864
Aktau Avatar asked Sep 17 '12 08:09

Aktau


2 Answers

This is the solution:

val f = myCache ? GetOrCalc("myKey", myCalculation)

def receive = {
    case GetOrCalc(key, calculation) =>
        if (keyNotExists) Future { calculation() } pipeTo sender
        else sender ! cacheMap(key)
}

Send-And-Receive-Future">http://doc.akka.io/docs/akka/2.0.3/scala/actors.html#Ask_Send-And-Receive-Future

like image 104
Viktor Klang Avatar answered Sep 21 '22 22:09

Viktor Klang


Add onComplete to the calculation future.

def receive = {
    case GetOrCalc(key, calculation) =>
        if (keyNotExists) // calculation() is long-running
            Future { calculation() } onComplete {
                case Right(result) => result match {
                        case Some(value) => sender ! value
                        case None => sender ! Status.Failure(new Exception("Cannot find the value"))
                    }
                case Left(ex) =>
                    sender ! Status.Failure(ex)

            }
        else sender ! cacheMap(key)
}

And there is an article about using Akka to build a cache system. http://letitcrash.com/post/30509298968/case-study-an-auto-updating-cache-using-actors

like image 41
Evans Y. Avatar answered Sep 19 '22 22:09

Evans Y.