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

     1  package actor
     2  
     3  import (
     4  	"log/slog"
     5  	"net"
     6  	"strconv"
     7  
     8  	"github.com/asynkron/protoactor-go/eventstream"
     9  	"github.com/asynkron/protoactor-go/extensions"
    10  	"github.com/lithammer/shortuuid/v4"
    11  )
    12  
    13  //goland:noinspection GoNameStartsWithPackageName
    14  type ActorSystem struct {
    15  	ProcessRegistry *ProcessRegistryValue
    16  	Root            *RootContext
    17  	EventStream     *eventstream.EventStream
    18  	Guardians       *guardiansValue
    19  	DeadLetter      *deadLetterProcess
    20  	Extensions      *extensions.Extensions
    21  	Config          *Config
    22  	ID              string
    23  	stopper         chan struct{}
    24  	logger          *slog.Logger
    25  }
    26  
    27  func (as *ActorSystem) Logger() *slog.Logger {
    28  	return as.logger
    29  }
    30  
    31  func (as *ActorSystem) NewLocalPID(id string) *PID {
    32  	return NewPID(as.ProcessRegistry.Address, id)
    33  }
    34  
    35  func (as *ActorSystem) Address() string {
    36  	return as.ProcessRegistry.Address
    37  }
    38  
    39  func (as *ActorSystem) GetHostPort() (host string, port int, err error) {
    40  	addr := as.ProcessRegistry.Address
    41  	if h, p, e := net.SplitHostPort(addr); e != nil {
    42  		if addr != localAddress {
    43  			err = e
    44  		}
    45  
    46  		host = localAddress
    47  		port = -1
    48  	} else {
    49  		host = h
    50  		port, err = strconv.Atoi(p)
    51  	}
    52  
    53  	return
    54  }
    55  
    56  func (as *ActorSystem) Shutdown() {
    57  	close(as.stopper)
    58  }
    59  
    60  func (as *ActorSystem) IsStopped() bool {
    61  	select {
    62  	case <-as.stopper:
    63  		return true
    64  	default:
    65  		return false
    66  	}
    67  }
    68  
    69  func NewActorSystem(options ...ConfigOption) *ActorSystem {
    70  	config := Configure(options...)
    71  
    72  	return NewActorSystemWithConfig(config)
    73  }
    74  
    75  func NewActorSystemWithConfig(config *Config) *ActorSystem {
    76  	system := &ActorSystem{}
    77  	system.ID = shortuuid.New()
    78  	system.Config = config
    79  	system.logger = config.LoggerFactory(system)
    80  	system.ProcessRegistry = NewProcessRegistry(system)
    81  	system.Root = NewRootContext(system, EmptyMessageHeader)
    82  	system.Guardians = NewGuardians(system)
    83  	system.EventStream = eventstream.NewEventStream()
    84  	system.DeadLetter = NewDeadLetter(system)
    85  	system.Extensions = extensions.NewExtensions()
    86  	SubscribeSupervision(system)
    87  	system.Extensions.Register(NewMetrics(system, config.MetricsProvider))
    88  
    89  	system.ProcessRegistry.Add(NewEventStreamProcess(system), "eventstream")
    90  	system.stopper = make(chan struct{})
    91  
    92  	system.Logger().Info("actor system started", slog.String("id", system.ID))
    93  
    94  	return system
    95  }