github.com/asynkron/protoactor-go@v0.0.0-20240308120642-ef91a6abee75/actor/supervision.go (about)

     1  package actor
     2  
     3  import (
     4  	"time"
     5  )
     6  
     7  // DeciderFunc is a function which is called by a SupervisorStrategy
     8  type DeciderFunc func(reason interface{}) Directive
     9  
    10  // SupervisorStrategy is an interface that decides how to handle failing child actors
    11  type SupervisorStrategy interface {
    12  	HandleFailure(actorSystem *ActorSystem, supervisor Supervisor, child *PID, rs *RestartStatistics, reason interface{}, message interface{})
    13  }
    14  
    15  // Supervisor is an interface that is used by the SupervisorStrategy to manage child actor lifecycle
    16  type Supervisor interface {
    17  	Children() []*PID
    18  	EscalateFailure(reason interface{}, message interface{})
    19  	RestartChildren(pids ...*PID)
    20  	StopChildren(pids ...*PID)
    21  	ResumeChildren(pids ...*PID)
    22  }
    23  
    24  func logFailure(actorSystem *ActorSystem, child *PID, reason interface{}, directive Directive) {
    25  	actorSystem.EventStream.Publish(&SupervisorEvent{
    26  		Child:     child,
    27  		Reason:    reason,
    28  		Directive: directive,
    29  	})
    30  }
    31  
    32  // DefaultDecider is a decider that will always restart the failing child actor
    33  func DefaultDecider(_ interface{}) Directive {
    34  	return RestartDirective
    35  }
    36  
    37  var (
    38  	defaultSupervisionStrategy    = NewOneForOneStrategy(10, 10*time.Second, DefaultDecider)
    39  	restartingSupervisionStrategy = NewRestartingStrategy()
    40  )
    41  
    42  func DefaultSupervisorStrategy() SupervisorStrategy {
    43  	return defaultSupervisionStrategy
    44  }
    45  
    46  func RestartingSupervisorStrategy() SupervisorStrategy {
    47  	return restartingSupervisionStrategy
    48  }