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

     1  package router
     2  
     3  import (
     4  	"sync"
     5  
     6  	"github.com/asynkron/protoactor-go/actor"
     7  )
     8  
     9  type groupRouterActor struct {
    10  	props  *actor.Props
    11  	config RouterConfig
    12  	state  State
    13  	wg     *sync.WaitGroup
    14  }
    15  
    16  func (a *groupRouterActor) Receive(context actor.Context) {
    17  	switch m := context.Message().(type) {
    18  	case *actor.Started:
    19  		a.config.OnStarted(context, a.props, a.state)
    20  		a.wg.Done()
    21  
    22  	case *AddRoutee:
    23  		r := a.state.GetRoutees()
    24  		if r.Contains(m.PID) {
    25  			return
    26  		}
    27  		context.Watch(m.PID)
    28  		r.Add(m.PID)
    29  		a.state.SetRoutees(r)
    30  
    31  	case *RemoveRoutee:
    32  		r := a.state.GetRoutees()
    33  		if !r.Contains(m.PID) {
    34  			return
    35  		}
    36  
    37  		context.Unwatch(m.PID)
    38  		r.Remove(m.PID)
    39  		a.state.SetRoutees(r)
    40  
    41  	case *BroadcastMessage:
    42  		msg := m.Message
    43  		sender := context.Sender()
    44  		a.state.GetRoutees().ForEach(func(i int, pid *actor.PID) {
    45  			context.RequestWithCustomSender(pid, msg, sender)
    46  		})
    47  
    48  	case *GetRoutees:
    49  		r := a.state.GetRoutees()
    50  		routees := make([]*actor.PID, r.Len())
    51  		r.ForEach(func(i int, pid *actor.PID) {
    52  			routees[i] = pid
    53  		})
    54  
    55  		context.Respond(&Routees{PIDs: routees})
    56  	}
    57  }