1
votes

All started with question "Scala, Actors, what happens to unread inbox messages?". I was thinking how to avoid such problems in large system with many actors.

I found myself writing something like this:

react {
  //all cases
  case any: AnyRef => logMessageWithoutCase(any)
}

Is it good avoidance from memory leaks or is it have some side effects?

UPDATE 1 Thanks to @Alexey Romanov and @Luigi Plinge, if in the system will have some Spam actor?
Something like this:

react{
  //all cases
  case msg: Any => Spam!msg
}

And finally in Spam will log or save to database. I think, it is more intuitive solution.

2
I'd suggest removing the AnyRef type restriction, or changing it to Any, so you capture all the AnyVal types as wellLuigi Plinge

2 Answers

2
votes

You might also investigate using Akka actors, which do not suffer from this problem because they enforce in-sequence message processing. Here, unhandled messages get passed to the unhandled() callback, which by default logs and throws an exception.

Another thing to consider is that Akka actors will replace the current scala.actor package in the medium term. This will be especially beneficial for large systems with many actors, because the current Scala actors are not as light-weight as Akka actors.

2
votes

Logging messages is a side effect :) Since logging likely needs to write to disk or database, if you have many unmatched messages, this may degrade performance. Otherwise yes, this is a good way to avoid memory leaks.