github.com/aergoio/aergo@v1.3.1/chain/subcomponent.go (about) 1 package chain 2 3 import ( 4 "fmt" 5 "github.com/aergoio/aergo-actor/actor" 6 "github.com/aergoio/aergo-actor/router" 7 "github.com/aergoio/aergo/pkg/component" 8 ) 9 10 // SubComponent handles message with Receive(), and requests to other actor services with IComponentRequester 11 // To use SubComponent, only need to implement Actor interface 12 type SubComponent struct { 13 actor.Actor 14 component.IComponentRequester // use basecomponent to request to other actors 15 16 name string 17 pid *actor.PID 18 count int 19 } 20 21 const ( 22 defaultChainWorkerCount = 5 23 ) 24 25 func NewSubComponent(subactor actor.Actor, requester *component.BaseComponent, name string, cntWorker int) *SubComponent { 26 return &SubComponent{ 27 Actor: subactor, 28 IComponentRequester: requester, 29 name: name, 30 count: cntWorker} 31 } 32 33 // spawn new subactor 34 func (sub *SubComponent) Start() { 35 sub.pid = actor.Spawn(router.NewRoundRobinPool(sub.count).WithInstance(sub.Actor)) 36 37 msg := fmt.Sprintf("%s[%d] started", sub.name, sub.count) 38 logger.Info().Msg(msg) 39 } 40 41 // stop subactor 42 func (sub *SubComponent) Stop() { 43 sub.pid.GracefulStop() 44 msg := fmt.Sprintf("%s stoped", sub.name) 45 logger.Info().Msg(msg) 46 } 47 48 //send message to this subcomponent and reply to actor with pid respondTo 49 func (sub *SubComponent) Request(message interface{}, respondTo *actor.PID) { 50 sub.pid.Request(message, respondTo) 51 }