Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Akka context become recursive function

Tags:

state

akka

I have an actor in which I mutate the state using context.become: Here is the snippet:

def stateMachine(state: State): Receive = {
  case a => {
    ... do something
    context.become(stateMachine(newState))
  }

  case b => {
    ... do something
    sender ! state
  }

  case c => {
    ... do something
    context.become(stateMachine(newState))
  }
}

My IntelliJ says that my stateMachine(...) function is recursive. Is this a problem? Should I be concerned? Is there something fundamentally wrong with my approach in the above example?

like image 808
joesan Avatar asked Sep 27 '22 23:09

joesan


1 Answers

The approach you are using is fine, it is a common way to implement state inside an Actor without using var. The default version of context.become does not maintain a stack, it just replaces the existing functionality with the new one. The is called "HotSwap". To maintain a stack, you would have to add discardOld = false.

http://doc.akka.io/docs/akka/snapshot/scala/actors.html#become-unbecome

http://doc.akka.io/docs/akka/1.3.1/scala/actors.html#actor-hotswap

like image 76
mattinbits Avatar answered Oct 06 '22 01:10

mattinbits