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

     1  package router
     2  
     3  import (
     4  	"github.com/asynkron/protoactor-go/actor"
     5  )
     6  
     7  type broadcastGroupRouter struct {
     8  	GroupRouter
     9  }
    10  
    11  type broadcastPoolRouter struct {
    12  	PoolRouter
    13  }
    14  
    15  type broadcastRouterState struct {
    16  	routees *actor.PIDSet
    17  	sender  actor.SenderContext
    18  }
    19  
    20  func (state *broadcastRouterState) SetSender(sender actor.SenderContext) {
    21  	state.sender = sender
    22  }
    23  
    24  func (state *broadcastRouterState) SetRoutees(routees *actor.PIDSet) {
    25  	state.routees = routees
    26  }
    27  
    28  func (state *broadcastRouterState) GetRoutees() *actor.PIDSet {
    29  	return state.routees
    30  }
    31  
    32  func (state *broadcastRouterState) RouteMessage(message interface{}) {
    33  	state.routees.ForEach(func(i int, pid *actor.PID) {
    34  		state.sender.Send(pid, message)
    35  	})
    36  }
    37  
    38  func NewBroadcastPool(size int, opts ...actor.PropsOption) *actor.Props {
    39  	return (&actor.Props{}).
    40  		Configure(actor.WithSpawnFunc(spawner(&broadcastPoolRouter{PoolRouter{PoolSize: size}}))).
    41  		Configure(opts...)
    42  }
    43  
    44  func NewBroadcastGroup(routees ...*actor.PID) *actor.Props {
    45  	return (&actor.Props{}).Configure(actor.WithSpawnFunc(spawner(&broadcastGroupRouter{GroupRouter{Routees: actor.NewPIDSet(routees...)}})))
    46  }
    47  
    48  func (config *broadcastPoolRouter) CreateRouterState() State {
    49  	return &broadcastRouterState{}
    50  }
    51  
    52  func (config *broadcastGroupRouter) CreateRouterState() State {
    53  	return &broadcastRouterState{}
    54  }