|
@@ -1,12 +1,18 @@
|
|
package ruleEngine
|
|
package ruleEngine
|
|
|
|
|
|
import (
|
|
import (
|
|
|
|
+ "encoding/json"
|
|
"errors"
|
|
"errors"
|
|
"fmt"
|
|
"fmt"
|
|
"github.com/gogf/gf/os/grpool"
|
|
"github.com/gogf/gf/os/grpool"
|
|
|
|
+ "github.com/gogf/gf/util/guid"
|
|
|
|
+ "golang.org/x/time/rate"
|
|
|
|
+ "sparrow/pkg/entities"
|
|
|
|
+ "sparrow/pkg/models"
|
|
"sparrow/pkg/protocol"
|
|
"sparrow/pkg/protocol"
|
|
"sparrow/pkg/server"
|
|
"sparrow/pkg/server"
|
|
"sync"
|
|
"sync"
|
|
|
|
+ "time"
|
|
)
|
|
)
|
|
|
|
|
|
// SystemContext actor system context, with some func
|
|
// SystemContext actor system context, with some func
|
|
@@ -16,26 +22,84 @@ type SystemContext struct {
|
|
ClusterService ClusterService
|
|
ClusterService ClusterService
|
|
RuleChainService RuleChainService
|
|
RuleChainService RuleChainService
|
|
TenantService TenantService
|
|
TenantService TenantService
|
|
|
|
+ EventService EventService
|
|
|
|
+ // 调试信息的限流器
|
|
|
|
+ debugPerTenantLimits map[string]*rate.Limiter
|
|
}
|
|
}
|
|
|
|
|
|
type SystemContextServiceConfig struct {
|
|
type SystemContextServiceConfig struct {
|
|
ClusterService ClusterService
|
|
ClusterService ClusterService
|
|
RuleChainService RuleChainService
|
|
RuleChainService RuleChainService
|
|
TenantService TenantService
|
|
TenantService TenantService
|
|
|
|
+ EventService EventService
|
|
}
|
|
}
|
|
|
|
|
|
func NewSystemContext(sys System, config SystemContextServiceConfig) *SystemContext {
|
|
func NewSystemContext(sys System, config SystemContextServiceConfig) *SystemContext {
|
|
- if config.TenantService == nil || config.RuleChainService== nil || config.ClusterService== nil {
|
|
|
|
|
|
+ if config.TenantService == nil || config.RuleChainService == nil || config.ClusterService == nil ||
|
|
|
|
+ config.EventService == nil {
|
|
panic("RuleEngine init error: services is not set")
|
|
panic("RuleEngine init error: services is not set")
|
|
}
|
|
}
|
|
return &SystemContext{
|
|
return &SystemContext{
|
|
- ActorSystem: sys,
|
|
|
|
- ClusterService: config.ClusterService,
|
|
|
|
- RuleChainService: config.RuleChainService,
|
|
|
|
- TenantService: config.TenantService,
|
|
|
|
|
|
+ ActorSystem: sys,
|
|
|
|
+ ClusterService: config.ClusterService,
|
|
|
|
+ RuleChainService: config.RuleChainService,
|
|
|
|
+ TenantService: config.TenantService,
|
|
|
|
+ debugPerTenantLimits: make(map[string]*rate.Limiter),
|
|
|
|
+ EventService: config.EventService,
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+// PersistDebugInput 保存输入调试信息
|
|
|
|
+func (s *SystemContext) PersistDebugInput(tenantId string, entityId entities.EntityId, msg *protocol.Message, relationType string, err error) error {
|
|
|
|
+ return s.persistDebugAsync(tenantId, entityId, "IN", msg, relationType, err)
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+// PersistDebugOutput 保存输出调试信息
|
|
|
|
+func (s *SystemContext) PersistDebugOutput(tenantId string, entityId entities.EntityId, msg *protocol.Message, relationType string, err error) error {
|
|
|
|
+ return s.persistDebugAsync(tenantId, entityId, "OUT", msg, relationType, err)
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func (s *SystemContext) persistDebugAsync(tenantId string, id entities.EntityId, eType string, msg *protocol.Message, rType string, errInfo error) error {
|
|
|
|
+ var limiter *rate.Limiter
|
|
|
|
+ if v, ok := s.debugPerTenantLimits[tenantId]; !ok {
|
|
|
|
+ limiter = rate.NewLimiter(10, 200)
|
|
|
|
+ s.debugPerTenantLimits[tenantId] = limiter
|
|
|
|
+ } else {
|
|
|
|
+ limiter = v
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if limiter.AllowN(time.Now(), 200) {
|
|
|
|
+ var errStr string
|
|
|
|
+ if v, ok := msg.MetaData["error"]; ok {
|
|
|
|
+ errStr = v.(string)
|
|
|
|
+ }
|
|
|
|
+ if errInfo != nil {
|
|
|
|
+ errStr = errInfo.Error()
|
|
|
|
+ }
|
|
|
|
+ buf, err := json.Marshal(msg.MetaData)
|
|
|
|
+ if err != nil {
|
|
|
|
+ server.Log.WithField("method", "persistDebugAsync").Error(err)
|
|
|
|
+ }
|
|
|
|
+ if s.EventService != nil {
|
|
|
|
+ if err := s.EventService.SaveAsync(&models.Event{
|
|
|
|
+ RecordId: guid.S(),
|
|
|
|
+ ServerId: server.InternalIP,
|
|
|
|
+ EventType: eType,
|
|
|
|
+ EntityType: id.GetEntityType().String(),
|
|
|
|
+ EntityId: id.GetId(),
|
|
|
|
+ MessageId: msg.Id,
|
|
|
|
+ RelationType: rType,
|
|
|
|
+ Data: msg.Data,
|
|
|
|
+ MetaData: string(buf),
|
|
|
|
+ Error: errStr,
|
|
|
|
+ }); err != nil {
|
|
|
|
+ return err
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return nil
|
|
|
|
+}
|
|
|
|
+
|
|
func (s *SystemContext) Tell(msg protocol.ActorMsg) {
|
|
func (s *SystemContext) Tell(msg protocol.ActorMsg) {
|
|
s.AppActor.Tell(msg)
|
|
s.AppActor.Tell(msg)
|
|
}
|
|
}
|