Compare commits

..

22 Commits
v2.1.10 ... v2

Author SHA1 Message Date
boyce
6f87cda07f 优化滚动日志 2026-02-02 15:02:33 +08:00
boyce
2e4863d073 优化网络层 2026-01-08 14:37:12 +08:00
boyce
330644cebb 优化ws读写最大限制 2026-01-08 08:33:29 +08:00
boyce
ef7ee0ab8e 优化协议解析器和服务接口 2025-12-25 14:32:13 +08:00
boyce
976efe0c04 新增通过模板名获取服务和node信息 2025-12-18 13:15:38 +08:00
boyce
b61906fb24 优化日志依赖 2025-12-18 09:36:35 +08:00
boyce
29dcf2bfeb 优化日志文件生成 2025-12-18 09:33:36 +08:00
boyce
857e3c4311 优化日志 2025-12-12 10:52:08 +08:00
boyce
210a3fa982 补充readme 2025-12-11 17:33:44 +08:00
boyce
d365dde8c0 优化服务发现 2025-12-11 09:56:39 +08:00
boyce
e0d412810f 优化服务发现 2025-12-10 10:13:53 +08:00
boyce
593abd263e 优化websocket
优化蓝图
2025-12-08 15:30:44 +08:00
boyce
75660bdec0 新增结点 2025-11-27 09:15:18 +08:00
boyce
eaf20c4e3a 新增蓝图热更新功能 2025-11-17 15:01:51 +08:00
boyce
027e83b706 整理优化代码 2025-11-17 10:55:17 +08:00
boyce
f9be55e98d 优化蓝图结点 2025-11-13 11:04:35 +08:00
boyce
d7c4cfb1ef 优化ws超时 2025-11-12 16:46:35 +08:00
boyce
4cb6882a1a 新增结点数组下标 2025-11-10 10:33:07 +08:00
boyce
b78d9721f2 新增蓝图结点 2025-11-07 14:16:47 +08:00
boyce
f8953d1764 优化bp结点 2025-11-05 17:01:43 +08:00
boyce
fac7a323e1 清理文件 2025-11-03 14:23:18 +08:00
boyce
1995d91cfc 优化蓝图执行器 2025-10-30 16:00:46 +08:00
36 changed files with 826 additions and 362 deletions

View File

@@ -80,8 +80,22 @@ Etcd方式示例
"DialTimeoutMillisecond": 3000,
"EtcdList": [
{
"NetworkName": ["network1"],
"Endpoints": ["http://192.168.13.24:12379"]
"LocalNetworkName": "network_Area1",
"Endpoints": ["http://127.0.0.1:12379"],
"UserName": "",
"Password": "",
"Cert": "",
"CertKey": "",
"Ca": ""
},
{
"NeighborNetworkName": ["network_Area2"],
"Endpoints": ["http://127.0.0.1:12379"],
"UserName": "",
"Password": "",
"Cert": "",
"CertKey": "",
"Ca": ""
}
]
}
@@ -92,12 +106,16 @@ TTLSecond表示健康检查TTL失效时间10秒
DialTimeoutMillisecond: 与etcd连接超时时间
EtcdListEtcd列表可以多个Etcd服务器连接
EtcdListEtcd列表可以多个Etcd服务器连接注意列表中必需有一个LocalNetworkName项表示当前所有的Node归属当前网络名为network_Area1Node下所有的服务会往network_Area1中注册。监听该网络的结点可以发现该网络中的Service。本地网络会默认监听本地网络中所有的服务。
NetworkName所在的网络名,可以配置多个。node会往对应的网络名称中注册、监听发现Service。NetworkName也起到发现隔离的作用。
NeighborNetworkName表示监听的邻居网络名,可以发现该网络中所有Service
EndpointsEtcd服务器地址
Origin方式示例
```json
@@ -105,10 +123,15 @@ Origin方式示例
"Discovery": {
"Origin":{
"TTLSecond": 10,
"LocalMasterNodeId": "bot",
"MasterNodeList": [
{
"NodeId": "test_1",
"ListenAddr": "127.0.0.1:8801"
"NodeId": "bot",
"ListenAddr": "127.0.0.1:11001"
},
{
"NodeId": "mp1server",
"ListenAddr": "127.0.0.1:11000"
}
]
}
@@ -118,8 +141,12 @@ Origin方式示例
TTLSecond表示健康检查TTL失效时间10秒
LocalMasterNodeId本地所有的Node归属当前Master NodeId。归属当前的所有的Node会往该Master Node中注册服务。MasterNode会自动同步给所有的监听结点。本地Node会默认监听本地Master Node中所有的服务。注意LocalMasterNodeId配置的NodeId要在MasterNodeList列表中。
MasterNodeList指定哪些Node为服务发现Master结点需要配置NodeId与ListenAddr注意它们要与实际的Node配置一致。
### RpcMode部分
默认模式
@@ -947,20 +974,45 @@ origin引擎默认使用读取所有结点配置的进行确认结点有哪些Se
"Private": false,
"remark": "//以_打头的表示只在本机进程不对整个子网开发",
"ServiceList": ["_TestService1", "TestService9", "TestService10"],
"DiscoveryService": [
"AllowDiscovery": [
{
"MasterNodeId": "nodeid_1",
"NetworkName":"networkname1"
"DiscoveryService": ["TestService8"]
"NetworkName":"networkname1",
"NodeIdList":[".*server"],
"ServiceList": ["TestService8"]
}
]
}]
}
```
DiscoveryService在当前nodeid为nodeid_test的结点中只发现 MasterNodeId为nodeid_1或NetworkName为networkname1网络中的TestService8服务。
以上如果是使用Etcd发现模式则表示可以发现网络名networkname1NodeId为server结尾服务名为TestService8服务。
AllowDiscovery可以配置发现的规则如果只配置MasterNodeId或NetworkName时(如果使用Etcd则只配置NetworkName,Origin则只配置MasterNodeId),则会筛选指定网络的所有服务,如下:
```
"AllowDiscovery": [
{
"NetworkName":"networkname1",
}
]
```
则以上只匹配networkname1网络名的所有服务支持正则表达式例如可以配置为"NetworkName":".*name1",则可以发现网络名为name1结尾的所有服务。
如果只发现NodeId为server结尾的所有服务可以使用以下配置方式
```
"AllowDiscovery": [
{
"NodeIdList":[".*server"]
}
]
```
筛选服务也是同上。也可以组合配置NetworkName和NodeIdList配置。
**注意**MasterNodeId与NetworkName只配置一个分别在模式为origin或者etcd服务发现类型时。
第八章HttpService使用
-----------------------

View File

@@ -3,13 +3,16 @@ package cluster
import (
"errors"
"fmt"
"reflect"
"strings"
"sync"
"regexp"
"github.com/duanhf2012/origin/v2/event"
"github.com/duanhf2012/origin/v2/log"
"github.com/duanhf2012/origin/v2/rpc"
"github.com/duanhf2012/origin/v2/service"
"reflect"
"strings"
"sync"
)
var configDir = "./config/"
@@ -23,21 +26,23 @@ const (
Discard NodeStatus = 1 //丢弃
)
type DiscoveryService struct {
MasterNodeId string //要筛选的主结点Id如果不配置或者配置成0表示针对所有的主结点
NetworkName string //如果是etcd指定要筛选的网络名中的服务不配置表示所有的网络
ServiceList []string //只发现的服务列表
// AllowDiscovery 允许发现的网络服务
type AllowDiscovery struct {
MasterNodeId string // 支持正则表达式
NetworkName string // 支持正则表达式
NodeIdList []string // 支持正则表达式
ServiceList []string
}
type NodeInfo struct {
NodeId string
Private bool
ListenAddr string
MaxRpcParamLen uint32 //最大Rpc参数长度
CompressBytesLen int //超过字节进行压缩的长度
ServiceList []string //所有的有序服务列表
PublicServiceList []string //对外公开的服务列表
DiscoveryService []DiscoveryService //筛选发现的服务,如果不配置,不进行筛选
MaxRpcParamLen uint32 //最大Rpc参数长度
CompressBytesLen int //超过字节进行压缩的长度
ServiceList []string //所有的有序服务列表
PublicServiceList []string //对外公开的服务列表
AllowDiscovery []AllowDiscovery //允许发现的网络服务
status NodeStatus
Retire bool
}
@@ -222,9 +227,9 @@ func (cls *Cluster) serviceDiscoverySetNodeInfo(nodeInfo *NodeInfo) {
}
cls.mapRpc[nodeInfo.NodeId] = &rpcInfo
if cls.IsNatsMode() == true || cls.discoveryInfo.discoveryType != OriginType {
log.Info("Discovery nodeId and new rpc client", log.String("NodeId", nodeInfo.NodeId), log.Any("services:", nodeInfo.PublicServiceList), log.Bool("Retire", nodeInfo.Retire))
log.Info("Discovery node and new rpc client", log.String("NodeId", nodeInfo.NodeId), log.Any("services:", nodeInfo.PublicServiceList), log.Bool("Retire", nodeInfo.Retire))
} else {
log.Info("Discovery nodeId and new rpc client", log.String("NodeId", nodeInfo.NodeId), log.Any("services:", nodeInfo.PublicServiceList), log.Bool("Retire", nodeInfo.Retire), log.String("nodeListenAddr", nodeInfo.ListenAddr))
log.Info("Discovery node and new rpc client", log.String("NodeId", nodeInfo.NodeId), log.Any("services:", nodeInfo.PublicServiceList), log.Bool("Retire", nodeInfo.Retire), log.String("nodeListenAddr", nodeInfo.ListenAddr))
}
}
@@ -406,13 +411,13 @@ func GetNodeByServiceName(serviceName string) map[string]struct{} {
return mapNodeId
}
// GetNodeByTemplateServiceName 通过模板服务名获取服务名,返回 map[serviceName真实服务名]NodeId
func GetNodeByTemplateServiceName(templateServiceName string) map[string]string {
// GetNodeByTemplateServiceName 通过模板服务名获取服务名,返回 map[serviceName真实服务名][]NodeId
func GetNodeByTemplateServiceName(templateServiceName string) map[string][]string {
cluster.locker.RLock()
defer cluster.locker.RUnlock()
mapServiceName := cluster.mapTemplateServiceNode[templateServiceName]
mapNodeId := make(map[string]string, 9)
mapNodeId := make(map[string][]string, 9)
for serviceName := range mapServiceName {
mapNode, ok := cluster.mapServiceNode[serviceName]
if ok == false {
@@ -420,7 +425,9 @@ func GetNodeByTemplateServiceName(templateServiceName string) map[string]string
}
for nodeId := range mapNode {
mapNodeId[serviceName] = nodeId
nodes := mapNodeId[serviceName]
nodes = append(nodes, nodeId)
mapNodeId[serviceName] = nodes
}
}
@@ -461,28 +468,76 @@ func (cls *Cluster) GetNodeInfo(nodeId string) (NodeInfo, bool) {
return nodeInfo.nodeInfo, true
}
func (cls *Cluster) CanDiscoveryService(fromMasterNodeId string, serviceName string) bool {
func (cls *Cluster) CanDiscoveryService(fromNetworkName string, fromMasterNodeId string, fromNodeId string, serviceName string) bool {
canDiscovery := true
// 筛选允许的服务
splitServiceName := strings.Split(serviceName, ":")
if len(splitServiceName) == 2 {
serviceName = splitServiceName[0]
}
for i := 0; i < len(cls.GetLocalNodeInfo().DiscoveryService); i++ {
masterNodeId := cls.GetLocalNodeInfo().DiscoveryService[i].MasterNodeId
//无效的配置,则跳过
if masterNodeId == rpc.NodeIdNull && len(cls.GetLocalNodeInfo().DiscoveryService[i].ServiceList) == 0 {
continue
}
// 先筛选允许的网络,有配置才会检测
if len(cls.GetLocalNodeInfo().AllowDiscovery) > 0 {
allowNetwork := false
for i := 0; i < len(cls.GetLocalNodeInfo().AllowDiscovery); i++ {
masterNodeId := cls.GetLocalNodeInfo().AllowDiscovery[i].MasterNodeId
networkName := cls.GetLocalNodeInfo().AllowDiscovery[i].NetworkName
nodeIdList := cls.GetLocalNodeInfo().AllowDiscovery[i].NodeIdList
serviceList := cls.GetLocalNodeInfo().AllowDiscovery[i].ServiceList
canDiscovery = false
if masterNodeId == fromMasterNodeId || masterNodeId == rpc.NodeIdNull {
for _, discoveryService := range cls.GetLocalNodeInfo().DiscoveryService[i].ServiceList {
if discoveryService == serviceName {
return true
// 如果配置了网络及Master结点则匹配之
if fromNetworkName != "" {
matchNetWork, _ := regexp.MatchString(networkName, fromNetworkName)
if !matchNetWork {
continue
}
} else if fromMasterNodeId != "" {
matchMasterNode, _ := regexp.MatchString(masterNodeId, fromMasterNodeId)
if !matchMasterNode {
continue
}
}
// 如果配置了
if len(nodeIdList) > 0 {
hasNode := false
for _, nodeId := range nodeIdList {
matchNodeId, _ := regexp.MatchString(nodeId, fromNodeId)
if !matchNodeId {
continue
}
hasNode = true
break
}
if !hasNode {
continue
}
}
// 如果配置了服务,则匹配之
if len(serviceList) > 0 {
hasService := false
for _, service := range serviceList {
// service按正则表达式匹配serviceName
matched, _ := regexp.MatchString(service, serviceName)
if matched {
hasService = true
break
}
}
if !hasService {
continue
}
}
allowNetwork = true
break
}
if !allowNetwork {
return false
}
}

View File

@@ -6,6 +6,12 @@ import (
"crypto/x509"
"errors"
"fmt"
"os"
"path"
"strings"
"sync/atomic"
"time"
"github.com/duanhf2012/origin/v2/event"
"github.com/duanhf2012/origin/v2/log"
"github.com/duanhf2012/origin/v2/rpc"
@@ -14,19 +20,15 @@ import (
"go.etcd.io/etcd/api/v3/mvccpb"
"go.etcd.io/etcd/client/v3"
"google.golang.org/protobuf/proto"
"os"
"path"
"strings"
"sync/atomic"
"time"
)
const originDir = "/origin"
type etcdClientInfo struct {
watchKeys []string
leaseID clientv3.LeaseID
keepAliveChan <-chan *clientv3.LeaseKeepAliveResponse
isLocalNetwork bool
watchKeys []string
leaseID clientv3.LeaseID
keepAliveChan <-chan *clientv3.LeaseKeepAliveResponse
}
type EtcdDiscoveryService struct {
@@ -93,6 +95,7 @@ func (ed *EtcdDiscoveryService) OnInit() error {
return errors.New("etcd discovery config is nil.")
}
var hasLocalNetwork bool
for i := 0; i < len(etcdDiscoveryCfg.EtcdList); i++ {
var client *clientv3.Client
var tlsConfig *tls.Config
@@ -141,13 +144,25 @@ func (ed *EtcdDiscoveryService) OnInit() error {
}
ec := &etcdClientInfo{}
for _, networkName := range etcdDiscoveryCfg.EtcdList[i].NetworkName {
ec.watchKeys = append(ec.watchKeys, fmt.Sprintf("%s/%s", originDir, networkName))
if etcdDiscoveryCfg.EtcdList[i].LocalNetworkName != "" {
hasLocalNetwork = true
ec.isLocalNetwork = true
ec.watchKeys = append(ec.watchKeys, fmt.Sprintf("%s/%s", originDir, etcdDiscoveryCfg.EtcdList[i].LocalNetworkName))
} else {
ec.isLocalNetwork = false
for _, networkName := range etcdDiscoveryCfg.EtcdList[i].NeighborNetworkName {
ec.watchKeys = append(ec.watchKeys, fmt.Sprintf("%s/%s", originDir, networkName))
}
}
ed.mapClient[client] = ec
}
if !hasLocalNetwork {
return errors.New("etcd discovery init fail,cannot find local network")
}
return nil
}
@@ -167,14 +182,18 @@ func (ed *EtcdDiscoveryService) registerServiceByClient(client *clientv3.Client,
}
etcdClient.leaseID = resp.ID
for _, watchKey := range etcdClient.watchKeys {
// 注册服务节点到 etcd
_, err = client.Put(context.Background(), ed.getRegisterKey(watchKey), ed.byteLocalNodeInfo, clientv3.WithLease(resp.ID))
if err != nil {
log.Error("etcd Put fail", log.ErrorField("err", err))
ed.tryRegisterService(client, etcdClient)
return
}
// 注册服务节点到 etcd,LocalNetwork时才会注册且etcdClient.watchKeys必然>0
if len(etcdClient.watchKeys) != 1 {
log.Error("LocalNetwork watchkey is error")
return
}
_, err = client.Put(context.Background(), ed.getRegisterKey(etcdClient.watchKeys[0]), ed.byteLocalNodeInfo, clientv3.WithLease(resp.ID))
if err != nil {
log.Error("etcd Put fail", log.ErrorField("err", err))
ed.tryRegisterService(client, etcdClient)
return
}
etcdClient.keepAliveChan, err = client.KeepAlive(context.Background(), etcdClient.leaseID)
@@ -204,6 +223,10 @@ func (ed *EtcdDiscoveryService) tryRegisterService(client *clientv3.Client, etcd
return
}
if !etcdClient.isLocalNetwork {
return
}
ed.AfterFunc(time.Second*3, func(t *timer.Timer) {
ed.registerServiceByClient(client, etcdClient)
})
@@ -229,13 +252,18 @@ func (ed *EtcdDiscoveryService) tryLaterRetire() {
func (ed *EtcdDiscoveryService) retire() error {
//从etcd中更新
for c, ec := range ed.mapClient {
for _, watchKey := range ec.watchKeys {
// 注册服务节点到 etcd
_, err := c.Put(context.Background(), ed.getRegisterKey(watchKey), ed.byteLocalNodeInfo, clientv3.WithLease(ec.leaseID))
if err != nil {
log.Error("etcd Put fail", log.ErrorField("err", err))
return err
}
if !ec.isLocalNetwork {
continue
}
if len(ec.watchKeys)!=1 {
log.Error("LocalNetwork watchkey is error")
continue
}
_, err := c.Put(context.Background(), ed.getRegisterKey(ec.watchKeys[0]), ed.byteLocalNodeInfo, clientv3.WithLease(ec.leaseID))
if err != nil {
log.Error("etcd Put fail", log.ErrorField("err", err))
return err
}
}
@@ -292,7 +320,7 @@ func (ed *EtcdDiscoveryService) setNodeInfo(networkName string, nodeInfo *rpc.No
//筛选关注的服务
var discoverServiceSlice = make([]string, 0, 24)
for _, pubService := range nodeInfo.PublicServiceList {
if cluster.CanDiscoveryService(networkName, pubService) == true {
if cluster.CanDiscoveryService(networkName, "", nodeInfo.NodeId, pubService) == true {
discoverServiceSlice = append(discoverServiceSlice, pubService)
}
}
@@ -503,11 +531,16 @@ func (ed *EtcdDiscoveryService) RPC_ServiceRecord(etcdServiceRecord *service.Etc
//写入到etcd中
for c, info := range ed.mapClient {
for _, watchKey := range info.watchKeys {
if ed.getNetworkNameByWatchKey(watchKey) == etcdServiceRecord.NetworkName {
client = c
break
}
if !info.isLocalNetwork {
continue
}
if len(info.watchKeys)!=1 {
log.Error("")
}
if ed.getNetworkNameByWatchKey(info.watchKeys[0]) == etcdServiceRecord.NetworkName {
client = c
break
}
}

View File

@@ -508,11 +508,17 @@ func (dc *OriginDiscoveryClient) regServiceDiscover(nodeId string) {
if nodeId == cluster.GetLocalNodeInfo().NodeId {
return
}
nodeInfo := cluster.getOriginMasterDiscoveryNodeInfo(nodeId)
if nodeInfo == nil {
return
}
// 只能往本地Master结点上注册
if !cluster.IsLocalMasterNodeId(nodeId) {
return
}
var req rpc.RegServiceDiscoverReq
req.NodeInfo = &rpc.NodeInfo{}
req.NodeInfo.NodeId = cluster.localNodeInfo.NodeId
@@ -548,7 +554,7 @@ func (dc *OriginDiscoveryClient) setNodeInfo(masterNodeId string, nodeInfo *rpc.
//筛选关注的服务
var discoverServiceSlice = make([]string, 0, 24)
for _, pubService := range nodeInfo.PublicServiceList {
if cluster.CanDiscoveryService(masterNodeId, pubService) == true {
if cluster.CanDiscoveryService("",masterNodeId, nodeInfo.NodeId,pubService) == true {
discoverServiceSlice = append(discoverServiceSlice, pubService)
}
}
@@ -617,13 +623,22 @@ func (cls *Cluster) IsOriginMasterDiscoveryNode(nodeId string) bool {
return cls.getOriginMasterDiscoveryNodeInfo(nodeId) != nil
}
func (cls *Cluster) IsLocalMasterNodeId(nodeId string) bool {
if cls.discoveryInfo.Origin == nil {
return false
}
return cls.discoveryInfo.Origin.LocalMasterNodeId == nodeId
}
func (cls *Cluster) getOriginMasterDiscoveryNodeInfo(nodeId string) *NodeInfo {
if cls.discoveryInfo.Origin == nil {
return nil
}
for i := 0; i < len(cls.discoveryInfo.Origin.MasterNodeList); i++ {
if cls.discoveryInfo.Origin.MasterNodeList[i].NodeId == nodeId {
if cls.discoveryInfo.Origin.MasterNodeList[i].NodeId == nodeId {
return &cls.discoveryInfo.Origin.MasterNodeList[i]
}
}

View File

@@ -16,27 +16,27 @@ import (
var json = jsoniter.ConfigCompatibleWithStandardLibrary
type EtcdList struct {
NetworkName []string
Endpoints []string
UserName string
Password string
Cert string
CertKey string
Ca string
LocalNetworkName string // 如果配置,则为本地网络,必需配置一个本地网络
NeighborNetworkName []string
Endpoints []string
UserName string
Password string
Cert string
CertKey string
Ca string
}
type EtcdDiscovery struct {
DialTimeoutMillisecond time.Duration
TTLSecond int64
EtcdList []EtcdList
EtcdList []EtcdList
}
type OriginDiscovery struct {
TTLSecond int64
MasterNodeList []NodeInfo
TTLSecond int64
LocalMasterNodeId string
MasterNodeList []NodeInfo
}
type DiscoveryType int
@@ -72,7 +72,7 @@ type NodeInfoList struct {
}
func validConfigFile(f string) bool {
return strings.HasSuffix(f, ".json")|| strings.HasSuffix(f, ".yml") || strings.HasSuffix(f, ".yaml")
return strings.HasSuffix(f, ".json") || strings.HasSuffix(f, ".yml") || strings.HasSuffix(f, ".yaml")
}
func yamlToJson(data []byte, v interface{}) ([]byte, error) {
@@ -131,21 +131,20 @@ func (d *DiscoveryInfo) setEtcd(etcd *EtcdDiscovery) error {
return fmt.Errorf("repeat configuration of Discovery")
}
//Endpoints不允许重复
mapAddr := make(map[string]struct{})
for _, n := range etcd.EtcdList {
for _, endPoint := range n.Endpoints {
if _, ok := mapAddr[endPoint]; ok == true {
return fmt.Errorf("etcd discovery config Etcd.EtcdList.Endpoints %+v is repeat", endPoint)
}
mapAddr[endPoint] = struct{}{}
}
//networkName不允许重复
mapNetworkName := make(map[string]struct{})
for _, netName := range n.NetworkName {
if n.LocalNetworkName != "" {
if _, ok := mapNetworkName[n.LocalNetworkName]; ok == true {
return fmt.Errorf("etcd discovery config Etcd.EtcdList.LocalNetworkName %+v is repeat", n.LocalNetworkName)
}
mapNetworkName[n.LocalNetworkName] = struct{}{}
continue
}
for _, netName := range n.NeighborNetworkName {
if _, ok := mapNetworkName[netName]; ok == true {
return fmt.Errorf("etcd discovery config Etcd.EtcdList.NetworkName %+v is repeat", n.NetworkName)
return fmt.Errorf("etcd discovery config Etcd.EtcdList.NetworkName %+v is repeat", netName)
}
mapNetworkName[netName] = struct{}{}
@@ -275,7 +274,7 @@ func (cls *Cluster) readLocalClusterConfig(nodeId string) (DiscoveryInfo, []Node
var rpcMode RpcMode
//读取任何文件,只读符合格式的配置,目录下的文件可以自定义分文件
err := filepath.Walk(configDir, func(path string, info fs.FileInfo, err error)error {
err := filepath.Walk(configDir, func(path string, info fs.FileInfo, err error) error {
if info.IsDir() {
return nil
}
@@ -340,7 +339,7 @@ func (cls *Cluster) readLocalService(localNodeId string) error {
nodeService := map[string]interface{}{}
//读取任何文件,只读符合格式的配置,目录下的文件可以自定义分文件
err := filepath.Walk(configDir, func(path string, info fs.FileInfo, err error)error{
err := filepath.Walk(configDir, func(path string, info fs.FileInfo, err error) error {
if info.IsDir() {
return nil
}
@@ -432,7 +431,7 @@ func (cls *Cluster) readLocalService(localNodeId string) error {
return nil
}
func (cls *Cluster) parseLocalCfg() error{
func (cls *Cluster) parseLocalCfg() error {
rpcInfo := NodeRpcInfo{}
rpcInfo.nodeInfo = cls.localNodeInfo
rpcInfo.client = rpc.NewLClient(rpcInfo.nodeInfo.NodeId, &cls.callSet)
@@ -454,7 +453,7 @@ func (cls *Cluster) parseLocalCfg() error{
cls.mapServiceNode[serviceName] = make(map[string]struct{})
}
if _,ok:=cls.mapServiceNode[serviceName][cls.localNodeInfo.NodeId];ok {
if _, ok := cls.mapServiceNode[serviceName][cls.localNodeInfo.NodeId]; ok {
return fmt.Errorf("duplicate service %s is configured in node %s", serviceName, cls.localNodeInfo.NodeId)
}
cls.mapServiceNode[serviceName][cls.localNodeInfo.NodeId] = struct{}{}
@@ -508,6 +507,30 @@ func (cls *Cluster) IsConfigService(serviceName string) bool {
return ok
}
// GetServiceInfoByTemplateService 通过模板服务名获取map[服务名]map[NodeId]struct{}
func (cls *Cluster) GetServiceInfoByTemplateService(templateServiceName string) map[string]map[string]struct{} {
mapService := map[string]map[string]struct{}{}
cls.locker.RLock()
defer cls.locker.RUnlock()
mapServiceName := cls.mapTemplateServiceNode[templateServiceName]
for serviceName := range mapServiceName {
mapNodeId, ok := cls.mapServiceNode[serviceName]
if ok == true {
for nodeId := range mapNodeId{
mapNodeIds:=mapService[serviceName]
if mapNodeIds==nil {
mapNodeIds = map[string]struct{}{}
mapService[serviceName] = mapNodeIds
}
mapNodeIds[nodeId] = struct{}{}
}
}
}
return mapService
}
func (cls *Cluster) GetNodeIdByTemplateService(templateServiceName string, rpcClientList []*rpc.Client, filterRetire bool) (error, []*rpc.Client) {
cls.locker.RLock()
defer cls.locker.RUnlock()

View File

@@ -10,7 +10,7 @@ const (
Sys_Event_Tcp EventType = -3
Sys_Event_Http_Event EventType = -4
Sys_Event_WebSocket EventType = -5
Sys_Event_Kcp EventType = -6
Sys_Event_Kcp EventType = -6
Sys_Event_Node_Conn_Event EventType = -7
Sys_Event_Nats_Conn_Event EventType = -8
Sys_Event_DiscoverService EventType = -9
@@ -18,6 +18,6 @@ const (
Sys_Event_EtcdDiscovery EventType = -11
Sys_Event_Gin_Event EventType = -12
Sys_Event_FrameTick EventType = -13
Sys_Event_User_Define EventType = 1
Sys_Event_ReloadBlueprint EventType = -14
Sys_Event_User_Define EventType = 1
)

2
go.mod
View File

@@ -6,7 +6,7 @@ toolchain go1.22.7
require (
github.com/IBM/sarama v1.43.3
github.com/duanhf2012/rotatelogs v0.0.0-20250124024205-39765c212d8a
github.com/duanhf2012/rotatelogs v0.0.0-20260202065658-f38ef69c6a39
github.com/gin-gonic/gin v1.10.0
github.com/go-sql-driver/mysql v1.6.0
github.com/goccy/go-json v0.10.2

4
go.sum
View File

@@ -20,8 +20,8 @@ github.com/coreos/go-systemd/v22 v22.3.2/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSV
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/duanhf2012/rotatelogs v0.0.0-20250124024205-39765c212d8a h1:BVmZrOSKTg9ry1YjqY6IjVXmBDsFdX/W+pnvO5cPUDc=
github.com/duanhf2012/rotatelogs v0.0.0-20250124024205-39765c212d8a/go.mod h1:S/NNkpdnXps6VXaYVVDFtqQAm/NKayHxxOAhsrFnCgg=
github.com/duanhf2012/rotatelogs v0.0.0-20260202065658-f38ef69c6a39 h1:T+lS1jdEUNgkx3gG6MrO4rgkf8jGJNHSfRLKsLz31MM=
github.com/duanhf2012/rotatelogs v0.0.0-20260202065658-f38ef69c6a39/go.mod h1:S/NNkpdnXps6VXaYVVDFtqQAm/NKayHxxOAhsrFnCgg=
github.com/eapache/go-resiliency v1.7.0 h1:n3NRTnBn5N0Cbi/IeOHuQn9s2UwVUH7Ga0ZWcP+9JTA=
github.com/eapache/go-resiliency v1.7.0/go.mod h1:5yPzW0MIvSe0JDsv0v+DvcjEv2FyD6iZYSs1ZI+iQho=
github.com/eapache/go-xerial-snappy v0.0.0-20230731223053-c322873962e3 h1:Oy0F4ALJ04o5Qqpdz8XLIpNA3WM/iSIXqxtqo7UGVws=

View File

@@ -142,7 +142,7 @@ func (logger *Logger) NewRotatelogsWriter() zapcore.WriteSyncer {
options = append(options, rotatelogs.WithRotationTime(time.Hour*24))
fileName := strings.TrimRight(logger.FileName, filepath.Ext(logger.FileName))
rotateLogs, err := rotatelogs.NewRotateLogs(LogPath, "20060102/"+fileName+"_20060102_150405", options...)
rotateLogs, err := rotatelogs.NewRotateLogs(LogPath, fileName,"20060102","_20060102_150405", options...)
if err != nil {
panic(err)
}

View File

@@ -69,15 +69,15 @@ func (client *KCPClient) init() {
if client.MinMsgLen == 0 {
client.MinMsgLen = Default_MinMsgLen
}
if client.MaxMsgLen == 0 {
client.MaxMsgLen = Default_MaxMsgLen
if client.MaxReadMsgLen == 0 {
client.MaxReadMsgLen = Default_MaxReadMsgLen
}
if client.LenMsgLen == 0 {
client.LenMsgLen = Default_LenMsgLen
}
maxMsgLen := client.MsgParser.getMaxMsgLen()
if client.MaxMsgLen > maxMsgLen {
client.MaxMsgLen = maxMsgLen
if client.MaxReadMsgLen > maxMsgLen {
client.MaxReadMsgLen = maxMsgLen
log.Info("invalid MaxMsgLen", log.Uint32("reset", maxMsgLen))
}

View File

@@ -81,7 +81,8 @@ type KcpCfg struct {
LittleEndian bool //是否小端序
LenMsgLen int //消息头占用byte数量只能是1byte,2byte,4byte。如果是4byte意味着消息最大可以是math.MaxUint32(4GB)
MinMsgLen uint32 //最小消息长度
MaxMsgLen uint32 //最大消息长度,超过判定不合法,断开连接
MaxReadMsgLen uint32 //最大消息长度,超过判定不合法,断开连接
MaxWriteMsgLen uint32 // 最大写消息长度
PendingWriteNum int //写channel最大消息数量
}
@@ -89,7 +90,8 @@ func (kp *KCPServer) Init(kcpCfg *KcpCfg) {
kp.kcpCfg = kcpCfg
kp.msgParser.Init()
kp.msgParser.LenMsgLen = kp.kcpCfg.LenMsgLen
kp.msgParser.MaxMsgLen = kp.kcpCfg.MaxMsgLen
kp.msgParser.MaxReadMsgLen = kp.kcpCfg.MaxReadMsgLen
kp.msgParser.MaxWriteMsgLen = kp.kcpCfg.MaxWriteMsgLen
kp.msgParser.MinMsgLen = kp.kcpCfg.MinMsgLen
kp.msgParser.LittleEndian = kp.kcpCfg.LittleEndian

View File

@@ -45,9 +45,8 @@ func (jsonProcessor *JsonProcessor) SetByteOrder(littleEndian bool) {
}
// MsgRoute must goroutine safe
func (jsonProcessor *JsonProcessor) MsgRoute(clientId string, msg interface{}, recyclerReaderBytes func(data []byte)) error {
func (jsonProcessor *JsonProcessor) MsgRoute(clientId string, msg interface{}) error {
pPackInfo := msg.(*JsonPackInfo)
defer recyclerReaderBytes(pPackInfo.rawMsg)
v, ok := jsonProcessor.mapMsg[pPackInfo.typ]
if ok == false {
@@ -107,8 +106,7 @@ func (jsonProcessor *JsonProcessor) MakeRawMsg(msgType uint16, msg []byte) *Json
return &JsonPackInfo{typ: msgType, rawMsg: msg}
}
func (jsonProcessor *JsonProcessor) UnknownMsgRoute(clientId string, msg interface{}, recyclerReaderBytes func(data []byte)) {
defer recyclerReaderBytes(msg.([]byte))
func (jsonProcessor *JsonProcessor) UnknownMsgRoute(clientId string, msg interface{}) {
if jsonProcessor.unknownMessageHandler == nil {
log.Debug("Unknown message", log.String("clientId", clientId))
return

View File

@@ -54,10 +54,8 @@ func (slf *PBPackInfo) GetMsg() proto.Message {
}
// MsgRoute must goroutine safe
func (pbProcessor *PBProcessor) MsgRoute(clientId string, msg interface{}, recyclerReaderBytes func(data []byte)) error {
func (pbProcessor *PBProcessor) MsgRoute(clientId string, msg interface{}) error {
pPackInfo := msg.(*PBPackInfo)
defer recyclerReaderBytes(pPackInfo.rawMsg)
v, ok := pbProcessor.mapMsg[pPackInfo.typ]
if ok == false {
return fmt.Errorf("cannot find msgtype %d is register", pPackInfo.typ)
@@ -134,9 +132,8 @@ func (pbProcessor *PBProcessor) MakeRawMsg(msgType uint16, msg []byte) *PBPackIn
return &PBPackInfo{typ: msgType, rawMsg: msg}
}
func (pbProcessor *PBProcessor) UnknownMsgRoute(clientId string, msg interface{}, recyclerReaderBytes func(data []byte)) {
func (pbProcessor *PBProcessor) UnknownMsgRoute(clientId string, msg interface{}) {
pbProcessor.unknownMessageHandler(clientId, msg.([]byte))
recyclerReaderBytes(msg.([]byte))
}
func (pbProcessor *PBProcessor) ConnectedRoute(clientId string) {

View File

@@ -10,7 +10,7 @@ type RawMessageInfo struct {
msgHandler RawMessageHandler
}
type RawMessageHandler func(clientId string, packType uint16, msg []byte)
type RawMessageHandler func(clientId string, packType uint16,additionData any, msg []byte)
type RawConnectHandler func(clientId string)
type UnknownRawMessageHandler func(clientId string, msg []byte)
@@ -39,10 +39,9 @@ func (pbRawProcessor *PBRawProcessor) SetByteOrder(littleEndian bool) {
}
// MsgRoute must goroutine safe
func (pbRawProcessor *PBRawProcessor) MsgRoute(clientId string, msg interface{}, recyclerReaderBytes func(data []byte)) error {
func (pbRawProcessor *PBRawProcessor) MsgRoute(clientId string, msg interface{}) error {
pPackInfo := msg.(*PBRawPackInfo)
pbRawProcessor.msgHandler(clientId, pPackInfo.typ, pPackInfo.rawMsg)
recyclerReaderBytes(pPackInfo.rawMsg)
pbRawProcessor.msgHandler(clientId, pPackInfo.typ, nil, pPackInfo.rawMsg)
return nil
}
@@ -83,8 +82,7 @@ func (pbRawProcessor *PBRawProcessor) MakeRawMsg(msgType uint16, msg []byte, pbR
pbRawPackInfo.rawMsg = msg
}
func (pbRawProcessor *PBRawProcessor) UnknownMsgRoute(clientId string, msg interface{}, recyclerReaderBytes func(data []byte)) {
defer recyclerReaderBytes(msg.([]byte))
func (pbRawProcessor *PBRawProcessor) UnknownMsgRoute(clientId string, msg interface{}) {
if pbRawProcessor.unknownMessageHandler == nil {
return
}

View File

@@ -2,9 +2,9 @@ package processor
type IProcessor interface {
// MsgRoute must goroutine safe
MsgRoute(clientId string, msg interface{}, recyclerReaderBytes func(data []byte)) error
MsgRoute(clientId string, msg interface{}) error
// UnknownMsgRoute must goroutine safe
UnknownMsgRoute(clientId string, msg interface{}, recyclerReaderBytes func(data []byte))
UnknownMsgRoute(clientId string, msg interface{})
// ConnectedRoute connect event
ConnectedRoute(clientId string)
DisConnectedRoute(clientId string)
@@ -20,7 +20,6 @@ type IRawProcessor interface {
SetByteOrder(littleEndian bool)
SetRawMsgHandler(handle RawMessageHandler)
MakeRawMsg(msgType uint16, msg []byte, pbRawPackInfo *PBRawPackInfo)
SetUnknownMsgHandler(unknownMessageHandler UnknownRawMessageHandler)
SetConnectedHandler(connectHandler RawConnectHandler)
SetDisConnectedHandler(disconnectHandler RawConnectHandler)

View File

@@ -68,18 +68,19 @@ func (client *TCPClient) init() {
if client.MinMsgLen == 0 {
client.MinMsgLen = Default_MinMsgLen
}
if client.MaxMsgLen == 0 {
client.MaxMsgLen = Default_MaxMsgLen
if client.MaxReadMsgLen == 0 {
client.MaxReadMsgLen = Default_MaxReadMsgLen
}
if client.LenMsgLen == 0 {
client.LenMsgLen = Default_LenMsgLen
}
maxMsgLen := client.MsgParser.getMaxMsgLen()
if client.MaxMsgLen > maxMsgLen {
client.MaxMsgLen = maxMsgLen
if client.MaxReadMsgLen > maxMsgLen {
client.MaxReadMsgLen = maxMsgLen
log.Info("invalid MaxMsgLen", log.Uint32("reset", maxMsgLen))
}
client.cons = make(ConnSet)
client.closeFlag = false
client.MsgParser.Init()

View File

@@ -14,7 +14,8 @@ import (
type MsgParser struct {
LenMsgLen int
MinMsgLen uint32
MaxMsgLen uint32
MaxReadMsgLen uint32
MaxWriteMsgLen uint32
LittleEndian bool
bytespool.IBytesMemPool
@@ -67,7 +68,7 @@ func (p *MsgParser) Read(r io.Reader) ([]byte, error) {
}
// check len
if msgLen > p.MaxMsgLen {
if msgLen > p.MaxReadMsgLen {
return nil, errors.New("message too long")
} else if msgLen < p.MinMsgLen {
return nil, errors.New("message too short")
@@ -92,7 +93,7 @@ func (p *MsgParser) Write(conn io.Writer, args ...[]byte) error {
}
// check len
if msgLen > p.MaxMsgLen {
if p.MaxWriteMsgLen > 0 && msgLen > p.MaxWriteMsgLen {
return errors.New("message too long")
} else if msgLen < p.MinMsgLen {
return errors.New("message too short")

View File

@@ -11,13 +11,13 @@ import (
)
const (
Default_ReadDeadline = time.Second * 30 //默认读超时30s
Default_WriteDeadline = time.Second * 30 //默认写超时30s
Default_MaxConnNum = 1000000 //默认最大连接数
Default_PendingWriteNum = 100000 //单连接写消息Channel容量
Default_MinMsgLen = 2 //最小消息长度2byte
Default_LenMsgLen = 2 //包头字段长度占用2byte
Default_MaxMsgLen = 65535 //最大消息长度
Default_ReadDeadline = time.Second * 30 // 默认读超时30s
Default_WriteDeadline = time.Second * 30 // 默认写超时30s
Default_MaxConnNum = 1000000 // 默认最大连接数
Default_PendingWriteNum = 100000 // 单连接写消息Channel容量
Default_MinMsgLen = 2 // 最小消息长度2byte
Default_LenMsgLen = 2 // 包头字段长度占用2byte
Default_MaxReadMsgLen = 65535 // 最大消息长度
)
type TCPServer struct {
@@ -70,14 +70,14 @@ func (server *TCPServer) init() error {
log.Info("invalid LenMsgLen", log.Int("reset", server.LenMsgLen))
}
if server.MaxMsgLen <= 0 {
server.MaxMsgLen = Default_MaxMsgLen
log.Info("invalid MaxMsgLen", log.Uint32("reset to", server.MaxMsgLen))
if server.MaxReadMsgLen <= 0 {
server.MaxReadMsgLen = Default_MaxReadMsgLen
log.Info("invalid MaxMsgLen", log.Uint32("reset to", server.MaxReadMsgLen))
}
maxMsgLen := server.MsgParser.getMaxMsgLen()
if server.MaxMsgLen > maxMsgLen {
server.MaxMsgLen = maxMsgLen
if server.MaxReadMsgLen > maxMsgLen {
server.MaxReadMsgLen = maxMsgLen
log.Info("invalid MaxMsgLen", log.Uint32("reset", maxMsgLen))
}

View File

@@ -15,16 +15,16 @@ type WSConn struct {
sync.Mutex
conn *websocket.Conn
writeChan chan []byte
maxMsgLen uint32
maxWriteMsgLen uint32
closeFlag bool
header http.Header
}
func newWSConn(conn *websocket.Conn, header http.Header, pendingWriteNum int, maxMsgLen uint32, messageType int) *WSConn {
func newWSConn(conn *websocket.Conn, header http.Header, pendingWriteNum int, maxWriteMsgLen uint32, messageType int) *WSConn {
wsConn := new(WSConn)
wsConn.conn = conn
wsConn.writeChan = make(chan []byte, pendingWriteNum)
wsConn.maxMsgLen = maxMsgLen
wsConn.maxWriteMsgLen = maxWriteMsgLen
wsConn.header = header
go func() {
@@ -118,7 +118,7 @@ func (wsConn *WSConn) WriteMsg(args ...[]byte) error {
}
// check len
if msgLen > wsConn.maxMsgLen {
if wsConn.maxWriteMsgLen > 0 && msgLen > wsConn.maxWriteMsgLen {
return errors.New("message too long")
} else if msgLen < 1 {
return errors.New("message too short")

View File

@@ -3,32 +3,38 @@ package network
import (
"crypto/tls"
"errors"
"github.com/duanhf2012/origin/v2/log"
"github.com/gorilla/websocket"
"net"
"net/http"
"sync"
"time"
"github.com/duanhf2012/origin/v2/log"
"github.com/gorilla/websocket"
)
type WSServer struct {
Addr string
MaxConnNum int
PendingWriteNum int
MaxMsgLen uint32
HTTPTimeout time.Duration
MaxReadMsgLen uint32
MaxWriteMsgLen uint32
CertFile string
KeyFile string
NewAgent func(*WSConn) Agent
ln net.Listener
handler *WSHandler
messageType int
HandshakeTimeout time.Duration
ReadTimeout time.Duration
WriteTimeout time.Duration
}
type WSHandler struct {
maxConnNum int
pendingWriteNum int
maxMsgLen uint32
maxReadMsgLen uint32
maxWriteMsgLen uint32
newAgent func(*WSConn) Agent
upgrader websocket.Upgrader
conns WebsocketConnSet
@@ -51,7 +57,7 @@ func (handler *WSHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
log.Error("upgrade fail", log.String("error", err.Error()))
return
}
conn.SetReadLimit(int64(handler.maxMsgLen))
conn.SetReadLimit(int64(handler.maxReadMsgLen))
if handler.messageType == 0 {
handler.messageType = websocket.TextMessage
}
@@ -73,14 +79,14 @@ func (handler *WSHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}
handler.conns[conn] = struct{}{}
handler.mutexConns.Unlock()
c,ok:=conn.NetConn().(*net.TCPConn)
c, ok := conn.NetConn().(*net.TCPConn)
if !ok {
tlsConn,ok := conn.NetConn().(*tls.Conn)
tlsConn, ok := conn.NetConn().(*tls.Conn)
if !ok {
log.Error("conn error")
return
}
c,ok = tlsConn.NetConn().(*net.TCPConn)
c, ok = tlsConn.NetConn().(*net.TCPConn)
if !ok {
log.Error("conn error")
return
@@ -89,7 +95,7 @@ func (handler *WSHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
c.SetLinger(0)
c.SetNoDelay(true)
wsConn := newWSConn(conn, r.Header, handler.pendingWriteNum, handler.maxMsgLen, handler.messageType)
wsConn := newWSConn(conn, r.Header, handler.pendingWriteNum, handler.maxWriteMsgLen, handler.messageType)
agent := handler.newAgent(wsConn)
agent.Run()
@@ -114,7 +120,6 @@ func (server *WSServer) Start() error {
log.Error("WSServer Listen fail", log.String("error", err.Error()))
return err
}
if server.MaxConnNum <= 0 {
server.MaxConnNum = 100
log.Info("invalid MaxConnNum", log.Int("reset", server.MaxConnNum))
@@ -123,14 +128,23 @@ func (server *WSServer) Start() error {
server.PendingWriteNum = 100
log.Info("invalid PendingWriteNum", log.Int("reset", server.PendingWriteNum))
}
if server.MaxMsgLen <= 0 {
server.MaxMsgLen = 4096
log.Info("invalid MaxMsgLen", log.Uint32("reset", server.MaxMsgLen))
if server.MaxReadMsgLen <= 0 {
server.MaxReadMsgLen = 4096
log.Info("invalid MaxReadMsgLen", log.Uint32("reset", server.MaxReadMsgLen))
}
if server.HTTPTimeout <= 0 {
server.HTTPTimeout = 10 * time.Second
log.Info("invalid HTTPTimeout", log.Duration("reset", server.HTTPTimeout))
if server.HandshakeTimeout <= 0 {
server.HandshakeTimeout = 15 * time.Second
log.Info("invalid HandshakeTimeout", log.Duration("reset", server.HandshakeTimeout))
}
if server.ReadTimeout <= 0 {
server.ReadTimeout = 15 * time.Second
log.Info("invalid ReadTimeout", log.Duration("reset", server.ReadTimeout))
}
if server.WriteTimeout <= 0 {
server.WriteTimeout = 15 * time.Second
log.Info("invalid WriteTimeout", log.Duration("reset", server.WriteTimeout))
}
if server.NewAgent == nil {
log.Error("NewAgent must not be nil")
return errors.New("NewAgent must not be nil")
@@ -154,12 +168,13 @@ func (server *WSServer) Start() error {
server.handler = &WSHandler{
maxConnNum: server.MaxConnNum,
pendingWriteNum: server.PendingWriteNum,
maxMsgLen: server.MaxMsgLen,
maxReadMsgLen: server.MaxReadMsgLen,
maxWriteMsgLen: server.MaxWriteMsgLen,
newAgent: server.NewAgent,
conns: make(WebsocketConnSet),
messageType: server.messageType,
upgrader: websocket.Upgrader{
HandshakeTimeout: server.HTTPTimeout,
HandshakeTimeout: server.HandshakeTimeout,
CheckOrigin: func(_ *http.Request) bool { return true },
},
}
@@ -167,8 +182,8 @@ func (server *WSServer) Start() error {
httpServer := &http.Server{
Addr: server.Addr,
Handler: server.handler,
ReadTimeout: server.HTTPTimeout,
WriteTimeout: server.HTTPTimeout,
ReadTimeout: server.ReadTimeout,
WriteTimeout: server.WriteTimeout,
MaxHeaderBytes: 1024,
}

View File

@@ -127,9 +127,11 @@ func NewRClient(targetNodeId string, addr string, maxRpcParamLen uint32, compres
c.NewAgent = client.NewClientAgent
if maxRpcParamLen > 0 {
c.MaxMsgLen = maxRpcParamLen
c.MaxReadMsgLen = maxRpcParamLen
c.MaxWriteMsgLen = maxRpcParamLen
} else {
c.MaxMsgLen = math.MaxUint32
c.MaxReadMsgLen = math.MaxUint32
c.MaxWriteMsgLen = math.MaxUint32
}
client.IRealClient = c
client.CallSet = callSet

View File

@@ -91,9 +91,11 @@ func (server *Server) Start() error {
server.rpcServer.Addr = ":" + splitAddr[1]
server.rpcServer.MinMsgLen = 2
if server.maxRpcParamLen > 0 {
server.rpcServer.MaxMsgLen = server.maxRpcParamLen
server.rpcServer.MaxReadMsgLen = server.maxRpcParamLen
server.rpcServer.MaxWriteMsgLen = server.maxRpcParamLen
} else {
server.rpcServer.MaxMsgLen = math.MaxUint32
server.rpcServer.MaxReadMsgLen = math.MaxUint32
server.rpcServer.MaxWriteMsgLen = math.MaxUint32
}
server.rpcServer.MaxConnNum = 100000

View File

@@ -19,6 +19,7 @@ type KcpModule struct {
mapClientLocker sync.RWMutex
mapClient map[string]*Client
process processor.IRawProcessor
newClientIdHandler func() string
kcpServer network.KCPServer
kcpCfg *network.KcpCfg
@@ -56,7 +57,11 @@ func (km *KcpModule) OnInit() error {
km.process.SetByteOrder(km.kcpCfg.LittleEndian)
km.kcpServer.Init(km.kcpCfg)
km.kcpServer.NewAgent = km.NewAgent
if km.newClientIdHandler == nil {
km.newClientIdHandler = func()string{
return primitive.NewObjectID().Hex()
}
}
return nil
}
@@ -65,6 +70,10 @@ func (km *KcpModule) Init(kcpCfg *network.KcpCfg, process processor.IRawProcesso
km.process = process
}
func (km *KcpModule) SetNewClientIdHandler(newClientIdHandler func() string){
km.newClientIdHandler = newClientIdHandler
}
func (km *KcpModule) Start() error {
return km.kcpServer.Start()
}
@@ -77,9 +86,9 @@ func (km *KcpModule) kcpEventHandler(ev event.IEvent) {
case KPTDisConnected:
km.process.DisConnectedRoute(e.StringExt[0])
case KPTUnknownPack:
km.process.UnknownMsgRoute(e.StringExt[0], e.Data, e.AnyExt[0].(func(data []byte)))
km.process.UnknownMsgRoute(e.StringExt[0], e.Data)
case KPTPack:
km.process.MsgRoute(e.StringExt[0], e.Data, e.AnyExt[0].(func(data []byte)))
km.process.MsgRoute(e.StringExt[0], e.Data)
}
event.DeleteEvent(ev)
@@ -111,7 +120,7 @@ func (km *KcpModule) newClient(conn network.Conn) *Client {
km.mapClientLocker.Lock()
defer km.mapClientLocker.Unlock()
pClient := &Client{kcpConn: conn.(*network.NetConn), id: primitive.NewObjectID().Hex()}
pClient := &Client{kcpConn: conn.(*network.NetConn), id: km.newClientIdHandler()}
pClient.kcpModule = km
km.mapClient[pClient.id] = pClient

View File

@@ -20,6 +20,7 @@ type TcpModule struct {
mapClient map[string]*Client
process processor.IRawProcessor
tcpCfg *TcpCfg
newClientIdHandler func() string
}
type TcpPackType int8
@@ -35,6 +36,7 @@ type TcpPack struct {
Type TcpPackType //0表示连接 1表示断开 2表示数据
ClientId string
Data interface{}
rawData []byte
RecyclerReaderBytes func(data []byte)
}
@@ -51,7 +53,8 @@ type TcpCfg struct {
LittleEndian bool //是否小端序
LenMsgLen int //消息头占用byte数量只能是1byte,2byte,4byte。如果是4byte意味着消息最大可以是math.MaxUint32(4GB)
MinMsgLen uint32 //最小消息长度
MaxMsgLen uint32 //最大消息长度,超过判定不合法,断开连接
MaxReadMsgLen uint32 //最大消息长度,超过判定不合法,断开连接
MaxWriteMsgLen uint32 // 最大写消息长度
ReadDeadlineSecond time.Duration //读超时
WriteDeadlineSecond time.Duration //写超时
}
@@ -68,11 +71,17 @@ func (tm *TcpModule) OnInit() error {
tm.tcpServer.LittleEndian = tm.tcpCfg.LittleEndian
tm.tcpServer.LenMsgLen = tm.tcpCfg.LenMsgLen
tm.tcpServer.MinMsgLen = tm.tcpCfg.MinMsgLen
tm.tcpServer.MaxMsgLen = tm.tcpCfg.MaxMsgLen
tm.tcpServer.MaxReadMsgLen = tm.tcpCfg.MaxReadMsgLen
tm.tcpServer.MaxWriteMsgLen = tm.tcpCfg.MaxWriteMsgLen
tm.tcpServer.ReadDeadline = tm.tcpCfg.ReadDeadlineSecond * time.Second
tm.tcpServer.WriteDeadline = tm.tcpCfg.WriteDeadlineSecond * time.Second
tm.mapClient = make(map[string]*Client, tm.tcpServer.MaxConnNum)
tm.tcpServer.NewAgent = tm.NewClient
if tm.newClientIdHandler == nil {
tm.newClientIdHandler = func()string{
return primitive.NewObjectID().Hex()
}
}
//3.设置解析处理器
tm.process.SetByteOrder(tm.tcpCfg.LittleEndian)
@@ -87,6 +96,10 @@ func (tm *TcpModule) Init(tcpCfg *TcpCfg, process processor.IRawProcessor) {
tm.process = process
}
func (tm *TcpModule) SetNewClientIdHandler(newClientIdHandler func() string){
tm.newClientIdHandler = newClientIdHandler
}
func (tm *TcpModule) Start() error {
return tm.tcpServer.Start()
}
@@ -99,9 +112,11 @@ func (tm *TcpModule) tcpEventHandler(ev event.IEvent) {
case TPTDisConnected:
tm.process.DisConnectedRoute(pack.ClientId)
case TPTUnknownPack:
tm.process.UnknownMsgRoute(pack.ClientId, pack.Data, pack.RecyclerReaderBytes)
tm.process.UnknownMsgRoute(pack.ClientId, pack.Data)
pack.RecyclerReaderBytes(pack.rawData)
case TPTPack:
tm.process.MsgRoute(pack.ClientId, pack.Data, pack.RecyclerReaderBytes)
tm.process.MsgRoute(pack.ClientId, pack.Data)
pack.RecyclerReaderBytes(pack.rawData)
}
}
@@ -109,7 +124,7 @@ func (tm *TcpModule) NewClient(conn network.Conn) network.Agent {
tm.mapClientLocker.Lock()
defer tm.mapClientLocker.Unlock()
clientId := primitive.NewObjectID().Hex()
clientId := tm.newClientIdHandler()
pClient := &Client{tcpConn: conn.(*network.NetConn), id: clientId}
pClient.tcpModule = tm
tm.mapClient[clientId] = pClient
@@ -138,10 +153,10 @@ func (slf *Client) Run() {
}
data, err := slf.tcpModule.process.Unmarshal(slf.id, bytes)
if err != nil {
slf.tcpModule.NotifyEvent(&event.Event{Type: event.Sys_Event_Tcp, Data: TcpPack{ClientId: slf.id, Type: TPTUnknownPack, Data: bytes, RecyclerReaderBytes: slf.tcpConn.GetRecyclerReaderBytes()}})
slf.tcpModule.NotifyEvent(&event.Event{Type: event.Sys_Event_Tcp, Data: TcpPack{ClientId: slf.id, Type: TPTUnknownPack, Data: data,rawData: bytes,RecyclerReaderBytes: slf.tcpConn.GetRecyclerReaderBytes()}})
continue
}
slf.tcpModule.NotifyEvent(&event.Event{Type: event.Sys_Event_Tcp, Data: TcpPack{ClientId: slf.id, Type: TPTPack, Data: data, RecyclerReaderBytes: slf.tcpConn.GetRecyclerReaderBytes()}})
slf.tcpModule.NotifyEvent(&event.Event{Type: event.Sys_Event_Tcp, Data: TcpPack{ClientId: slf.id, Type: TPTPack, Data: data,rawData: bytes, RecyclerReaderBytes: slf.tcpConn.GetRecyclerReaderBytes()}})
}
}

View File

@@ -2,13 +2,15 @@ package wsmodule
import (
"fmt"
"sync"
"time"
"github.com/duanhf2012/origin/v2/event"
"github.com/duanhf2012/origin/v2/log"
"github.com/duanhf2012/origin/v2/network"
"github.com/duanhf2012/origin/v2/network/processor"
"github.com/duanhf2012/origin/v2/service"
"go.mongodb.org/mongo-driver/bson/primitive"
"sync"
)
type WSModule struct {
@@ -20,6 +22,7 @@ type WSModule struct {
mapClient map[string]*WSClient
process processor.IRawProcessor
wsCfg *WSCfg
newClientIdHandler func() string
}
type WSClient struct {
@@ -32,10 +35,15 @@ type WSCfg struct {
ListenAddr string
MaxConnNum int
PendingWriteNum int
MaxMsgLen uint32
MaxReadMsgLen uint32
MaxWriteMsgLen uint32
LittleEndian bool //是否小端序
KeyFile string
CertFile string
HandshakeTimeoutSecond time.Duration
ReadTimeoutSecond time.Duration
WriteTimeoutSecond time.Duration
}
type WSPackType int8
@@ -61,9 +69,17 @@ func (ws *WSModule) OnInit() error {
ws.WSServer.MaxConnNum = ws.wsCfg.MaxConnNum
ws.WSServer.PendingWriteNum = ws.wsCfg.PendingWriteNum
ws.WSServer.MaxMsgLen = ws.wsCfg.MaxMsgLen
ws.WSServer.MaxReadMsgLen = ws.wsCfg.MaxReadMsgLen
ws.WSServer.MaxWriteMsgLen = ws.wsCfg.MaxWriteMsgLen
ws.WSServer.Addr = ws.wsCfg.ListenAddr
ws.WSServer.HandshakeTimeout = ws.wsCfg.HandshakeTimeoutSecond*time.Second
ws.WSServer.ReadTimeout = ws.wsCfg.ReadTimeoutSecond*time.Second
ws.WSServer.WriteTimeout = ws.wsCfg.WriteTimeoutSecond*time.Second
if ws.newClientIdHandler == nil {
ws.newClientIdHandler = func()string{
return primitive.NewObjectID().Hex()
}
}
if ws.wsCfg.KeyFile != "" && ws.wsCfg.CertFile != "" {
ws.WSServer.KeyFile = ws.wsCfg.KeyFile
ws.WSServer.CertFile = ws.wsCfg.CertFile
@@ -86,6 +102,10 @@ func (ws *WSModule) Init(wsCfg *WSCfg, process processor.IRawProcessor) {
ws.process = process
}
func (ws *WSModule) SetNewClientIdHandler(newClientIdHandler func() string){
ws.newClientIdHandler = newClientIdHandler
}
func (ws *WSModule) Start() error {
return ws.WSServer.Start()
}
@@ -98,9 +118,9 @@ func (ws *WSModule) wsEventHandler(ev event.IEvent) {
case WPTDisConnected:
ws.process.DisConnectedRoute(pack.ClientId)
case WPTUnknownPack:
ws.process.UnknownMsgRoute(pack.ClientId, pack.Data, ws.recyclerReaderBytes)
ws.process.UnknownMsgRoute(pack.ClientId, pack.Data)
case WPTPack:
ws.process.MsgRoute(pack.ClientId, pack.Data, ws.recyclerReaderBytes)
ws.process.MsgRoute(pack.ClientId, pack.Data)
}
}
@@ -110,8 +130,7 @@ func (ws *WSModule) recyclerReaderBytes([]byte) {
func (ws *WSModule) NewWSClient(conn *network.WSConn) network.Agent {
ws.mapClientLocker.Lock()
defer ws.mapClientLocker.Unlock()
pClient := &WSClient{wsConn: conn, id: primitive.NewObjectID().Hex()}
pClient := &WSClient{wsConn: conn, id: ws.newClientIdHandler()}
pClient.wsModule = ws
ws.mapClient[pClient.id] = pClient
@@ -132,7 +151,7 @@ func (wc *WSClient) Run() {
}
data, err := wc.wsModule.process.Unmarshal(wc.id, bytes)
if err != nil {
wc.wsModule.NotifyEvent(&event.Event{Type: event.Sys_Event_WebSocket, Data: &WSPack{ClientId: wc.id, Type: WPTUnknownPack, Data: bytes}})
wc.wsModule.NotifyEvent(&event.Event{Type: event.Sys_Event_WebSocket, Data: &WSPack{ClientId: wc.id, Type: WPTUnknownPack, Data: data}})
continue
}
wc.wsModule.NotifyEvent(&event.Event{Type: event.Sys_Event_WebSocket, Data: &WSPack{ClientId: wc.id, Type: WPTPack, Data: data}})
@@ -150,6 +169,22 @@ func (ws *WSModule) GetProcessor() processor.IRawProcessor {
return ws.process
}
func (ws *WSModule) GetClientHeader(clientId string,key string) string {
ws.mapClientLocker.Lock()
defer ws.mapClientLocker.Unlock()
pClient, ok := ws.mapClient[clientId]
if ok == false || pClient.wsConn == nil {
return ""
}
if pClient.wsConn.GetHeader() == nil {
log.Warn("clientId header is nil", log.String("clientId", clientId))
}
return pClient.wsConn.GetHeader().Get(key)
}
func (ws *WSModule) GetClientIp(clientId string) string {
ws.mapClientLocker.Lock()
defer ws.mapClientLocker.Unlock()

View File

@@ -3,78 +3,156 @@ package blueprint
import (
"fmt"
"sync/atomic"
"github.com/duanhf2012/origin/v2/log"
)
type Blueprint struct {
execNodes []IExecNode // 注册的定义执行结点
execPool ExecPool
graphPool GraphPool
execNodeList []func() IExecNode
execPool *ExecPool
graphPool *GraphPool
blueprintModule IBlueprintModule
mapGraph map[int64]IGraph
seedID int64
cancelTimer func(*uint64)bool
mapGraph map[int64]IGraph
seedID int64
cancelTimer func(*uint64) bool
execDefFilePath string // 执行结点定义文件路径
graphFilePath string // 蓝图文件路径
}
func (bm *Blueprint) RegExecNode(execNode IExecNode) {
bm.execNodes = append(bm.execNodes, execNode)
func (bm *Blueprint) RegisterExecNode(execNodeFunc func() IExecNode) {
bm.execNodeList = append(bm.execNodeList, execNodeFunc)
}
func (bm *Blueprint) regSysNode(){
bm.RegExecNode(&AddInt{})
bm.RegExecNode(&SubInt{})
bm.RegExecNode(&MulInt{})
bm.RegExecNode(&DivInt{})
bm.RegExecNode(&ModInt{})
bm.RegExecNode(&RandNumber{})
bm.RegExecNode(&Entrance_ArrayParam{})
bm.RegExecNode(&Entrance_IntParam{})
bm.RegExecNode(&Entrance_Timer{})
bm.RegExecNode(&Output{})
bm.RegExecNode(&Sequence{})
bm.RegExecNode(&Foreach{})
bm.RegExecNode(&ForeachIntArray{})
bm.RegExecNode(&GetArrayInt{})
bm.RegExecNode(&GetArrayString{})
bm.RegExecNode(&GetArrayLen{})
bm.RegExecNode(&CreateIntArray{})
bm.RegExecNode(&CreateStringArray{})
bm.RegExecNode(&AppendIntegerToArray{})
bm.RegExecNode(&AppendStringToArray{})
bm.RegExecNode(&BoolIf{})
bm.RegExecNode(&GreaterThanInteger{})
bm.RegExecNode(&LessThanInteger{})
bm.RegExecNode(&EqualInteger{})
bm.RegExecNode(&RangeCompare{})
bm.RegExecNode(&Probability{})
bm.RegExecNode(&CreateTimer{})
type IExecNodeType[T any] interface {
*T
IExecNode
}
func (bm *Blueprint) Init(execDefFilePath string, graphFilePath string, blueprintModule IBlueprintModule,cancelTimer func(*uint64)bool) error {
bm.regSysNode()
err := bm.execPool.Load(execDefFilePath)
// 生成一个泛型函数返回func() IExecNode类型
func NewExecNode[T any, P IExecNodeType[T]]() func() IExecNode {
return func() IExecNode {
var t T
return P(&t)
}
}
func (bm *Blueprint) regSysNodes() {
bm.RegisterExecNode(NewExecNode[AddInt]())
bm.RegisterExecNode(NewExecNode[SubInt]())
bm.RegisterExecNode(NewExecNode[MulInt]())
bm.RegisterExecNode(NewExecNode[DivInt]())
bm.RegisterExecNode(NewExecNode[ModInt]())
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]())
bm.RegisterExecNode(NewExecNode[AppendIntReturn]())
bm.RegisterExecNode(NewExecNode[AppendStringReturn]())
}
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
}
func (bm *Blueprint) Init(execDefFilePath string, graphFilePath string, blueprintModule IBlueprintModule, cancelTimer func(*uint64) bool) error {
var execPool ExecPool
var graphPool GraphPool
// 加载配置结点生成名字对应的innerExecNode
err := execPool.Load(execDefFilePath)
if err != nil {
return err
}
for _, e := range bm.execNodes {
if !bm.execPool.Register(e) {
// 注册系统执行结点
bm.regSysNodes()
// 将注册的实际执行结点与innerExecNode进行关联
for _, newExec := range bm.execNodeList {
e := newExec()
if !execPool.Register(e) {
return fmt.Errorf("register exec failed,exec:%s", e.GetName())
}
}
err = bm.graphPool.Load(&bm.execPool, graphFilePath, blueprintModule)
// 加载所有的vgf蓝图文件
err = graphPool.Load(&execPool, graphFilePath, blueprintModule)
if err != nil {
return err
}
bm.execPool = &execPool
bm.graphPool = &graphPool
bm.cancelTimer = cancelTimer
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
}
@@ -82,32 +160,41 @@ func (bm *Blueprint) Create(graphName string) int64 {
if graphName == "" {
return 0
}
graphID := atomic.AddInt64(&bm.seedID, 1)
bm.mapGraph[graphID] = bm.graphPool.Create(graphName, graphID)
gr := bm.graphPool.Create(graphName, graphID)
if gr == nil {
return 0
}
bm.mapGraph[graphID] = gr
return graphID
}
func (bm *Blueprint) TriggerEvent(graphID int64, eventID int64, args ...any) error{
func (bm *Blueprint) TriggerEvent(graphID int64, eventID int64, args ...any) error {
graph := bm.mapGraph[graphID]
if graph == nil {
return fmt.Errorf("can not find graph:%d", graphID)
}
_,err:= graph.Do(eventID, args...)
_, err := graph.Do(eventID, args...)
return err
}
func (bm *Blueprint) Do(graphID int64, entranceID int64, args ...any) (Port_Array,error){
func (bm *Blueprint) Do(graphID int64, entranceID int64, args ...any) (Port_Array, error) {
graph := bm.mapGraph[graphID]
if graph == nil {
return nil,fmt.Errorf("can not find graph:%d", graphID)
return nil, fmt.Errorf("can not find graph:%d", graphID)
}
return graph.Do(entranceID, args...)
}
func (bm *Blueprint) ReleaseGraph(graphID int64) {
if graphID == 0 {
return
}
defer delete(bm.mapGraph, graphID)
graph := bm.mapGraph[graphID]
if graph == nil {
@@ -117,7 +204,7 @@ func (bm *Blueprint) ReleaseGraph(graphID int64) {
graph.Release()
}
func (bm *Blueprint) CancelTimerId(graphID int64, timerId *uint64) bool{
func (bm *Blueprint) CancelTimerId(graphID int64, timerId *uint64) bool {
tId := *timerId
bm.cancelTimer(timerId)
@@ -126,11 +213,11 @@ func (bm *Blueprint) CancelTimerId(graphID int64, timerId *uint64) bool{
return false
}
gr,ok := graph.(*Graph)
gr, ok := graph.(*Graph)
if !ok {
return false
}
delete(gr.mapTimerID, tId)
return true
}
}

View File

@@ -5,6 +5,11 @@ import (
)
func TestExecMgr(t *testing.T) {
var bp Blueprint
err := bp.Init("E:\\WorkSpace\\c4\\OriginNodeEditor\\json", "E:\\WorkSpace\\c4\\OriginNodeEditor\\vgf", nil, nil)
if err != nil {
t.Fatalf("Init failed,err:%v", err)
}
//graphTest2 := bp.Create("testForeach")
//err = graphTest2.Do(EntranceID_IntParam, 1, 2, 3)

View File

@@ -2,15 +2,7 @@ package blueprint
import "fmt"
type IBaseExecNode interface {
initInnerExecNode(innerNode *innerExecNode)
initExecNode(gr *Graph, en *execNode) error
GetPorts() ([]IPort, []IPort)
getExecNodeInfo() (*ExecContext, *execNode)
setExecNodeInfo(gr *ExecContext, en *execNode)
GetBlueprintModule() IBlueprintModule
}
// IInnerExecNode 配置生成的结点
type IInnerExecNode interface {
GetName() string
SetExec(exec IExecNode)
@@ -26,16 +18,27 @@ type IInnerExecNode interface {
GetOutPortParamStartIndex() int
}
// IBaseExecNode 实际注册的执行结点的基础结构体
type IBaseExecNode interface {
initInnerExecNode(innerNode *innerExecNode)
initExecNode(gr *Graph, en *execNode) error
GetPorts() ([]IPort, []IPort)
getExecNodeInfo() (*ExecContext, *execNode)
setExecNodeInfo(gr *ExecContext, en *execNode)
GetBlueprintModule() IBlueprintModule
}
// IExecNode 实际注册的执行结点
type IExecNode interface {
IBaseExecNode
GetName() string
DoNext(index int) error
Exec() (int, error) // 返回后续执行的Node的Index
GetNextExecLen() int
getInnerExecNode() IInnerExecNode
setVariableName(name string) bool
}
// 配置对应的基础信息+端口数据
type innerExecNode struct {
Name string
Title string
@@ -47,11 +50,11 @@ type innerExecNode struct {
outPortParamStartIndex int // 输出参数的起始索引,用于排除执行出口
IExecNode
IExecNode // 实际注册的执行结点
}
type BaseExecNode struct {
*innerExecNode
*innerExecNode // 内部注册的执行结点
// 执行时初始化的数据
*ExecContext
@@ -108,26 +111,12 @@ func (bc *BaseExecConfig) GetMaxOutPortId() int {
return maxPortId
}
//func (em *innerExecNode) AppendInPort(port ...IPort) {
// if len(em.inPort) == 0 {
// em.inPortParamStartIndex = -1
// }
//
// for i := 0; i < len(port); i++ {
// if !port[i].IsPortExec() && em.inPortParamStartIndex < 0 {
// em.inPortParamStartIndex = len(em.inPort)
// }
//
// em.inPort = append(em.inPort, port[i])
// }
//}
func (em *innerExecNode) PrepareMaxInPortId(maxInPortId int) {
em.inPort = make([]IPort, 0, maxInPortId+1)
em.inPort = make([]IPort, maxInPortId+1)
}
func (em *innerExecNode) PrepareMaxOutPortId(maxOutPortId int) {
em.outPort = make([]IPort, 0, maxOutPortId+1)
em.outPort = make([]IPort, maxOutPortId+1)
}
func (em *innerExecNode) SetInPortById(id int, port IPort) bool {
@@ -243,6 +232,10 @@ func (em *innerExecNode) GetOutPort(index int) IPort {
return em.outPort[index]
}
func (en *BaseExecNode) GetVariableName() string {
return en.execNode.variableName
}
func (en *BaseExecNode) GetBluePrintModule() IBlueprintModule {
return en.gr.IBlueprintModule
}
@@ -579,10 +572,6 @@ func (en *BaseExecNode) getInnerExecNode() IInnerExecNode {
return en.innerExecNode.IExecNode.(IInnerExecNode)
}
func (en *BaseExecNode) setVariableName(name string) bool {
return false
}
func (en *BaseExecNode) GetBlueprintModule() IBlueprintModule {
if en.gr == nil {
return nil

View File

@@ -5,19 +5,19 @@ import (
"fmt"
"os"
"path/filepath"
"sort"
"strconv"
"strings"
"sort"
)
// 格式说明Entrance_ID
// Entrance 格式:Entrance_XXXX_ID
const (
Entrance = "Entrance_"
)
type ExecPool struct {
innerExecNodeMap map[string]IInnerExecNode
execNodeMap map[string]IExecNode
innerExecNodeMap map[string]IInnerExecNode // 所有配置对应的结点信息
execNodeMap map[string]IExecNode // 实际注册的执行结点
}
func (em *ExecPool) Load(execDefFilePath string) error {
@@ -49,16 +49,19 @@ func (em *ExecPool) Load(execDefFilePath string) error {
// 只处理JSON文件
if filepath.Ext(path) == ".json" {
// 将配置的结点初始化为innerExecNode将加入到innerExecNodeMap中
return em.processJSONFile(path)
}
return nil
})
if err != nil {
return fmt.Errorf("failed to walk path %s: %v", execDefFilePath, err)
}
return em.loadSysExec()
// 生成变量配置对应的配置结点GetVar_类型、SetVar_类型
return em.regVariablesNode()
}
// 处理单个JSON文件
@@ -84,21 +87,24 @@ func (em *ExecPool) processJSONFile(filePath string) error {
}
for i := range baseExecConfig {
// 排序
// 对PortId进行排序
sort.Slice(baseExecConfig[i].Inputs, func(left, right int) bool {
return baseExecConfig[i].Inputs[left].PortId < baseExecConfig[i].Inputs[right].PortId
})
// 对PortId进行排序
sort.Slice(baseExecConfig[i].Outputs, func(left, right int) bool {
return baseExecConfig[i].Outputs[left].PortId < baseExecConfig[i].Outputs[right].PortId
})
exec, err := em.createExecFromJSON(baseExecConfig[i])
if err != nil {
return err
// 根据配置的结点信息创建innerExecNode
var execError error
exec, execError := em.createExecFromJSON(baseExecConfig[i])
if execError != nil {
return execError
}
if !em.loadBaseExec(exec) {
// 加载到innerExecNodeMap中
if !em.addInnerExec(exec) {
return fmt.Errorf("exec %s already registered", exec.GetName())
}
}
@@ -126,6 +132,7 @@ func (em *ExecPool) createPortByDataType(nodeName, portName, dataType string) (I
func (em *ExecPool) createExecFromJSON(baseExecConfig BaseExecConfig) (IInnerExecNode, error) {
var baseExec innerExecNode
// 如果是入口名则按入口名Entrance_ArrayParam_000002生成结点名:Entrance_ArrayParam
entranceName, _, ok := getEntranceNodeNameAndID(baseExecConfig.Name)
if ok {
baseExec.Name = entranceName
@@ -138,7 +145,7 @@ func (em *ExecPool) createExecFromJSON(baseExecConfig BaseExecConfig) (IInnerExe
baseExec.PrepareMaxInPortId(baseExecConfig.GetMaxInPortId())
baseExec.PrepareMaxOutPortId(baseExecConfig.GetMaxOutPortId())
// exec数量
// 初始化所有的输入端口
inExecNum := 0
for index, input := range baseExecConfig.Inputs {
portType := strings.ToLower(input.PortType)
@@ -146,6 +153,7 @@ func (em *ExecPool) createExecFromJSON(baseExecConfig BaseExecConfig) (IInnerExe
return nil, fmt.Errorf("input %s data type %s not support", input.Name, input.DataType)
}
// 输入执行结点只能有一个,且只能放在第一个
if portType == Config_PortType_Exec {
if inExecNum > 0 {
return nil, fmt.Errorf("inPort only allows one Execute,node name %s", baseExec.Name)
@@ -155,19 +163,22 @@ func (em *ExecPool) createExecFromJSON(baseExecConfig BaseExecConfig) (IInnerExe
}
inExecNum++
// 设置执行端口
baseExec.SetInPortById(input.PortId, NewPortExec())
// baseExec.AppendInPort(NewPortExec())
continue
}
// 根据类型设置对应的端口
port, err := em.createPortByDataType(baseExec.Name, input.Name, input.DataType)
if err != nil {
return nil, err
}
// 根据PortId设置端口
baseExec.SetInPortById(input.PortId, port)
}
// 初始化所有的输出端口
hasData := false
for _, output := range baseExecConfig.Outputs {
portType := strings.ToLower(output.PortType)
@@ -180,11 +191,13 @@ func (em *ExecPool) createExecFromJSON(baseExecConfig BaseExecConfig) (IInnerExe
return nil, fmt.Errorf("the exec port can only be placed at the front,node name %s", baseExec.Name)
}
// 设置执行端口
if portType == Config_PortType_Exec {
baseExec.SetOutPortById(output.PortId, NewPortExec())
continue
}
// 根据类型设置数据端口
hasData = true
port, err := em.createPortByDataType(baseExec.Name, output.Name, output.DataType)
if err != nil {
@@ -196,7 +209,7 @@ func (em *ExecPool) createExecFromJSON(baseExecConfig BaseExecConfig) (IInnerExe
return &baseExec, nil
}
func (em *ExecPool) loadBaseExec(exec IInnerExecNode) bool {
func (em *ExecPool) addInnerExec(exec IInnerExecNode) bool {
if _, ok := em.innerExecNodeMap[exec.GetName()]; ok {
return false
}
@@ -224,10 +237,14 @@ func (em *ExecPool) Register(exec IExecNode) bool {
return false
}
// 设置实际执行结点中innerExecNode变量,BaseExecNode.innerExecNode = innerNode
baseExecNode.initInnerExecNode(innerNode.(*innerExecNode))
// innerNode设置实际的exec变量,innerExecNode.IExecNode = exec
innerNode.SetExec(exec)
em.execNodeMap[baseExec.GetName()] = baseExec
// 将实际的执行结点保存到execNodeMap中
em.execNodeMap[baseExec.GetName()] = exec
return true
}
@@ -238,7 +255,8 @@ func (em *ExecPool) GetExec(name string) IInnerExecNode {
return nil
}
func (em *ExecPool) loadSysExec() error {
// regVariablesNode 注册变量结点GetVar_类型、SetVar_类型
func (em *ExecPool) regVariablesNode() error {
var err error
if err = em.regGetVariables(Config_DataType_Int); err != nil {
return err
@@ -300,7 +318,7 @@ func (em *ExecPool) regGetVariables(typ string) error {
var getVariablesNode GetVariablesNode
getVariablesNode.nodeName = baseExec.GetName()
if !em.loadBaseExec(&baseExec) {
if !em.addInnerExec(&baseExec) {
return fmt.Errorf("exec %s already registered", baseExec.GetName())
}
if !em.Register(&getVariablesNode) {
@@ -336,7 +354,7 @@ func (em *ExecPool) regSetVariables(typ string) error {
baseExec.SetOutPortById(1, outPort)
baseExec.IExecNode = &SetVariablesNode{nodeName: baseExec.GetName()}
if !em.loadBaseExec(&baseExec) {
if !em.addInnerExec(&baseExec) {
return fmt.Errorf("exec %s already registered", baseExec.GetName())
}
if !em.Register(baseExec.IExecNode) {

View File

@@ -4,15 +4,20 @@ import (
"fmt"
"time"
"github.com/duanhf2012/origin/v2/log"
"github.com/duanhf2012/origin/v2/service"
"github.com/goccy/go-json"
)
const ReturnVarial = "g_Return"
var IsDebug = false
type IGraph interface {
Do(entranceID int64, args ...any) (Port_Array, error)
Release()
GetGraphFileName() string
HotReload(newBaseGraph *baseGraph)
}
type IBlueprintModule interface {
@@ -28,7 +33,8 @@ type baseGraph struct {
}
type Graph struct {
graphID int64
graphFileName string
graphID int64
*baseGraph
graphContext
IBlueprintModule
@@ -136,15 +142,34 @@ func (gc *graphConfig) GetNodeByID(nodeID string) *nodeConfig {
return nil
}
func (gr *Graph) GetAndCreateReturnPort() IPort {
p, ok := gr.globalVariables[ReturnVarial]
if ok && p != nil {
return p
}
p = NewPortArray()
gr.globalVariables[ReturnVarial] = p
return p
}
func (gr *Graph) Do(entranceID int64, args ...any) (Port_Array, error) {
if IsDebug {
log.Debug("Graph Do", log.String("graphName", gr.graphFileName), log.Int64("graphID", gr.graphID), log.Int64("entranceID", entranceID))
}
entranceNode := gr.entrance[entranceID]
if entranceNode == nil {
return nil, fmt.Errorf("entranceID:%d not found", entranceID)
}
gr.variables = map[string]IPort{}
gr.context = map[string]*ExecContext{}
if gr.globalVariables == nil {
gr.globalVariables = map[string]IPort{}
}else {
gr.globalVariables[ReturnVarial] = nil
}
err := entranceNode.Do(gr, args...)
@@ -196,3 +221,11 @@ func (gr *Graph) Release() {
// 清理掉所有数据
*gr = Graph{}
}
func (gr *Graph) HotReload(newBaseGraph *baseGraph) {
gr.baseGraph = newBaseGraph
}
func (gr *Graph) GetGraphFileName() string{
return gr.graphFileName
}

View File

@@ -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 {
gr, ok := gp.mapGraphs[graphName]
if !ok {
@@ -61,6 +70,7 @@ func (gp *GraphPool) Create(graphName string, graphID int64) IGraph {
var graph Graph
graph.baseGraph = gr
graph.graphID = graphID
graph.graphFileName = graphName
graph.context = make(map[string]*ExecContext, 4)
graph.IBlueprintModule = gp.blueprintModule
return &graph
@@ -72,21 +82,25 @@ func (gp *GraphPool) processJSONFile(filePath string) error {
if err != nil {
return fmt.Errorf("failed to open file %s: %v", filePath, err)
}
defer func() {
if err := file.Close(); err != nil {
if err = file.Close(); err != nil {
fmt.Printf("关闭文件 %s 时出错: %v\n", filePath, err)
}
}()
fileName := filepath.Base(filePath)
ext := filepath.Ext(fileName) // 获取".html"
ext := filepath.Ext(fileName) // 获取".vgf"
name := strings.TrimSuffix(fileName, ext) // 获取"name"
// 解析文件
var gConfig graphConfig
decoder := json.NewDecoder(file)
if err := decoder.Decode(&gConfig); err != nil {
if err = decoder.Decode(&gConfig); err != nil {
return fmt.Errorf("failed to decode JSON from file %s: %v", filePath, err)
}
// 预处理蓝图
return gp.prepareGraph(name, &gConfig)
}
@@ -137,8 +151,6 @@ func (gp *GraphPool) genVarExec(nodeCfg *nodeConfig, graphConfig *graphConfig) (
}
e := gp.execPool.GetExec(nodeName)
e.(IExecNode).setVariableName(varName)
return e, varName
}
@@ -174,7 +186,7 @@ func (gp *GraphPool) genAllNode(graphConfig *graphConfig) (map[string]*execNode,
func (gp *GraphPool) prepareOneNode(mapNodeExec map[string]*execNode, nodeExec *execNode, graphConfig *graphConfig, recursion *int) error {
*recursion++
if *recursion > 100 {
if *recursion > 256 {
return fmt.Errorf("recursion too deep")
}

View File

@@ -2,6 +2,7 @@ package blueprint
import (
"fmt"
"github.com/duanhf2012/origin/v2/log"
)
type prePortNode struct {
@@ -102,20 +103,6 @@ func (en *execNode) exec(gr *Graph) (int, error) {
return -1, err
}
//defer func() {
inPort, outPort := node.GetPorts()
debugString := "inPort:"
for i := 0; i < len(inPort); i++ {
debugString += fmt.Sprintf("%+v,", inPort[i])
}
debugString += " outPort:"
for i := 0; i < len(outPort); i++ {
debugString += fmt.Sprintf("%+v,", outPort[i])
}
fmt.Printf("exec node %s,%s\n", en.execNode.GetName(), debugString)
//}()
return e.Exec()
}
@@ -155,6 +142,10 @@ func (en *execNode) doSetInPort(gr *Graph, index int, inPort IPort) error {
}
func (en *execNode) Do(gr *Graph, outPortArgs ...any) error {
if IsDebug {
log.Debug("Start ExecNode", log.String("Name",en.execNode.GetName()))
}
// 重新初始化上下文
inPorts, outPorts := en.execNode.CloneInOutPort()
gr.context[en.Id] = &ExecContext{
@@ -164,7 +155,7 @@ func (en *execNode) Do(gr *Graph, outPortArgs ...any) error {
startOutIdx := en.execNode.GetOutPortParamStartIndex()
for i := 0; i < len(outPortArgs); i++ {
if i >= len(outPorts) {
if i+startOutIdx >= len(outPorts) {
return fmt.Errorf("args %d not found in node %s", i, en.execNode.GetName())
}
@@ -194,6 +185,10 @@ func (en *execNode) Do(gr *Graph, outPortArgs ...any) error {
return err
}
if IsDebug {
log.Debug("End ExecNode", log.String("Name",en.execNode.GetName()),log.Any("InPort",inPorts ),log.Any("OutPort",outPorts))
}
if nextIndex == -1 || en.nextNode == nil {
return nil
}

View File

@@ -213,7 +213,7 @@ func (em *Port[T]) convertInt64(v any) (int64, bool) {
func (em *Port[T]) setAnyVale(v any) error {
switch v.(type) {
case int, int64:
case int8,int16,int32,int, int64,uint8,uint16,uint32,uint, uint64:
val, ok := em.convertInt64(v)
if !ok {
return fmt.Errorf("port type is %T, but value is %v", em.PortVal, v)

View File

@@ -1,4 +0,0 @@
package blueprint

View File

@@ -15,8 +15,6 @@ const (
EntranceID_Timer = 3
)
type Entrance_ArrayParam struct {
BaseExecNode
}
@@ -53,15 +51,15 @@ func (em *Entrance_Timer) Exec() (int, error) {
return 0, nil
}
type Output struct {
type DebugOutput struct {
BaseExecNode
}
func (em *Output) GetName() string {
return "Output"
func (em *DebugOutput) GetName() string {
return "DebugOutput"
}
func (em *Output) Exec() (int, error) {
func (em *DebugOutput) Exec() (int, error) {
val, ok := em.GetInPortInt(1)
if !ok {
return 0, fmt.Errorf("output Exec inParam not found")
@@ -77,7 +75,7 @@ func (em *Output) Exec() (int, error) {
return 0, fmt.Errorf("output Exec inParam not found")
}
fmt.Printf("output Exec inParam [%d] [%s] [%v]\n", val, valStr, valArray)
log.Debug("DebugOutput Exec", log.Any("param1", val), log.Any("param2", valStr), log.Any("param3", valArray))
return 0, nil
}
@@ -119,7 +117,8 @@ func (em *ForeachIntArray) Exec() (int, error) {
}
for i := range array {
em.ExecContext.OutputPorts[2].SetInt(array[i].IntVal)
em.ExecContext.OutputPorts[2].SetInt(Port_Int(i))
em.ExecContext.OutputPorts[3].SetInt(array[i].IntVal)
err := em.DoNext(0)
if err != nil {
return -1, err
@@ -471,6 +470,40 @@ func (em *RangeCompare) Exec() (int, error) {
return 0, nil
}
// EqualSwitch 等于分支==
type EqualSwitch struct {
BaseExecNode
}
func (em *EqualSwitch) GetName() string {
return "EqualSwitch"
}
func (em *EqualSwitch) Exec() (int, error) {
inPortA := em.GetInPort(1)
if inPortA == nil {
return -1, fmt.Errorf("GreaterThanInteger inParam 1 not found")
}
ret, ok := inPortA.GetInt()
if !ok {
return -1, fmt.Errorf("GreaterThanInteger inParam 1 error")
}
intArray := em.execNode.GetInPortDefaultIntArrayValue(2)
if intArray == nil {
return 0, nil
}
for i := 0; i < len(intArray) && i < em.GetOutPortCount()-2; i++ {
if ret == intArray[i] {
return i + 2, nil
}
}
return 0, nil
}
// Probability 概率判断(万分比)
type Probability struct {
BaseExecNode
@@ -648,7 +681,7 @@ func (em *CreateTimer) Exec() (int, error) {
array, ok := em.GetInPortArray(1)
if !ok {
return -1, fmt.Errorf("CreateTimer inParam 0 error")
return -1, fmt.Errorf("CreateTimer inParam 1 error")
}
var timerId uint64
@@ -685,7 +718,7 @@ func (em *CloseTimer) GetName() string {
func (em *CloseTimer) Exec() (int, error) {
timerID, ok := em.GetInPortInt(1)
if !ok {
return -1, fmt.Errorf("CreateTimer inParam 0 error")
return -1, fmt.Errorf("CreateTimer inParam 1 error")
}
id := uint64(timerID)
@@ -696,3 +729,56 @@ func (em *CloseTimer) Exec() (int, error) {
return 0, nil
}
// AppendIntReturn 追加返回结果(Int)
type AppendIntReturn struct {
BaseExecNode
}
func (em *AppendIntReturn) GetName() string {
return "AppendIntReturn"
}
func (em *AppendIntReturn) Exec() (int, error) {
val, ok := em.GetInPortInt(1)
if !ok {
return -1, fmt.Errorf("AppendIntReturn inParam 1 error")
}
returnPort := em.gr.GetAndCreateReturnPort()
if returnPort == nil {
return -1, fmt.Errorf("GetAndCreateReturnPort fail")
}
returnPort.AppendArrayValInt(val)
return -0, nil
}
// AppendStringReturn 追加返回结果(String)
type AppendStringReturn struct {
BaseExecNode
}
func (em *AppendStringReturn) GetName() string {
return "AppendStringReturn"
}
func (em *AppendStringReturn) Exec() (int, error) {
val, ok := em.GetInPortStr(1)
if !ok {
return -1, fmt.Errorf("AppendStringReturn inParam 1 error")
}
returnPort := em.gr.GetAndCreateReturnPort()
if returnPort == nil {
return -1, fmt.Errorf("GetAndCreateReturnPort fail")
}
returnPort.AppendArrayValStr(val)
return -0, nil
}

View File

@@ -12,13 +12,11 @@ const globalVariablesPrefix = "g_"
type GetVariablesNode struct {
BaseExecNode
nodeName string
varName string
}
type SetVariablesNode struct {
BaseExecNode
nodeName string
varName string
}
func (g *GetVariablesNode) GetName() string {
@@ -27,14 +25,14 @@ func (g *GetVariablesNode) GetName() string {
func (g *GetVariablesNode) Exec() (int, error) {
var port IPort
if strings.HasPrefix(g.varName, globalVariablesPrefix) {
port = g.gr.globalVariables[g.varName]
if strings.HasPrefix(g.GetVariableName(), globalVariablesPrefix) {
port = g.gr.globalVariables[g.GetVariableName()]
} else {
port = g.gr.variables[g.varName]
port = g.gr.variables[g.GetVariableName()]
}
if port == nil {
return -1, fmt.Errorf("variable %s not found,node name %s", g.varName, g.nodeName)
return -1, fmt.Errorf("variable %s not found,node name %s", g.GetVariableName(), g.nodeName)
}
if !g.SetOutPort(0, port) {
@@ -44,10 +42,7 @@ func (g *GetVariablesNode) Exec() (int, error) {
return 0, nil
}
func (g *GetVariablesNode) setVariableName(name string) bool {
g.varName = name
return true
}
func (g *SetVariablesNode) GetName() string {
return g.nodeName
@@ -60,10 +55,10 @@ func (g *SetVariablesNode) Exec() (int, error) {
}
varPort := port.Clone()
if strings.HasPrefix(g.varName, globalVariablesPrefix) {
g.gr.globalVariables[g.varName] = varPort
if strings.HasPrefix(g.GetVariableName(), globalVariablesPrefix) {
g.gr.globalVariables[g.GetVariableName()] = varPort
} else {
g.gr.variables[g.varName] = varPort
g.gr.variables[g.GetVariableName()] = varPort
}
if !g.SetOutPort(1, varPort) {
@@ -73,7 +68,3 @@ func (g *SetVariablesNode) Exec() (int, error) {
return 0, nil
}
func (g *SetVariablesNode) setVariableName(name string) bool {
g.varName = name
return true
}