actor_system.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. package actor
  2. import (
  3. "errors"
  4. "fmt"
  5. "github.com/gogf/gf/os/grpool"
  6. "sparrow/pkg/protocol"
  7. "sparrow/pkg/queue"
  8. "sparrow/pkg/server"
  9. "sync"
  10. )
  11. // SystemContext actor system context, with some func
  12. type SystemContext struct {
  13. ActorSystem System
  14. AppActor Ref
  15. ClusterService queue.ClusterService
  16. }
  17. func NewSystemContext(sys System) *SystemContext {
  18. return &SystemContext{
  19. ActorSystem: sys,
  20. }
  21. }
  22. func (s *SystemContext) tell(msg protocol.ActorMsg) {
  23. s.AppActor.Tell(msg)
  24. }
  25. func (s *SystemContext) tellWithHighPriority(msg protocol.ActorMsg) {
  26. s.AppActor.TellWithHighPriority(msg)
  27. }
  28. // System actor system interface
  29. type System interface {
  30. // 创建分发器
  31. CreateDispatcher(name string, dispatcher IDispatcher) error
  32. // 销毁分发器
  33. DestroyDispatcher(name string) error
  34. // 获取一个actor ref
  35. GetActor(actorId string) Ref
  36. // create root actor
  37. CreateRootActor(dispatcherName string, creator Creator) (Ref, error)
  38. // create child actor by parent actor
  39. CreateChildActor(dispatcherName string, creator Creator, parentId string) (Ref, error)
  40. // tell actor a message
  41. Tell(actorId string, msg protocol.ActorMsg) error
  42. // tell actor message with high priority
  43. TellWithHighPriority(actorId string, msg protocol.ActorMsg) error
  44. // stop actor by actor id
  45. StopActorById(actorId string) error
  46. // stop actor by actor ref
  47. StopActorByRef(ref Ref) error
  48. // broadcast message to children
  49. BroadcastToChildren(parentActorId string, msg protocol.ActorMsg) error
  50. }
  51. // DefaultActorSystem a default actor system implements System interface
  52. type DefaultActorSystem struct {
  53. dispatchers map[string]IDispatcher
  54. actors map[string]*MailBox
  55. parentActors map[string][]string
  56. scheduler *grpool.Pool
  57. config *DefaultActorSystemConfig
  58. mu sync.Mutex
  59. }
  60. // DefaultActorSystemConfig system config
  61. type DefaultActorSystemConfig struct {
  62. SchedulerPoolSize int // 系统调度执行池大小
  63. AppDispatcherPoolSize int // 应用调度执行池大小
  64. TenantDispatcherPoolSize int // 租户执行池大小
  65. RuleEngineDispatcherPoolSize int // 规则引擎执行池大小
  66. }
  67. func NewDefaultActorSystem(config *DefaultActorSystemConfig) *DefaultActorSystem {
  68. return &DefaultActorSystem{
  69. dispatchers: make(map[string]IDispatcher),
  70. parentActors: make(map[string][]string),
  71. scheduler: grpool.New(config.SchedulerPoolSize),
  72. config: config,
  73. actors: make(map[string]*MailBox),
  74. }
  75. }
  76. func (d *DefaultActorSystem) CreateDispatcher(name string, dispatcher IDispatcher) error {
  77. if _, ok := d.dispatchers[name]; ok {
  78. return errors.New(fmt.Sprintf("dispatcher name :%s is already registered!", name))
  79. }
  80. d.dispatchers[name] = dispatcher
  81. return nil
  82. }
  83. func (d *DefaultActorSystem) DestroyDispatcher(name string) error {
  84. if _, ok := d.dispatchers[name]; !ok {
  85. return errors.New(fmt.Sprintf("dispatcher %s is not registered!", name))
  86. }
  87. if err := d.dispatchers[name].Destroy(); err != nil {
  88. return err
  89. }
  90. delete(d.dispatchers, name)
  91. return nil
  92. }
  93. func (d *DefaultActorSystem) GetActor(actorId string) Ref {
  94. return d.actors[actorId]
  95. }
  96. func (d *DefaultActorSystem) CreateRootActor(dispatcherName string, creator Creator) (Ref, error) {
  97. return d.creator(dispatcherName, creator, "")
  98. }
  99. func (d *DefaultActorSystem) Tell(actorId string, msg protocol.ActorMsg) error {
  100. return d.tell(actorId, msg, false)
  101. }
  102. func (d *DefaultActorSystem) TellWithHighPriority(actorId string, msg protocol.ActorMsg) error {
  103. return d.tell(actorId, msg, true)
  104. }
  105. func (d *DefaultActorSystem) tell(actorId string, msg protocol.ActorMsg, isHighPriority bool) error {
  106. if mailBox, ok := d.actors[actorId]; ok {
  107. if isHighPriority {
  108. mailBox.TellWithHighPriority(msg)
  109. } else {
  110. mailBox.Tell(msg)
  111. }
  112. } else {
  113. return errors.New(fmt.Sprintf("actor with id %s is not registered!", actorId))
  114. }
  115. return nil
  116. }
  117. func (d *DefaultActorSystem) stop(actorId string) error {
  118. children := d.parentActors[actorId]
  119. if len(children) > 0 {
  120. for _, child := range children {
  121. _ = d.stop(child)
  122. }
  123. }
  124. if m, found := d.actors[actorId]; found {
  125. return m.destroy()
  126. }
  127. return nil
  128. }
  129. func (d *DefaultActorSystem) StopActorById(actorId string) error {
  130. return d.stop(actorId)
  131. }
  132. func (d *DefaultActorSystem) StopActorByRef(ref Ref) error {
  133. return d.stop(ref.GetActorId())
  134. }
  135. func (d *DefaultActorSystem) BroadcastToChildren(parentActorId string, msg protocol.ActorMsg) error {
  136. children := d.parentActors[parentActorId]
  137. for _, item := range children {
  138. if err := d.Tell(item, msg); err != nil {
  139. return err
  140. }
  141. }
  142. return nil
  143. }
  144. func (d *DefaultActorSystem) CreateChildActor(dispatcherName string, creator Creator, parentId string) (Ref, error) {
  145. return d.creator(dispatcherName, creator, parentId)
  146. }
  147. func (d *DefaultActorSystem) creator(name string, creator Creator, parentId string) (Ref, error) {
  148. d.mu.Lock()
  149. defer d.mu.Unlock()
  150. dispatcher := d.dispatchers[name]
  151. if dispatcher == nil {
  152. return nil, errors.New(fmt.Sprintf("dispatcher %s not found!", name))
  153. }
  154. actorId := creator.CreateActorId()
  155. var actorMailBox *MailBox
  156. actorMailBox = d.actors[actorId]
  157. if actorMailBox != nil {
  158. server.Log.Debugf("actor with id :%s is already registered!", actorId)
  159. } else {
  160. server.Log.Debugf("creating actor with id :%s", actorId)
  161. actor := creator.CreateActor()
  162. var parentActor Ref
  163. if parentId != "" {
  164. parentActor = d.GetActor(parentId)
  165. if parentActor == nil {
  166. return nil, errors.New(fmt.Sprintf("Parent actor with id :%s is not registered!", parentId))
  167. }
  168. }
  169. mailBox := NewMailBox(d, actorId, parentActor, actor, dispatcher)
  170. d.actors[actorId] = actorMailBox
  171. if err := mailBox.Init(); err != nil {
  172. server.Log.Error(err)
  173. return nil, err
  174. }
  175. actorMailBox = mailBox
  176. if parentActor != nil {
  177. d.parentActors[parentId] = append(d.parentActors[parentId], actorId)
  178. }
  179. }
  180. return actorMailBox, nil
  181. }