actor_system.go 5.9 KB

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