actor_system.go 6.3 KB

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