Effect of returning a supervised receive behavior from within a supervised receive behavior

I’d like to cross-post a question that I originally posted on Akka’s user forum.

In Akka typed: What is the motivation for allowing a supervised receive behavior to return a new, supervised receive behavior?

Suppose we have a Receive behavior R1 decorated by a supervisor S1. As part of message processing, R1 returns a new Receive behavior R2, itself decorated by a supervisor S2.

Akka allows for this. Why? I couldn’t find a similar example on the Internet. It is unusual: typically, a supervisor decorates a behavior once. Therefore, I am trying to come up with some motivating examples, and I am looking for validation.

Scenario 1:

  • R1 can throw Exception1 (and Exception1 only) as part of message processing.
    S1 decorates R1 handling Exception1.
  • R1 returns R2. Suppose that R2 can throw Exception2 as part of message processing logic. S1 is still there, but it only handles Exception1, not Exception2. Therefore, we must decorate R2 with S2 handling Exception2.

Does this make sense? If yes, why not make S1 handle both Exception1 and Exception2 directly, by nesting calls to onFailure?

Scenario 2:

  • R1 can throw both Exception1 and Exception2 as part of message processing. S1 handles both failures, by nesting calls to onFailure, with restart strategy.
  • R1 returns R2. Suppose we want to override S1’s supervision strategy for Exception2: resume instead of restart. To do so, we decorate R2 with S2 handling Exception2 with resume strategy.

As discussed in Akka’s user forum post, supervisors are evaluated in order inner to outer. Therefore, S2’s supervision strategy for Exception2 will override S1’s.

Questions

Am I on the right track? Do such scenarios ever arise in real life?

Leave a Comment