123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219 |
- package ruleEngine
- import (
- "errors"
- "fmt"
- "github.com/gogf/gf/os/grpool"
- "sparrow/pkg/protocol"
- "sparrow/pkg/server"
- "sync"
- )
- // SystemContext actor system context, with some func
- type SystemContext struct {
- ActorSystem System
- AppActor Ref
- ClusterService ClusterService
- RuleChainService RuleChainService
- TenantService TenantService
- }
- type SystemContextServiceConfig struct {
- ClusterService ClusterService
- RuleChainService RuleChainService
- TenantService TenantService
- }
- func NewSystemContext(sys System, config SystemContextServiceConfig) *SystemContext {
- if config.TenantService == nil || config.RuleChainService== nil || config.ClusterService== nil {
- panic("RuleEngine init error: services is not set")
- }
- return &SystemContext{
- ActorSystem: sys,
- ClusterService: config.ClusterService,
- RuleChainService: config.RuleChainService,
- TenantService: config.TenantService,
- }
- }
- func (s *SystemContext) Tell(msg protocol.ActorMsg) {
- s.AppActor.Tell(msg)
- }
- func (s *SystemContext) TellWithHighPriority(msg protocol.ActorMsg) {
- s.AppActor.TellWithHighPriority(msg)
- }
- // System actor system interface
- type System interface {
- // 创建分发器
- CreateDispatcher(name string, dispatcher IDispatcher) error
- // 销毁分发器
- DestroyDispatcher(name string) error
- // 获取一个actor ref
- GetActor(actorId string) Ref
- // create root actor
- CreateRootActor(dispatcherName string, creator Creator) (Ref, error)
- // create child actor by parent actor
- CreateChildActor(dispatcherName string, creator Creator, parentId string) (Ref, error)
- // tell actor a message
- Tell(actorId string, msg protocol.ActorMsg) error
- // tell actor message with high priority
- TellWithHighPriority(actorId string, msg protocol.ActorMsg) error
- // stop actor by actor id
- StopActorById(actorId string) error
- // stop actor by actor ref
- StopActorByRef(ref Ref) error
- // broadcast message to children
- BroadcastToChildren(parentActorId string, msg protocol.ActorMsg) error
- }
- // DefaultActorSystem a default actor system implements System interface
- type DefaultActorSystem struct {
- dispatchers map[string]IDispatcher
- actors map[string]*MailBox
- parentActors map[string][]string
- scheduler *grpool.Pool
- config *DefaultActorSystemConfig
- mu sync.Mutex
- }
- // DefaultActorSystemConfig system config
- type DefaultActorSystemConfig struct {
- SchedulerPoolSize int // 系统调度执行池大小
- AppDispatcherPoolSize int // 应用调度执行池大小
- TenantDispatcherPoolSize int // 租户执行池大小
- RuleEngineDispatcherPoolSize int // 规则引擎执行池大小
- }
- func NewDefaultActorSystem(config *DefaultActorSystemConfig) *DefaultActorSystem {
- return &DefaultActorSystem{
- dispatchers: make(map[string]IDispatcher),
- parentActors: make(map[string][]string),
- scheduler: grpool.New(config.SchedulerPoolSize),
- config: config,
- actors: make(map[string]*MailBox),
- }
- }
- func (d *DefaultActorSystem) CreateDispatcher(name string, dispatcher IDispatcher) error {
- if _, ok := d.dispatchers[name]; ok {
- return errors.New(fmt.Sprintf("dispatcher name :%s is already registered!", name))
- }
- d.dispatchers[name] = dispatcher
- return nil
- }
- func (d *DefaultActorSystem) DestroyDispatcher(name string) error {
- if _, ok := d.dispatchers[name]; !ok {
- return errors.New(fmt.Sprintf("dispatcher %s is not registered!", name))
- }
- if err := d.dispatchers[name].Destroy(); err != nil {
- return err
- }
- delete(d.dispatchers, name)
- return nil
- }
- func (d *DefaultActorSystem) GetActor(actorId string) Ref {
- if ref, ok := d.actors[actorId]; !ok {
- return nil
- } else {
- return ref
- }
- }
- func (d *DefaultActorSystem) CreateRootActor(dispatcherName string, creator Creator) (Ref, error) {
- return d.creator(dispatcherName, creator, "")
- }
- func (d *DefaultActorSystem) Tell(actorId string, msg protocol.ActorMsg) error {
- return d.tell(actorId, msg, false)
- }
- func (d *DefaultActorSystem) TellWithHighPriority(actorId string, msg protocol.ActorMsg) error {
- return d.tell(actorId, msg, true)
- }
- func (d *DefaultActorSystem) tell(actorId string, msg protocol.ActorMsg, isHighPriority bool) error {
- if mailBox, ok := d.actors[actorId]; ok {
- if isHighPriority {
- mailBox.TellWithHighPriority(msg)
- } else {
- mailBox.Tell(msg)
- }
- } else {
- return errors.New(fmt.Sprintf("actor with id %s is not registered!", actorId))
- }
- return nil
- }
- func (d *DefaultActorSystem) stop(actorId string) error {
- children := d.parentActors[actorId]
- if len(children) > 0 {
- for _, child := range children {
- _ = d.stop(child)
- }
- }
- if m, found := d.actors[actorId]; found {
- return m.destroy()
- }
- return nil
- }
- func (d *DefaultActorSystem) StopActorById(actorId string) error {
- return d.stop(actorId)
- }
- func (d *DefaultActorSystem) StopActorByRef(ref Ref) error {
- return d.stop(ref.GetActorId())
- }
- func (d *DefaultActorSystem) BroadcastToChildren(parentActorId string, msg protocol.ActorMsg) error {
- children := d.parentActors[parentActorId]
- for _, item := range children {
- if err := d.Tell(item, msg); err != nil {
- return err
- }
- }
- return nil
- }
- func (d *DefaultActorSystem) CreateChildActor(dispatcherName string, creator Creator, parentId string) (Ref, error) {
- return d.creator(dispatcherName, creator, parentId)
- }
- func (d *DefaultActorSystem) creator(name string, creator Creator, parentId string) (Ref, error) {
- d.mu.Lock()
- defer d.mu.Unlock()
- dispatcher := d.dispatchers[name]
- if dispatcher == nil {
- return nil, errors.New(fmt.Sprintf("dispatcher %s not found!", name))
- }
- actorId := creator.CreateActorId()
- var actorMailBox *MailBox
- actorMailBox = d.actors[actorId]
- if actorMailBox != nil {
- server.Log.Debugf("actor with id :%s is already registered!", actorId)
- } else {
- server.Log.Debugf("creating actor with id :%s", actorId)
- actor := creator.CreateActor()
- var parentActor Ref
- if parentId != "" {
- parentActor = d.GetActor(parentId)
- if parentActor == nil {
- return nil, errors.New(fmt.Sprintf("Parent actor with id :%s is not registered!", parentId))
- }
- }
- mailBox := NewMailBox(d, actorId, parentActor, actor, dispatcher)
- d.actors[actorId] = mailBox
- if err := mailBox.Init(); err != nil {
- server.Log.Error(err)
- return nil, err
- }
- actorMailBox = mailBox
- if parentActor != nil {
- d.parentActors[parentId] = append(d.parentActors[parentId], actorId)
- }
- }
- return actorMailBox, nil
- }
|