Search code examples
kotlinakkaakka-supervision

Can a BackoffSupervisor have multiple children actors?


All my actors inherit from BaseActor and can create children actor with registerActor()

abstract class BaseActor() : AbstractLoggingActor() {

    protected fun registerActor(childProps: Props, name: String): ActorRef {
        val child = context.child(name)
        val supervisorProps = BackoffSupervisor.props(
            BackoffOpts.onFailure(
                childProps,
                name,
                java.time.Duration.ofSeconds(1),
                java.time.Duration.ofSeconds(30),
                0.2 // adds 20% "noise" to vary the intervals slightly
            ).withAutoReset(FiniteDuration(20, TimeUnit.SECONDS))
        )

        return if (child.isEmpty) {
            context.actorOf(supervisorProps, "supervisor_$name").also { addChildRoutee(it) }
        } else {
            child.get()
        }
    }
}

When an actor /user/dad create 2 children actors using registerActor() however 2 supervisors are created

/user/dad/supervisor_foo/foo
/user/dad/supervisor_bar/bar

How can I reuse the same supervisor to supervise both foo and bar ?

/user/dad/supervisor/foo
/user/dad/supervisor/bar

Solution

  • BackoffSupervisors only support a single (direct) child.

    If you want to re-use a BackoffSupervisor the only way is by introducing another supervisor as its child:

    /user/dad/backoffsupervisor/supervisor/foo
    /user/dad/backoffsupervisor/supervisor/bar
    

    Where /user/dad/backoffsupervisor is a BackoffSupervisor and where /user/dad/backoffsupervisor/supervisor is an actor that watches its children (foo and bar) and stops when either child stops, cascading the failure to the backoff supervisor.