mirror of
https://github.com/duanhf2012/origin.git
synced 2026-02-19 10:54:46 +08:00
新增蓝图热更新功能
This commit is contained in:
@@ -10,7 +10,7 @@ const (
|
|||||||
Sys_Event_Tcp EventType = -3
|
Sys_Event_Tcp EventType = -3
|
||||||
Sys_Event_Http_Event EventType = -4
|
Sys_Event_Http_Event EventType = -4
|
||||||
Sys_Event_WebSocket EventType = -5
|
Sys_Event_WebSocket EventType = -5
|
||||||
Sys_Event_Kcp EventType = -6
|
Sys_Event_Kcp EventType = -6
|
||||||
Sys_Event_Node_Conn_Event EventType = -7
|
Sys_Event_Node_Conn_Event EventType = -7
|
||||||
Sys_Event_Nats_Conn_Event EventType = -8
|
Sys_Event_Nats_Conn_Event EventType = -8
|
||||||
Sys_Event_DiscoverService EventType = -9
|
Sys_Event_DiscoverService EventType = -9
|
||||||
@@ -18,6 +18,6 @@ const (
|
|||||||
Sys_Event_EtcdDiscovery EventType = -11
|
Sys_Event_EtcdDiscovery EventType = -11
|
||||||
Sys_Event_Gin_Event EventType = -12
|
Sys_Event_Gin_Event EventType = -12
|
||||||
Sys_Event_FrameTick EventType = -13
|
Sys_Event_FrameTick EventType = -13
|
||||||
|
Sys_Event_ReloadBlueprint EventType = -14
|
||||||
Sys_Event_User_Define EventType = 1
|
Sys_Event_User_Define EventType = 1
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -3,84 +3,153 @@ package blueprint
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
|
"github.com/duanhf2012/origin/v2/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Blueprint struct {
|
type Blueprint struct {
|
||||||
execNodes []IExecNode // 注册的定义执行结点
|
execNodes []IExecNode // 注册的定义执行结点
|
||||||
|
execNodeList []func() IExecNode
|
||||||
execPool ExecPool
|
execPool *ExecPool
|
||||||
graphPool GraphPool
|
graphPool *GraphPool
|
||||||
|
|
||||||
blueprintModule IBlueprintModule
|
blueprintModule IBlueprintModule
|
||||||
mapGraph map[int64]IGraph
|
mapGraph map[int64]IGraph
|
||||||
seedID int64
|
seedID int64
|
||||||
cancelTimer func(*uint64) bool
|
cancelTimer func(*uint64) bool
|
||||||
|
|
||||||
|
execDefFilePath string // 执行结点定义文件路径
|
||||||
|
graphFilePath string // 蓝图文件路径
|
||||||
}
|
}
|
||||||
|
|
||||||
func (bm *Blueprint) RegExecNode(execNode IExecNode) {
|
func (bm *Blueprint) RegisterExecNode(execNodeFunc func() IExecNode) {
|
||||||
bm.execNodes = append(bm.execNodes, execNode)
|
bm.execNodeList = append(bm.execNodeList, execNodeFunc)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (bm *Blueprint) regSysNode() {
|
type IExecNodeType[T any] interface {
|
||||||
bm.RegExecNode(&AddInt{})
|
*T
|
||||||
bm.RegExecNode(&SubInt{})
|
IExecNode
|
||||||
bm.RegExecNode(&MulInt{})
|
}
|
||||||
bm.RegExecNode(&DivInt{})
|
|
||||||
bm.RegExecNode(&ModInt{})
|
|
||||||
bm.RegExecNode(&RandNumber{})
|
|
||||||
|
|
||||||
bm.RegExecNode(&Entrance_ArrayParam{})
|
// 生成一个泛型函数,返回func() IExecNode类型
|
||||||
bm.RegExecNode(&Entrance_IntParam{})
|
func NewExecNode[T any, P IExecNodeType[T]]() func() IExecNode {
|
||||||
bm.RegExecNode(&Entrance_Timer{})
|
return func() IExecNode {
|
||||||
bm.RegExecNode(&DebugOutput{})
|
var t T
|
||||||
bm.RegExecNode(&Sequence{})
|
return P(&t)
|
||||||
bm.RegExecNode(&Foreach{})
|
}
|
||||||
bm.RegExecNode(&ForeachIntArray{})
|
}
|
||||||
|
|
||||||
bm.RegExecNode(&GetArrayInt{})
|
func (bm *Blueprint) regSysNodes() {
|
||||||
bm.RegExecNode(&GetArrayString{})
|
bm.RegisterExecNode(NewExecNode[AddInt]())
|
||||||
bm.RegExecNode(&GetArrayLen{})
|
bm.RegisterExecNode(NewExecNode[SubInt]())
|
||||||
bm.RegExecNode(&CreateIntArray{})
|
bm.RegisterExecNode(NewExecNode[MulInt]())
|
||||||
bm.RegExecNode(&CreateStringArray{})
|
bm.RegisterExecNode(NewExecNode[DivInt]())
|
||||||
bm.RegExecNode(&AppendIntegerToArray{})
|
bm.RegisterExecNode(NewExecNode[ModInt]())
|
||||||
bm.RegExecNode(&AppendStringToArray{})
|
bm.RegisterExecNode(NewExecNode[RandNumber]())
|
||||||
|
|
||||||
|
bm.RegisterExecNode(NewExecNode[Entrance_ArrayParam]())
|
||||||
|
bm.RegisterExecNode(NewExecNode[Entrance_IntParam]())
|
||||||
|
bm.RegisterExecNode(NewExecNode[Entrance_Timer]())
|
||||||
|
bm.RegisterExecNode(NewExecNode[DebugOutput]())
|
||||||
|
bm.RegisterExecNode(NewExecNode[Sequence]())
|
||||||
|
bm.RegisterExecNode(NewExecNode[Foreach]())
|
||||||
|
bm.RegisterExecNode(NewExecNode[ForeachIntArray]())
|
||||||
|
|
||||||
|
bm.RegisterExecNode(NewExecNode[GetArrayInt]())
|
||||||
|
bm.RegisterExecNode(NewExecNode[GetArrayString]())
|
||||||
|
bm.RegisterExecNode(NewExecNode[GetArrayLen]())
|
||||||
|
bm.RegisterExecNode(NewExecNode[CreateIntArray]())
|
||||||
|
bm.RegisterExecNode(NewExecNode[CreateStringArray]())
|
||||||
|
bm.RegisterExecNode(NewExecNode[AppendIntegerToArray]())
|
||||||
|
bm.RegisterExecNode(NewExecNode[AppendStringToArray]())
|
||||||
|
bm.RegisterExecNode(NewExecNode[BoolIf]())
|
||||||
|
bm.RegisterExecNode(NewExecNode[GreaterThanInteger]())
|
||||||
|
bm.RegisterExecNode(NewExecNode[LessThanInteger]())
|
||||||
|
bm.RegisterExecNode(NewExecNode[EqualInteger]())
|
||||||
|
bm.RegisterExecNode(NewExecNode[RangeCompare]())
|
||||||
|
bm.RegisterExecNode(NewExecNode[EqualSwitch]())
|
||||||
|
bm.RegisterExecNode(NewExecNode[Probability]())
|
||||||
|
bm.RegisterExecNode(NewExecNode[CreateTimer]())
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
func (bm *Blueprint) StartHotReload() (func(),error) {
|
||||||
|
var execPool ExecPool
|
||||||
|
var graphPool GraphPool
|
||||||
|
|
||||||
|
// 加载配置结点生成名字对应的innerExecNode
|
||||||
|
err := execPool.Load(bm.execDefFilePath)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// 将注册的实际执行结点与innerExecNode进行关联
|
||||||
|
for _, newExec := range bm.execNodeList {
|
||||||
|
e := newExec()
|
||||||
|
if !execPool.Register(e) {
|
||||||
|
return nil,fmt.Errorf("register exec failed,exec:%s", e.GetName())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 加载所有的vgf蓝图文件
|
||||||
|
err = graphPool.Load(&execPool, bm.graphFilePath, bm.blueprintModule)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// 返回配置加载后的刷新内存处理
|
||||||
|
return func() {
|
||||||
|
// 替换旧的执行池和图池
|
||||||
|
bm.execPool = &execPool
|
||||||
|
bm.graphPool = &graphPool
|
||||||
|
|
||||||
|
for _, gh := range bm.mapGraph {
|
||||||
|
gFileName := gh.GetGraphFileName()
|
||||||
|
bGraph := bm.graphPool.GetBaseGraph(gFileName)
|
||||||
|
if bGraph == nil {
|
||||||
|
log.Warn("GetBaseGraph fail", log.String("graph file name", gFileName))
|
||||||
|
bGraph = &baseGraph{entrance: map[int64]*execNode{}}
|
||||||
|
}
|
||||||
|
|
||||||
|
gh.HotReload(bGraph)
|
||||||
|
}
|
||||||
|
}, nil
|
||||||
|
|
||||||
bm.RegExecNode(&BoolIf{})
|
|
||||||
bm.RegExecNode(&GreaterThanInteger{})
|
|
||||||
bm.RegExecNode(&LessThanInteger{})
|
|
||||||
bm.RegExecNode(&EqualInteger{})
|
|
||||||
bm.RegExecNode(&RangeCompare{})
|
|
||||||
bm.RegExecNode(&EqualSwitch{})
|
|
||||||
bm.RegExecNode(&Probability{})
|
|
||||||
bm.RegExecNode(&CreateTimer{})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (bm *Blueprint) Init(execDefFilePath string, graphFilePath string, blueprintModule IBlueprintModule, cancelTimer func(*uint64) bool) error {
|
func (bm *Blueprint) Init(execDefFilePath string, graphFilePath string, blueprintModule IBlueprintModule, cancelTimer func(*uint64) bool) error {
|
||||||
|
var execPool ExecPool
|
||||||
|
var graphPool GraphPool
|
||||||
|
|
||||||
// 加载配置结点生成名字对应的innerExecNode
|
// 加载配置结点生成名字对应的innerExecNode
|
||||||
err := bm.execPool.Load(execDefFilePath)
|
err := execPool.Load(execDefFilePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// 注册系统执行结点
|
// 注册系统执行结点
|
||||||
bm.regSysNode()
|
bm.regSysNodes()
|
||||||
|
|
||||||
// 将注册的实际执行结点与innerExecNode进行关联
|
// 将注册的实际执行结点与innerExecNode进行关联
|
||||||
for _, e := range bm.execNodes {
|
for _, newExec := range bm.execNodeList {
|
||||||
if !bm.execPool.Register(e) {
|
e := newExec()
|
||||||
|
if !execPool.Register(e) {
|
||||||
return fmt.Errorf("register exec failed,exec:%s", e.GetName())
|
return fmt.Errorf("register exec failed,exec:%s", e.GetName())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 加载所有的vgf蓝图文件
|
// 加载所有的vgf蓝图文件
|
||||||
err = bm.graphPool.Load(&bm.execPool, graphFilePath, blueprintModule)
|
err = graphPool.Load(&execPool, graphFilePath, blueprintModule)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bm.execPool = &execPool
|
||||||
|
bm.graphPool = &graphPool
|
||||||
bm.cancelTimer = cancelTimer
|
bm.cancelTimer = cancelTimer
|
||||||
bm.blueprintModule = blueprintModule
|
bm.blueprintModule = blueprintModule
|
||||||
bm.mapGraph = make(map[int64]IGraph, 128)
|
bm.mapGraph = make(map[int64]IGraph, 128)
|
||||||
|
bm.execDefFilePath = execDefFilePath
|
||||||
|
bm.graphFilePath = graphFilePath
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,16 +4,20 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/duanhf2012/origin/v2/log"
|
||||||
"github.com/duanhf2012/origin/v2/service"
|
"github.com/duanhf2012/origin/v2/service"
|
||||||
"github.com/goccy/go-json"
|
"github.com/goccy/go-json"
|
||||||
"github.com/duanhf2012/origin/v2/log"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const ReturnVarial = "g_Return"
|
const ReturnVarial = "g_Return"
|
||||||
|
|
||||||
var IsDebug = false
|
var IsDebug = false
|
||||||
|
|
||||||
type IGraph interface {
|
type IGraph interface {
|
||||||
Do(entranceID int64, args ...any) (Port_Array, error)
|
Do(entranceID int64, args ...any) (Port_Array, error)
|
||||||
Release()
|
Release()
|
||||||
|
GetGraphFileName() string
|
||||||
|
HotReload(newBaseGraph *baseGraph)
|
||||||
}
|
}
|
||||||
|
|
||||||
type IBlueprintModule interface {
|
type IBlueprintModule interface {
|
||||||
@@ -30,7 +34,7 @@ type baseGraph struct {
|
|||||||
|
|
||||||
type Graph struct {
|
type Graph struct {
|
||||||
graphFileName string
|
graphFileName string
|
||||||
graphID int64
|
graphID int64
|
||||||
*baseGraph
|
*baseGraph
|
||||||
graphContext
|
graphContext
|
||||||
IBlueprintModule
|
IBlueprintModule
|
||||||
@@ -140,7 +144,7 @@ func (gc *graphConfig) GetNodeByID(nodeID string) *nodeConfig {
|
|||||||
|
|
||||||
func (gr *Graph) Do(entranceID int64, args ...any) (Port_Array, error) {
|
func (gr *Graph) Do(entranceID int64, args ...any) (Port_Array, error) {
|
||||||
if IsDebug {
|
if IsDebug {
|
||||||
log.Debug("Graph Do", log.String("graphName",gr.graphFileName),log.Int64("graphID", gr.graphID), log.Int64("entranceID", entranceID))
|
log.Debug("Graph Do", log.String("graphName", gr.graphFileName), log.Int64("graphID", gr.graphID), log.Int64("entranceID", entranceID))
|
||||||
}
|
}
|
||||||
|
|
||||||
entranceNode := gr.entrance[entranceID]
|
entranceNode := gr.entrance[entranceID]
|
||||||
@@ -204,3 +208,11 @@ func (gr *Graph) Release() {
|
|||||||
// 清理掉所有数据
|
// 清理掉所有数据
|
||||||
*gr = Graph{}
|
*gr = Graph{}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (gr *Graph) HotReload(newBaseGraph *baseGraph) {
|
||||||
|
gr.baseGraph = newBaseGraph
|
||||||
|
}
|
||||||
|
|
||||||
|
func (gr *Graph) GetGraphFileName() string{
|
||||||
|
return gr.graphFileName
|
||||||
|
}
|
||||||
|
|||||||
@@ -52,6 +52,15 @@ func (gp *GraphPool) Load(execPool *ExecPool, graphFilePath string, blueprintMod
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (gp *GraphPool) GetBaseGraph(graphName string) *baseGraph {
|
||||||
|
gr, ok := gp.mapGraphs[graphName]
|
||||||
|
if !ok {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return gr
|
||||||
|
}
|
||||||
|
|
||||||
func (gp *GraphPool) Create(graphName string, graphID int64) IGraph {
|
func (gp *GraphPool) Create(graphName string, graphID int64) IGraph {
|
||||||
gr, ok := gp.mapGraphs[graphName]
|
gr, ok := gp.mapGraphs[graphName]
|
||||||
if !ok {
|
if !ok {
|
||||||
|
|||||||
Reference in New Issue
Block a user