mirror of
https://github.com/duanhf2012/origin.git
synced 2026-02-05 15:34:49 +08:00
Compare commits
72 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6f87cda07f | ||
|
|
2e4863d073 | ||
|
|
330644cebb | ||
|
|
ef7ee0ab8e | ||
|
|
976efe0c04 | ||
|
|
b61906fb24 | ||
|
|
29dcf2bfeb | ||
|
|
857e3c4311 | ||
|
|
210a3fa982 | ||
|
|
d365dde8c0 | ||
|
|
e0d412810f | ||
|
|
593abd263e | ||
|
|
75660bdec0 | ||
|
|
eaf20c4e3a | ||
|
|
027e83b706 | ||
|
|
f9be55e98d | ||
|
|
d7c4cfb1ef | ||
|
|
4cb6882a1a | ||
|
|
b78d9721f2 | ||
|
|
f8953d1764 | ||
|
|
fac7a323e1 | ||
|
|
1995d91cfc | ||
|
|
21e9b2cd4b | ||
|
|
969fbe818c | ||
|
|
f3ea9d7c7f | ||
|
|
70389b644d | ||
|
|
08effd5bca | ||
|
|
be91bcd4b5 | ||
|
|
f22ee230e4 | ||
|
|
419e7ee0c4 | ||
|
|
7a34fafdc8 | ||
|
|
640b61bcdb | ||
|
|
654426a836 | ||
|
|
c6488faeff | ||
|
|
3bf19ed329 | ||
|
|
d4c0bd22ad | ||
|
|
6511fc4ac0 | ||
|
|
be0078015f | ||
|
|
2a12d40f7a | ||
|
|
77e2986ffb | ||
|
|
3bcce31a86 | ||
|
|
a54b3c59fc | ||
|
|
6c44ba180c | ||
|
|
ecfd42bdec | ||
|
|
01d3b3e535 | ||
|
|
550d65a354 | ||
|
|
15580ffce9 | ||
|
|
bd467a219b | ||
|
|
af15615345 | ||
|
|
50dd80b082 | ||
|
|
a6487dd41e | ||
|
|
d5299294d8 | ||
|
|
4d36e525a5 | ||
|
|
3a4350769c | ||
|
|
d4966ea129 | ||
|
|
3b10eeb792 | ||
|
|
e3275e9f2a | ||
|
|
16745b34f0 | ||
|
|
f34dc7d53f | ||
|
|
0a09dc2fee | ||
|
|
f01a93c446 | ||
|
|
4d2ab4ee4f | ||
|
|
ffcc5a3489 | ||
|
|
cf6ca0483b | ||
|
|
97a21e6f71 | ||
|
|
f60a55d03a | ||
|
|
2c32d6eec9 | ||
|
|
da45f97fa8 | ||
|
|
d29abc0813 | ||
|
|
c9507f9ee9 | ||
|
|
61de4bba3a | ||
|
|
000853b479 |
74
README.md
74
README.md
@@ -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连接超时时间
|
||||
|
||||
EtcdList:Etcd列表,可以多个Etcd服务器连接
|
||||
EtcdList:Etcd列表,可以多个Etcd服务器连接(注意:列表中必需有一个LocalNetworkName项,表示当前所有的Node归属当前网络名为network_Area1)Node下所有的服务会往network_Area1中注册。监听该网络的结点可以发现该网络中的Service。本地网络会默认监听本地网络中所有的服务。
|
||||
|
||||
NetworkName:所在的网络名称,可以配置多个。node会往对应的网络名称中注册、监听发现Service。NetworkName也起到发现隔离的作用。
|
||||
NeighborNetworkName:表示监听的邻居网络名,可以发现该网络中所有Service
|
||||
|
||||
Endpoints:Etcd服务器地址
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
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发现模式,则表示可以发现网络名networkname1,NodeId为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使用
|
||||
-----------------------
|
||||
|
||||
@@ -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,25 +26,25 @@ 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
|
||||
|
||||
NetworkName string
|
||||
}
|
||||
|
||||
type NodeRpcInfo struct {
|
||||
@@ -224,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))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -250,7 +253,7 @@ func (cls *Cluster) Init(localNodeId string, setupServiceFun SetupServiceFun) er
|
||||
//2.安装服务发现结点
|
||||
err = cls.setupDiscovery(localNodeId, setupServiceFun)
|
||||
if err != nil {
|
||||
log.Error("setupDiscovery fail", log.ErrorAttr("err", err))
|
||||
log.Error("setupDiscovery fail", log.ErrorField("err", err))
|
||||
return err
|
||||
}
|
||||
service.RegRpcEventFun = cls.RegRpcEvent
|
||||
@@ -408,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 {
|
||||
@@ -422,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
|
||||
}
|
||||
}
|
||||
|
||||
@@ -463,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
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -3,24 +3,23 @@ package cluster
|
||||
import "github.com/duanhf2012/origin/v2/rpc"
|
||||
|
||||
type ConfigDiscovery struct {
|
||||
funDelNode FunDelNode
|
||||
funSetNode FunSetNode
|
||||
funDelNode FunDelNode
|
||||
funSetNode FunSetNode
|
||||
localNodeId string
|
||||
}
|
||||
|
||||
|
||||
func (discovery *ConfigDiscovery) InitDiscovery(localNodeId string,funDelNode FunDelNode,funSetNode FunSetNode) error{
|
||||
func (discovery *ConfigDiscovery) InitDiscovery(localNodeId string, funDelNode FunDelNode, funSetNode FunSetNode) error {
|
||||
discovery.localNodeId = localNodeId
|
||||
discovery.funDelNode = funDelNode
|
||||
discovery.funSetNode = funSetNode
|
||||
|
||||
|
||||
//解析本地其他服务配置
|
||||
_,nodeInfoList,_,err := GetCluster().readLocalClusterConfig(rpc.NodeIdNull)
|
||||
_, nodeInfoList, _, err := GetCluster().readLocalClusterConfig(rpc.NodeIdNull)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _,nodeInfo := range nodeInfoList {
|
||||
for _, nodeInfo := range nodeInfoList {
|
||||
if nodeInfo.NodeId == localNodeId {
|
||||
continue
|
||||
}
|
||||
@@ -30,5 +29,3 @@ func (discovery *ConfigDiscovery) InitDiscovery(localNodeId string,funDelNode Fu
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -5,17 +5,17 @@ import (
|
||||
"github.com/duanhf2012/origin/v2/service"
|
||||
)
|
||||
|
||||
func (cls *Cluster) setupDiscovery(localNodeId string, setupServiceFun SetupServiceFun) error{
|
||||
func (cls *Cluster) setupDiscovery(localNodeId string, setupServiceFun SetupServiceFun) error {
|
||||
if cls.discoveryInfo.getDiscoveryType() == OriginType { //origin类型服务发现
|
||||
return cls.setupOriginDiscovery(localNodeId,setupServiceFun)
|
||||
}else if cls.discoveryInfo.getDiscoveryType() == EtcdType{//etcd类型服务发现
|
||||
return cls.setupEtcdDiscovery(localNodeId,setupServiceFun)
|
||||
return cls.setupOriginDiscovery(localNodeId, setupServiceFun)
|
||||
} else if cls.discoveryInfo.getDiscoveryType() == EtcdType { //etcd类型服务发现
|
||||
return cls.setupEtcdDiscovery(localNodeId, setupServiceFun)
|
||||
}
|
||||
|
||||
return cls.setupConfigDiscovery(localNodeId,setupServiceFun)
|
||||
return cls.setupConfigDiscovery(localNodeId, setupServiceFun)
|
||||
}
|
||||
|
||||
func (cls *Cluster) setupOriginDiscovery(localNodeId string, setupServiceFun SetupServiceFun) error{
|
||||
func (cls *Cluster) setupOriginDiscovery(localNodeId string, setupServiceFun SetupServiceFun) error {
|
||||
if cls.serviceDiscovery != nil {
|
||||
return errors.New("service discovery has been setup")
|
||||
}
|
||||
@@ -27,6 +27,7 @@ func (cls *Cluster) setupOriginDiscovery(localNodeId string, setupServiceFun Set
|
||||
}
|
||||
|
||||
cls.serviceDiscovery = getOriginDiscovery()
|
||||
|
||||
//2.如果为动态服务发现安装本地发现服务
|
||||
if localMaster == true {
|
||||
setupServiceFun(&masterService)
|
||||
@@ -36,11 +37,10 @@ func (cls *Cluster) setupOriginDiscovery(localNodeId string, setupServiceFun Set
|
||||
setupServiceFun(&clientService)
|
||||
cls.AddDiscoveryService(OriginDiscoveryClientName, true)
|
||||
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (cls *Cluster) setupEtcdDiscovery(localNodeId string, setupServiceFun SetupServiceFun) error{
|
||||
func (cls *Cluster) setupEtcdDiscovery(localNodeId string, setupServiceFun SetupServiceFun) error {
|
||||
if cls.serviceDiscovery != nil {
|
||||
return errors.New("service discovery has been setup")
|
||||
}
|
||||
@@ -48,12 +48,12 @@ func (cls *Cluster) setupEtcdDiscovery(localNodeId string, setupServiceFun Setup
|
||||
//setup etcd service
|
||||
cls.serviceDiscovery = getEtcdDiscovery()
|
||||
setupServiceFun(cls.serviceDiscovery.(service.IService))
|
||||
|
||||
cls.AddDiscoveryService(cls.serviceDiscovery.(service.IService).GetName(),false)
|
||||
|
||||
cls.AddDiscoveryService(cls.serviceDiscovery.(service.IService).GetName(), false)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (cls *Cluster) setupConfigDiscovery(localNodeId string, setupServiceFun SetupServiceFun) error{
|
||||
func (cls *Cluster) setupConfigDiscovery(localNodeId string, setupServiceFun SetupServiceFun) error {
|
||||
if cls.serviceDiscovery != nil {
|
||||
return errors.New("service discovery has been setup")
|
||||
}
|
||||
|
||||
@@ -1,6 +1,17 @@
|
||||
package cluster
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/tls"
|
||||
"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"
|
||||
@@ -9,24 +20,15 @@ import (
|
||||
"go.etcd.io/etcd/api/v3/mvccpb"
|
||||
"go.etcd.io/etcd/client/v3"
|
||||
"google.golang.org/protobuf/proto"
|
||||
"time"
|
||||
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"go.uber.org/zap"
|
||||
"path"
|
||||
"runtime"
|
||||
"strings"
|
||||
"sync/atomic"
|
||||
)
|
||||
|
||||
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 {
|
||||
@@ -42,8 +44,13 @@ type EtcdDiscoveryService struct {
|
||||
mapDiscoveryNodeId map[string]map[string]struct{} //map[networkName]map[nodeId]
|
||||
}
|
||||
|
||||
var etcdDiscovery *EtcdDiscoveryService
|
||||
|
||||
func getEtcdDiscovery() IServiceDiscovery {
|
||||
etcdDiscovery := &EtcdDiscoveryService{}
|
||||
if etcdDiscovery == nil {
|
||||
etcdDiscovery = &EtcdDiscoveryService{}
|
||||
}
|
||||
|
||||
return etcdDiscovery
|
||||
}
|
||||
|
||||
@@ -88,33 +95,74 @@ func (ed *EtcdDiscoveryService) OnInit() error {
|
||||
return errors.New("etcd discovery config is nil.")
|
||||
}
|
||||
|
||||
var hasLocalNetwork bool
|
||||
for i := 0; i < len(etcdDiscoveryCfg.EtcdList); i++ {
|
||||
client, cerr := clientv3.New(clientv3.Config{
|
||||
var client *clientv3.Client
|
||||
var tlsConfig *tls.Config
|
||||
|
||||
if etcdDiscoveryCfg.EtcdList[i].Cert != "" {
|
||||
// load cert
|
||||
cert, cErr := tls.LoadX509KeyPair(etcdDiscoveryCfg.EtcdList[i].Cert, etcdDiscoveryCfg.EtcdList[i].CertKey)
|
||||
if cErr != nil {
|
||||
log.Error("load cert error", log.ErrorField("err", cErr))
|
||||
return cErr
|
||||
}
|
||||
|
||||
// load root ca
|
||||
caData, cErr := os.ReadFile(etcdDiscoveryCfg.EtcdList[i].Ca)
|
||||
if cErr != nil {
|
||||
log.Error("load root ca error", log.ErrorField("err", cErr))
|
||||
return cErr
|
||||
}
|
||||
pool := x509.NewCertPool()
|
||||
pool.AppendCertsFromPEM(caData)
|
||||
tlsConfig = &tls.Config{
|
||||
Certificates: []tls.Certificate{cert},
|
||||
RootCAs: pool,
|
||||
}
|
||||
}
|
||||
|
||||
client, err = clientv3.New(clientv3.Config{
|
||||
Endpoints: etcdDiscoveryCfg.EtcdList[i].Endpoints,
|
||||
DialTimeout: etcdDiscoveryCfg.DialTimeoutMillisecond,
|
||||
Logger: zap.NewNop(),
|
||||
Username: etcdDiscoveryCfg.EtcdList[i].UserName,
|
||||
Password: etcdDiscoveryCfg.EtcdList[i].Password,
|
||||
Logger: log.GetLogger().Logger,
|
||||
TLS: tlsConfig,
|
||||
})
|
||||
|
||||
if cerr != nil {
|
||||
log.Error("etcd discovery init fail", log.ErrorAttr("err", cerr))
|
||||
return cerr
|
||||
if err != nil {
|
||||
log.Error("etcd discovery init fail", log.ErrorField("err", err))
|
||||
return err
|
||||
}
|
||||
|
||||
ctx, _ := context.WithTimeout(context.Background(), time.Second*3)
|
||||
_, err = client.Leases(ctx)
|
||||
if err != nil {
|
||||
log.Error("etcd discovery init fail", log.Any("endpoint", etcdDiscoveryCfg.EtcdList[i].Endpoints), log.ErrorAttr("err", err))
|
||||
log.Error("etcd discovery init fail", log.Any("endpoint", etcdDiscoveryCfg.EtcdList[i].Endpoints), log.ErrorField("err", err))
|
||||
return err
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
@@ -128,25 +176,29 @@ func (ed *EtcdDiscoveryService) registerServiceByClient(client *clientv3.Client,
|
||||
var resp *clientv3.LeaseGrantResponse
|
||||
resp, err = client.Grant(context.Background(), cluster.GetEtcdDiscovery().TTLSecond)
|
||||
if err != nil {
|
||||
log.Error("etcd registerService fail", log.ErrorAttr("err", err))
|
||||
log.Error("etcd registerService fail", log.ErrorField("err", err))
|
||||
ed.tryRegisterService(client, etcdClient)
|
||||
return
|
||||
}
|
||||
|
||||
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.ErrorAttr("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)
|
||||
if err != nil {
|
||||
log.Error("etcd KeepAlive fail", log.ErrorAttr("err", err))
|
||||
log.Error("etcd KeepAlive fail", log.ErrorField("err", err))
|
||||
ed.tryRegisterService(client, etcdClient)
|
||||
return
|
||||
}
|
||||
@@ -171,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)
|
||||
})
|
||||
@@ -196,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.ErrorAttr("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
|
||||
}
|
||||
}
|
||||
|
||||
@@ -259,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)
|
||||
}
|
||||
}
|
||||
@@ -285,12 +346,12 @@ func (ed *EtcdDiscoveryService) setNodeInfo(networkName string, nodeInfo *rpc.No
|
||||
func (ed *EtcdDiscoveryService) close() {
|
||||
for c, ec := range ed.mapClient {
|
||||
if _, err := c.Revoke(context.Background(), ec.leaseID); err != nil {
|
||||
log.Error("etcd Revoke fail", log.ErrorAttr("err", err))
|
||||
log.Error("etcd Revoke fail", log.ErrorField("err", err))
|
||||
}
|
||||
c.Watcher.Close()
|
||||
err := c.Close()
|
||||
if err != nil {
|
||||
log.Error("etcd Close fail", log.ErrorAttr("err", err))
|
||||
log.Error("etcd Close fail", log.ErrorField("err", err))
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -299,7 +360,7 @@ func (ed *EtcdDiscoveryService) getServices(client *clientv3.Client, etcdClient
|
||||
// 根据前缀获取现有的key
|
||||
resp, err := client.Get(context.Background(), watchKey, clientv3.WithPrefix())
|
||||
if err != nil {
|
||||
log.Error("etcd Get fail", log.ErrorAttr("err", err))
|
||||
log.Error("etcd Get fail", log.ErrorField("err", err))
|
||||
ed.tryWatch(client, etcdClient)
|
||||
return false
|
||||
}
|
||||
@@ -322,11 +383,7 @@ func (ed *EtcdDiscoveryService) watchByClient(client *clientv3.Client, etcdClien
|
||||
func (ed *EtcdDiscoveryService) watcher(client *clientv3.Client, etcdClient *etcdClientInfo, watchKey string) {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
buf := make([]byte, 4096)
|
||||
l := runtime.Stack(buf, false)
|
||||
errString := fmt.Sprint(r)
|
||||
log.Dump(string(buf[:l]), log.String("error", errString))
|
||||
|
||||
log.StackError(fmt.Sprint(r))
|
||||
ed.tryWatch(client, etcdClient)
|
||||
}
|
||||
}()
|
||||
@@ -355,7 +412,7 @@ func (ed *EtcdDiscoveryService) setNode(netWorkName string, byteNode []byte) str
|
||||
var nodeInfo rpc.NodeInfo
|
||||
err := proto.Unmarshal(byteNode, &nodeInfo)
|
||||
if err != nil {
|
||||
log.Error("Unmarshal fail", log.String("netWorkName", netWorkName), log.ErrorAttr("err", err))
|
||||
log.Error("Unmarshal fail", log.String("netWorkName", netWorkName), log.ErrorField("err", err))
|
||||
return ""
|
||||
}
|
||||
|
||||
@@ -474,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
|
||||
}
|
||||
}
|
||||
|
||||
@@ -494,7 +556,7 @@ func (ed *EtcdDiscoveryService) RPC_ServiceRecord(etcdServiceRecord *service.Etc
|
||||
ctx, _ := context.WithTimeout(context.Background(), time.Second*3)
|
||||
lg, err = client.Grant(ctx, etcdServiceRecord.TTLSecond)
|
||||
if err != nil {
|
||||
log.Error("etcd record fail,cannot grant lease", log.ErrorAttr("err", err))
|
||||
log.Error("etcd record fail,cannot grant lease", log.ErrorField("err", err))
|
||||
return errors.New("cannot grant lease")
|
||||
}
|
||||
}
|
||||
@@ -503,14 +565,14 @@ func (ed *EtcdDiscoveryService) RPC_ServiceRecord(etcdServiceRecord *service.Etc
|
||||
ctx, _ := context.WithTimeout(context.Background(), time.Second*3)
|
||||
_, err = client.Put(ctx, path.Join(originDir, etcdServiceRecord.RecordKey), etcdServiceRecord.RecordInfo, clientv3.WithLease(lg.ID))
|
||||
if err != nil {
|
||||
log.Error("etcd record fail,cannot put record", log.ErrorAttr("err", err))
|
||||
log.Error("etcd record fail,cannot put record", log.ErrorField("err", err))
|
||||
}
|
||||
return errors.New("cannot put record")
|
||||
}
|
||||
|
||||
_, err = client.Put(context.Background(), path.Join(originDir, etcdServiceRecord.RecordKey), etcdServiceRecord.RecordInfo)
|
||||
if err != nil {
|
||||
log.Error("etcd record fail,cannot put record", log.ErrorAttr("err", err))
|
||||
log.Error("etcd record fail,cannot put record", log.ErrorField("err", err))
|
||||
return errors.New("cannot put record")
|
||||
}
|
||||
|
||||
|
||||
@@ -471,7 +471,7 @@ func (dc *OriginDiscoveryClient) OnRelease() {
|
||||
|
||||
err := dc.CallNodeWithTimeout(3*time.Second, masterNodeList.MasterNodeList[i].NodeId, UnRegServiceDiscover, &nodeRetireReq, &rpc.Empty{})
|
||||
if err != nil {
|
||||
log.Error("call "+UnRegServiceDiscover+" is fail", log.ErrorAttr("err", err))
|
||||
log.Error("call "+UnRegServiceDiscover+" is fail", log.ErrorField("err", err))
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -493,7 +493,7 @@ func (dc *OriginDiscoveryClient) OnRetire() {
|
||||
|
||||
err := dc.GoNode(masterNodeList.MasterNodeList[i].NodeId, NodeRetireRpcMethod, &nodeRetireReq)
|
||||
if err != nil {
|
||||
log.Error("call "+NodeRetireRpcMethod+" is fail", log.ErrorAttr("err", err))
|
||||
log.Error("call "+NodeRetireRpcMethod+" is fail", log.ErrorField("err", err))
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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]
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"github.com/duanhf2012/origin/v2/rpc"
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
"gopkg.in/yaml.v3"
|
||||
"io/fs"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
@@ -16,20 +17,26 @@ import (
|
||||
var json = jsoniter.ConfigCompatibleWithStandardLibrary
|
||||
|
||||
type EtcdList struct {
|
||||
NetworkName []string
|
||||
Endpoints []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
|
||||
@@ -64,12 +71,8 @@ type NodeInfoList struct {
|
||||
NodeList []NodeInfo
|
||||
}
|
||||
|
||||
func validConfigFile(f os.DirEntry) bool {
|
||||
if f.IsDir() == true || (filepath.Ext(f.Name()) != ".json" && filepath.Ext(f.Name()) != ".yml" && filepath.Ext(f.Name()) != ".yaml") {
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
func validConfigFile(f string) bool {
|
||||
return strings.HasSuffix(f, ".json") || strings.HasSuffix(f, ".yml") || strings.HasSuffix(f, ".yaml")
|
||||
}
|
||||
|
||||
func yamlToJson(data []byte, v interface{}) ([]byte, error) {
|
||||
@@ -88,15 +91,16 @@ func yamlToJson(data []byte, v interface{}) ([]byte, error) {
|
||||
}
|
||||
|
||||
func unmarshalConfig(data []byte, v interface{}) error {
|
||||
if !json.Valid(data) {
|
||||
envData := []byte(os.ExpandEnv(string(data)))
|
||||
if !json.Valid(envData) {
|
||||
var err error
|
||||
data, err = yamlToJson(data, v)
|
||||
envData, err = yamlToJson(envData, v)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return json.Unmarshal(data, v)
|
||||
return json.Unmarshal(envData, v)
|
||||
}
|
||||
|
||||
func (d *DiscoveryInfo) getDiscoveryType() DiscoveryType {
|
||||
@@ -127,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{}{}
|
||||
@@ -270,32 +273,33 @@ func (cls *Cluster) readLocalClusterConfig(nodeId string) (DiscoveryInfo, []Node
|
||||
var discoveryInfo DiscoveryInfo
|
||||
var rpcMode RpcMode
|
||||
|
||||
clusterCfgPath := strings.TrimRight(configDir, "/") + "/cluster"
|
||||
fileInfoList, err := os.ReadDir(clusterCfgPath)
|
||||
if err != nil {
|
||||
return discoveryInfo, nil, rpcMode, fmt.Errorf("read dir %s is fail :%+v", clusterCfgPath, err)
|
||||
}
|
||||
|
||||
//读取任何文件,只读符合格式的配置,目录下的文件可以自定义分文件
|
||||
for _, f := range fileInfoList {
|
||||
if !validConfigFile(f) {
|
||||
continue
|
||||
err := filepath.Walk(configDir, func(path string, info fs.FileInfo, err error) error {
|
||||
if info.IsDir() {
|
||||
return nil
|
||||
}
|
||||
|
||||
filePath := strings.TrimRight(strings.TrimRight(clusterCfgPath, "/"), "\\") + "/" + f.Name()
|
||||
fileNodeInfoList, rErr := cls.ReadClusterConfig(filePath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if !validConfigFile(info.Name()) {
|
||||
return nil
|
||||
}
|
||||
|
||||
fileNodeInfoList, rErr := cls.ReadClusterConfig(path)
|
||||
if rErr != nil {
|
||||
return discoveryInfo, nil, rpcMode, fmt.Errorf("read file path %s is error:%+v", filePath, rErr)
|
||||
return fmt.Errorf("read file path %s is error:%+v", path, rErr)
|
||||
}
|
||||
|
||||
err = cls.SetRpcMode(&fileNodeInfoList.RpcMode, &rpcMode)
|
||||
if err != nil {
|
||||
return discoveryInfo, nil, rpcMode, err
|
||||
return err
|
||||
}
|
||||
|
||||
err = discoveryInfo.setDiscovery(&fileNodeInfoList.Discovery)
|
||||
if err != nil {
|
||||
return discoveryInfo, nil, rpcMode, err
|
||||
return err
|
||||
}
|
||||
|
||||
for _, nodeInfo := range fileNodeInfoList.NodeList {
|
||||
@@ -303,6 +307,12 @@ func (cls *Cluster) readLocalClusterConfig(nodeId string) (DiscoveryInfo, []Node
|
||||
nodeInfoList = append(nodeInfoList, nodeInfo)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return discoveryInfo, nil, rpcMode, err
|
||||
}
|
||||
|
||||
if nodeId != rpc.NodeIdNull && (len(nodeInfoList) != 1) {
|
||||
@@ -324,32 +334,32 @@ func (cls *Cluster) readLocalClusterConfig(nodeId string) (DiscoveryInfo, []Node
|
||||
}
|
||||
|
||||
func (cls *Cluster) readLocalService(localNodeId string) error {
|
||||
clusterCfgPath := strings.TrimRight(configDir, "/") + "/cluster"
|
||||
fileInfoList, err := os.ReadDir(clusterCfgPath)
|
||||
if err != nil {
|
||||
return fmt.Errorf("read dir %s is fail :%+v", clusterCfgPath, err)
|
||||
}
|
||||
|
||||
var globalCfg interface{}
|
||||
publicService := map[string]interface{}{}
|
||||
nodeService := map[string]interface{}{}
|
||||
|
||||
//读取任何文件,只读符合格式的配置,目录下的文件可以自定义分文件
|
||||
for _, f := range fileInfoList {
|
||||
if !validConfigFile(f) {
|
||||
continue
|
||||
err := filepath.Walk(configDir, func(path string, info fs.FileInfo, err error) error {
|
||||
if info.IsDir() {
|
||||
return nil
|
||||
}
|
||||
|
||||
filePath := strings.TrimRight(strings.TrimRight(clusterCfgPath, "/"), "\\") + "/" + f.Name()
|
||||
currGlobalCfg, serviceConfig, mapNodeService, err := cls.readServiceConfig(filePath)
|
||||
if err != nil {
|
||||
continue
|
||||
return err
|
||||
}
|
||||
|
||||
if !validConfigFile(info.Name()) {
|
||||
return nil
|
||||
}
|
||||
currGlobalCfg, serviceConfig, mapNodeService, err := cls.readServiceConfig(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if currGlobalCfg != nil {
|
||||
//不允许重复的配置global配置
|
||||
if globalCfg != nil {
|
||||
return fmt.Errorf("[Global] does not allow repeated configuration in %s", f.Name())
|
||||
return fmt.Errorf("[Global] does not allow repeated configuration in %s", info.Name())
|
||||
}
|
||||
globalCfg = currGlobalCfg
|
||||
}
|
||||
@@ -365,7 +375,7 @@ func (cls *Cluster) readLocalService(localNodeId string) error {
|
||||
pubCfg, ok := serviceConfig[s]
|
||||
if ok == true {
|
||||
if _, publicOk := publicService[s]; publicOk == true {
|
||||
return fmt.Errorf("public service [%s] does not allow repeated configuration in %s", s, f.Name())
|
||||
return fmt.Errorf("public service [%s] does not allow repeated configuration in %s", s, info.Name())
|
||||
}
|
||||
publicService[s] = pubCfg
|
||||
}
|
||||
@@ -381,12 +391,17 @@ func (cls *Cluster) readLocalService(localNodeId string) error {
|
||||
}
|
||||
|
||||
if _, nodeOK := nodeService[s]; nodeOK == true {
|
||||
return fmt.Errorf("NodeService NodeId[%s] Service[%s] does not allow repeated configuration in %s", cls.localNodeInfo.NodeId, s, f.Name())
|
||||
return fmt.Errorf("NodeService NodeId[%s] Service[%s] does not allow repeated configuration in %s", cls.localNodeInfo.NodeId, s, info.Name())
|
||||
}
|
||||
nodeService[s] = nodeCfg
|
||||
break
|
||||
}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
//组合所有的配置
|
||||
@@ -416,13 +431,12 @@ func (cls *Cluster) readLocalService(localNodeId string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (cls *Cluster) parseLocalCfg() {
|
||||
func (cls *Cluster) parseLocalCfg() error {
|
||||
rpcInfo := NodeRpcInfo{}
|
||||
rpcInfo.nodeInfo = cls.localNodeInfo
|
||||
rpcInfo.client = rpc.NewLClient(rpcInfo.nodeInfo.NodeId, &cls.callSet)
|
||||
|
||||
cls.mapRpc[cls.localNodeInfo.NodeId] = &rpcInfo
|
||||
|
||||
for _, serviceName := range cls.localNodeInfo.ServiceList {
|
||||
splitServiceName := strings.Split(serviceName, ":")
|
||||
if len(splitServiceName) == 2 {
|
||||
@@ -439,8 +453,13 @@ func (cls *Cluster) parseLocalCfg() {
|
||||
cls.mapServiceNode[serviceName] = make(map[string]struct{})
|
||||
}
|
||||
|
||||
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{}{}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (cls *Cluster) IsNatsMode() bool {
|
||||
@@ -473,8 +492,7 @@ func (cls *Cluster) InitCfg(localNodeId string) error {
|
||||
}
|
||||
|
||||
//本地配置服务加到全局map信息中
|
||||
cls.parseLocalCfg()
|
||||
return nil
|
||||
return cls.parseLocalCfg()
|
||||
}
|
||||
|
||||
func (cls *Cluster) IsConfigService(serviceName string) bool {
|
||||
@@ -489,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()
|
||||
|
||||
@@ -58,7 +58,7 @@ func (c *Concurrent) AsyncDoByQueue(queueId int64, fn func() bool, cb func(err e
|
||||
}
|
||||
|
||||
if fn == nil && cb == nil {
|
||||
log.Stack("fn and cb is nil")
|
||||
log.StackError("fn and cb is nil")
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
@@ -6,7 +6,7 @@ import (
|
||||
"time"
|
||||
|
||||
"fmt"
|
||||
"runtime"
|
||||
|
||||
|
||||
"context"
|
||||
"github.com/duanhf2012/origin/v2/log"
|
||||
@@ -192,10 +192,7 @@ breakFor:
|
||||
func (d *dispatch) DoCallback(cb func(err error)) {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
buf := make([]byte, 4096)
|
||||
l := runtime.Stack(buf, false)
|
||||
errString := fmt.Sprint(r)
|
||||
log.Dump(string(buf[:l]), log.String("error", errString))
|
||||
log.StackError(fmt.Sprint(r))
|
||||
}
|
||||
}()
|
||||
|
||||
|
||||
@@ -5,7 +5,7 @@ import (
|
||||
|
||||
"errors"
|
||||
"fmt"
|
||||
"runtime"
|
||||
|
||||
|
||||
"github.com/duanhf2012/origin/v2/log"
|
||||
)
|
||||
@@ -51,15 +51,13 @@ func (w *worker) run(waitGroup *sync.WaitGroup, t task) {
|
||||
func (w *worker) exec(t *task) {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
buf := make([]byte, 4096)
|
||||
l := runtime.Stack(buf, false)
|
||||
errString := fmt.Sprint(r)
|
||||
|
||||
cb := t.cb
|
||||
t.cb = func(err error) {
|
||||
cb(errors.New(errString))
|
||||
}
|
||||
log.Dump(string(buf[:l]), log.String("error", errString))
|
||||
log.StackError(errString)
|
||||
w.endCallFun(true, t)
|
||||
}
|
||||
}()
|
||||
|
||||
@@ -3,7 +3,6 @@ package event
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/duanhf2012/origin/v2/log"
|
||||
"runtime"
|
||||
"sync"
|
||||
)
|
||||
|
||||
@@ -215,10 +214,7 @@ func (handler *EventHandler) Destroy() {
|
||||
func (processor *EventProcessor) EventHandler(ev IEvent) {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
buf := make([]byte, 4096)
|
||||
l := runtime.Stack(buf, false)
|
||||
errString := fmt.Sprint(r)
|
||||
log.Dump(string(buf[:l]), log.String("error", errString))
|
||||
log.StackError(fmt.Sprint(r))
|
||||
}
|
||||
}()
|
||||
|
||||
|
||||
@@ -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
|
||||
)
|
||||
|
||||
6
go.mod
6
go.mod
@@ -6,11 +6,13 @@ toolchain go1.22.7
|
||||
|
||||
require (
|
||||
github.com/IBM/sarama v1.43.3
|
||||
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
|
||||
github.com/gomodule/redigo v1.8.8
|
||||
github.com/google/uuid v1.6.0
|
||||
github.com/gorilla/websocket v1.5.0
|
||||
github.com/gorilla/websocket v1.5.3
|
||||
github.com/json-iterator/go v1.1.12
|
||||
github.com/nats-io/nats.go v1.34.1
|
||||
github.com/pierrec/lz4/v4 v4.1.21
|
||||
@@ -21,6 +23,7 @@ require (
|
||||
go.mongodb.org/mongo-driver v1.9.1
|
||||
go.uber.org/zap v1.27.0
|
||||
google.golang.org/protobuf v1.34.1
|
||||
gopkg.in/natefinch/lumberjack.v2 v2.2.1
|
||||
gopkg.in/yaml.v3 v3.0.1
|
||||
)
|
||||
|
||||
@@ -42,7 +45,6 @@ require (
|
||||
github.com/go-playground/universal-translator v0.18.1 // indirect
|
||||
github.com/go-playground/validator/v10 v10.20.0 // indirect
|
||||
github.com/go-stack/stack v1.8.0 // indirect
|
||||
github.com/goccy/go-json v0.10.2 // indirect
|
||||
github.com/gogo/protobuf v1.3.2 // indirect
|
||||
github.com/golang/protobuf v1.5.4 // indirect
|
||||
github.com/golang/snappy v0.0.4 // indirect
|
||||
|
||||
8
go.sum
8
go.sum
@@ -20,6 +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-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=
|
||||
@@ -86,8 +88,8 @@ github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
|
||||
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||
github.com/gorilla/securecookie v1.1.1/go.mod h1:ra0sb63/xPlUeL+yeDciTfxMRAA+MP+HVt/4epWDjd4=
|
||||
github.com/gorilla/sessions v1.2.1/go.mod h1:dk2InVEVJ0sfLlnXv9EAgkf6ecYs/i80K/zI+bUmuGM=
|
||||
github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc=
|
||||
github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
|
||||
github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg=
|
||||
github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
|
||||
github.com/hashicorp/errwrap v1.0.0 h1:hLrqtEDnRye3+sgx6z4qVLNuviH3MR5aQ0ykNJa/UYA=
|
||||
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
|
||||
github.com/hashicorp/go-multierror v1.1.1 h1:H5DkEtf6CXdFp0N0Em5UCwQpXMWke8IA0+lD48awMYo=
|
||||
@@ -330,6 +332,8 @@ gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8
|
||||
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
|
||||
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
|
||||
gopkg.in/natefinch/lumberjack.v2 v2.2.1 h1:bBRl1b0OH9s/DuPhuXpNl+VtCaJXFZ5/uEFST95x9zc=
|
||||
gopkg.in/natefinch/lumberjack.v2 v2.2.1/go.mod h1:YD8tP3GAjkrDg1eZH7EGmyESg/lsYskCTPBJVb9jqSc=
|
||||
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
|
||||
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
|
||||
|
||||
@@ -1,93 +0,0 @@
|
||||
package log
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
)
|
||||
|
||||
const _size = 9216
|
||||
|
||||
type Buffer struct {
|
||||
bs []byte
|
||||
//mu sync.Mutex // ensures atomic writes; protects the following fields
|
||||
}
|
||||
|
||||
func (buff *Buffer) Init() {
|
||||
buff.bs = make([]byte, _size)
|
||||
}
|
||||
|
||||
// AppendByte writes a single byte to the Buffer.
|
||||
func (buff *Buffer) AppendByte(v byte) {
|
||||
buff.bs = append(buff.bs, v)
|
||||
}
|
||||
|
||||
func (buff *Buffer) AppendBytes(v []byte) {
|
||||
buff.bs = append(buff.bs, v...)
|
||||
}
|
||||
|
||||
// AppendString writes a string to the Buffer.
|
||||
func (buff *Buffer) AppendString(s string) {
|
||||
buff.bs = append(buff.bs, s...)
|
||||
}
|
||||
|
||||
// AppendInt appends an integer to the underlying buffer (assuming base 10).
|
||||
func (buff *Buffer) AppendInt(i int64) {
|
||||
buff.bs = strconv.AppendInt(buff.bs, i, 10)
|
||||
}
|
||||
|
||||
// AppendUint appends an unsigned integer to the underlying buffer (assuming
|
||||
// base 10).
|
||||
func (buff *Buffer) AppendUint(i uint64) {
|
||||
buff.bs = strconv.AppendUint(buff.bs, i, 10)
|
||||
}
|
||||
|
||||
// AppendBool appends a bool to the underlying buffer.
|
||||
func (buff *Buffer) AppendBool(v bool) {
|
||||
buff.bs = strconv.AppendBool(buff.bs, v)
|
||||
}
|
||||
|
||||
// AppendFloat appends a float to the underlying buffer. It doesn't quote NaN
|
||||
// or +/- Inf.
|
||||
func (buff *Buffer) AppendFloat(f float64, bitSize int) {
|
||||
buff.bs = strconv.AppendFloat(buff.bs, f, 'f', -1, bitSize)
|
||||
}
|
||||
|
||||
// Len returns the length of the underlying byte slice.
|
||||
func (buff *Buffer) Len() int {
|
||||
return len(buff.bs)
|
||||
}
|
||||
|
||||
// Cap returns the capacity of the underlying byte slice.
|
||||
func (buff *Buffer) Cap() int {
|
||||
return cap(buff.bs)
|
||||
}
|
||||
|
||||
// Bytes returns a mutable reference to the underlying byte slice.
|
||||
func (buff *Buffer) Bytes() []byte {
|
||||
return buff.bs
|
||||
}
|
||||
|
||||
// String returns a string copy of the underlying byte slice.
|
||||
func (buff *Buffer) String() string {
|
||||
return string(buff.bs)
|
||||
}
|
||||
|
||||
// Reset resets the underlying byte slice. Subsequent writes re-use the slice's
|
||||
// backing array.
|
||||
func (buff *Buffer) Reset() {
|
||||
buff.bs = buff.bs[:0]
|
||||
}
|
||||
|
||||
// Write implements io.Writer.
|
||||
func (buff *Buffer) Write(bs []byte) (int, error) {
|
||||
buff.bs = append(buff.bs, bs...)
|
||||
return len(bs), nil
|
||||
}
|
||||
|
||||
// TrimNewline trims any final "\n" byte from the end of the buffer.
|
||||
func (buff *Buffer) TrimNewline() {
|
||||
if i := len(buff.bs) - 1; i >= 0 {
|
||||
if buff.bs[i] == '\n' {
|
||||
buff.bs = buff.bs[:i]
|
||||
}
|
||||
}
|
||||
}
|
||||
161
log/handler.go
161
log/handler.go
@@ -1,161 +0,0 @@
|
||||
package log
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
"log/slog"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"runtime/debug"
|
||||
"sync"
|
||||
)
|
||||
|
||||
const defaultSkip = 7
|
||||
|
||||
type IOriginHandler interface {
|
||||
slog.Handler
|
||||
Lock()
|
||||
UnLock()
|
||||
SetSkip(skip int)
|
||||
GetSkip() int
|
||||
}
|
||||
|
||||
type BaseHandler struct {
|
||||
addSource bool
|
||||
w io.Writer
|
||||
locker sync.Mutex
|
||||
skip int
|
||||
}
|
||||
|
||||
type OriginTextHandler struct {
|
||||
BaseHandler
|
||||
*slog.TextHandler
|
||||
}
|
||||
|
||||
type OriginJsonHandler struct {
|
||||
BaseHandler
|
||||
*slog.JSONHandler
|
||||
}
|
||||
|
||||
func (bh *BaseHandler) SetSkip(skip int) {
|
||||
bh.skip = skip
|
||||
}
|
||||
|
||||
func (bh *BaseHandler) GetSkip() int {
|
||||
return bh.skip
|
||||
}
|
||||
|
||||
func getStrLevel(level slog.Level) string {
|
||||
switch level {
|
||||
case LevelTrace:
|
||||
return "Trace"
|
||||
case LevelDebug:
|
||||
return "Debug"
|
||||
case LevelInfo:
|
||||
return "Info"
|
||||
case LevelWarning:
|
||||
return "Warning"
|
||||
case LevelError:
|
||||
return "Error"
|
||||
case LevelStack:
|
||||
return "Stack"
|
||||
case LevelDump:
|
||||
return "Dump"
|
||||
case LevelFatal:
|
||||
return "Fatal"
|
||||
}
|
||||
|
||||
return ""
|
||||
}
|
||||
|
||||
func defaultReplaceAttr(groups []string, a slog.Attr) slog.Attr {
|
||||
if a.Key == slog.LevelKey {
|
||||
level := a.Value.Any().(slog.Level)
|
||||
a.Value = slog.StringValue(getStrLevel(level))
|
||||
} else if a.Key == slog.TimeKey && len(groups) == 0 {
|
||||
a.Value = slog.StringValue(a.Value.Time().Format("2006/01/02 15:04:05"))
|
||||
} else if a.Key == slog.SourceKey {
|
||||
source := a.Value.Any().(*slog.Source)
|
||||
source.File = filepath.Base(source.File)
|
||||
}
|
||||
return a
|
||||
}
|
||||
|
||||
func NewOriginTextHandler(level slog.Level, w io.Writer, addSource bool, replaceAttr func([]string, slog.Attr) slog.Attr) slog.Handler {
|
||||
var textHandler OriginTextHandler
|
||||
textHandler.addSource = addSource
|
||||
textHandler.w = w
|
||||
textHandler.TextHandler = slog.NewTextHandler(w, &slog.HandlerOptions{
|
||||
AddSource: addSource,
|
||||
Level: level,
|
||||
ReplaceAttr: replaceAttr,
|
||||
})
|
||||
|
||||
textHandler.skip = defaultSkip
|
||||
return &textHandler
|
||||
}
|
||||
|
||||
func (oh *OriginTextHandler) Handle(context context.Context, record slog.Record) error {
|
||||
oh.Fill(context, &record)
|
||||
oh.locker.Lock()
|
||||
defer oh.locker.Unlock()
|
||||
|
||||
if record.Level == LevelStack || record.Level == LevelFatal {
|
||||
err := oh.TextHandler.Handle(context, record)
|
||||
oh.logStack(&record)
|
||||
return err
|
||||
} else if record.Level == LevelDump {
|
||||
strDump := record.Message
|
||||
record.Message = "dump info"
|
||||
err := oh.TextHandler.Handle(context, record)
|
||||
oh.w.Write([]byte(strDump))
|
||||
return err
|
||||
}
|
||||
|
||||
return oh.TextHandler.Handle(context, record)
|
||||
}
|
||||
|
||||
func (bh *BaseHandler) logStack(record *slog.Record) {
|
||||
bh.w.Write(debug.Stack())
|
||||
}
|
||||
|
||||
func (bh *BaseHandler) Lock() {
|
||||
bh.locker.Lock()
|
||||
}
|
||||
|
||||
func (bh *BaseHandler) UnLock() {
|
||||
bh.locker.Unlock()
|
||||
}
|
||||
|
||||
func NewOriginJsonHandler(level slog.Level, w io.Writer, addSource bool, replaceAttr func([]string, slog.Attr) slog.Attr) slog.Handler {
|
||||
var jsonHandler OriginJsonHandler
|
||||
jsonHandler.addSource = addSource
|
||||
jsonHandler.w = w
|
||||
jsonHandler.JSONHandler = slog.NewJSONHandler(w, &slog.HandlerOptions{
|
||||
AddSource: addSource,
|
||||
Level: level,
|
||||
ReplaceAttr: replaceAttr,
|
||||
})
|
||||
|
||||
jsonHandler.skip = defaultSkip
|
||||
return &jsonHandler
|
||||
}
|
||||
|
||||
func (oh *OriginJsonHandler) Handle(context context.Context, record slog.Record) error {
|
||||
oh.Fill(context, &record)
|
||||
if record.Level == LevelStack || record.Level == LevelFatal || record.Level == LevelDump {
|
||||
record.Add("stack", debug.Stack())
|
||||
}
|
||||
|
||||
oh.locker.Lock()
|
||||
defer oh.locker.Unlock()
|
||||
return oh.JSONHandler.Handle(context, record)
|
||||
}
|
||||
|
||||
func (bh *BaseHandler) Fill(_ context.Context, record *slog.Record) {
|
||||
if bh.addSource {
|
||||
var pcs [1]uintptr
|
||||
runtime.Callers(bh.skip, pcs[:])
|
||||
record.PC = pcs[0]
|
||||
}
|
||||
}
|
||||
807
log/log.go
807
log/log.go
@@ -1,520 +1,401 @@
|
||||
package log
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/duanhf2012/origin/v2/util/bytespool"
|
||||
"io"
|
||||
"log/slog"
|
||||
"github.com/duanhf2012/rotatelogs"
|
||||
"go.uber.org/zap"
|
||||
"go.uber.org/zap/zapcore"
|
||||
"gopkg.in/natefinch/lumberjack.v2"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
var OpenConsole bool
|
||||
var LogSize int64
|
||||
var LogChannelCap int
|
||||
var LogPath string
|
||||
var LogLevel = LevelTrace
|
||||
|
||||
var gLogger, _ = NewTextLogger(LevelDebug, "", "", true, LogChannelCap)
|
||||
var isSetLogger bool
|
||||
var memPool = bytespool.NewMemAreaPool()
|
||||
|
||||
// levels
|
||||
const (
|
||||
LevelTrace = slog.Level(-8)
|
||||
LevelDebug = slog.LevelDebug
|
||||
LevelInfo = slog.LevelInfo
|
||||
LevelWarning = slog.LevelWarn
|
||||
LevelError = slog.LevelError
|
||||
LevelStack = slog.Level(12)
|
||||
LevelDump = slog.Level(16)
|
||||
LevelFatal = slog.Level(20)
|
||||
)
|
||||
|
||||
type ILogger interface {
|
||||
Trace(msg string, args ...any)
|
||||
Debug(msg string, args ...any)
|
||||
Info(msg string, args ...any)
|
||||
Warning(msg string, args ...any)
|
||||
Error(msg string, args ...any)
|
||||
Stack(msg string, args ...any)
|
||||
Dump(msg string, args ...any)
|
||||
Fatal(msg string, args ...any)
|
||||
|
||||
DoSPrintf(level slog.Level, a []interface{})
|
||||
FormatHeader(buf *Buffer, level slog.Level, callDepth int)
|
||||
Close()
|
||||
}
|
||||
var gLogger = NewDefaultLogger()
|
||||
var LogLevel zapcore.Level
|
||||
var MaxSize int
|
||||
var LogPath string
|
||||
var OpenConsole *bool
|
||||
var LogChanLen int
|
||||
|
||||
type Logger struct {
|
||||
SLogger *slog.Logger
|
||||
*zap.Logger
|
||||
stack bool
|
||||
|
||||
ioWriter IoWriter
|
||||
|
||||
sBuff Buffer
|
||||
FileName string
|
||||
Skip int
|
||||
Encoder zapcore.Encoder
|
||||
SugaredLogger *zap.SugaredLogger
|
||||
CoreList []zapcore.Core
|
||||
WriteSyncerFun []func() zapcore.WriteSyncer
|
||||
}
|
||||
|
||||
type IoWriter struct {
|
||||
outFile io.Writer // destination for output
|
||||
writeBytes int64
|
||||
logChannel chan []byte
|
||||
wg sync.WaitGroup
|
||||
closeSig chan struct{}
|
||||
|
||||
lockWrite sync.Mutex
|
||||
|
||||
filePath string
|
||||
filePrefix string
|
||||
fileDay int
|
||||
fileCreateTime int64 //second
|
||||
}
|
||||
|
||||
func (iw *IoWriter) Close() error {
|
||||
iw.lockWrite.Lock()
|
||||
defer iw.lockWrite.Unlock()
|
||||
|
||||
iw.close()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (iw *IoWriter) close() error {
|
||||
if iw.closeSig != nil {
|
||||
close(iw.closeSig)
|
||||
iw.closeSig = nil
|
||||
}
|
||||
iw.wg.Wait()
|
||||
|
||||
if iw.outFile != nil {
|
||||
err := iw.outFile.(io.Closer).Close()
|
||||
iw.outFile = nil
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (iw *IoWriter) writeFile(p []byte) (n int, err error) {
|
||||
//switch log file
|
||||
iw.switchFile()
|
||||
|
||||
if iw.outFile != nil {
|
||||
n, err = iw.outFile.Write(p)
|
||||
if n > 0 {
|
||||
atomic.AddInt64(&iw.writeBytes, int64(n))
|
||||
}
|
||||
}
|
||||
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
func (iw *IoWriter) Write(p []byte) (n int, err error) {
|
||||
iw.lockWrite.Lock()
|
||||
defer iw.lockWrite.Unlock()
|
||||
|
||||
if iw.logChannel == nil {
|
||||
return iw.writeIo(p)
|
||||
}
|
||||
|
||||
copyBuff := memPool.MakeBytes(len(p))
|
||||
if copyBuff == nil {
|
||||
return 0, fmt.Errorf("MakeByteSlice failed")
|
||||
}
|
||||
copy(copyBuff, p)
|
||||
|
||||
iw.logChannel <- copyBuff
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (iw *IoWriter) writeIo(p []byte) (n int, err error) {
|
||||
n, err = iw.writeFile(p)
|
||||
|
||||
if OpenConsole {
|
||||
n, err = os.Stdout.Write(p)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (iw *IoWriter) setLogChannel(logChannelNum int) (err error) {
|
||||
iw.lockWrite.Lock()
|
||||
defer iw.lockWrite.Unlock()
|
||||
iw.close()
|
||||
|
||||
if logChannelNum == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
//copy iw.logChannel
|
||||
var logInfo []byte
|
||||
logChannel := make(chan []byte, logChannelNum)
|
||||
for i := 0; i < logChannelNum && i < len(iw.logChannel); i++ {
|
||||
logInfo = <-iw.logChannel
|
||||
logChannel <- logInfo
|
||||
}
|
||||
iw.logChannel = logChannel
|
||||
|
||||
iw.closeSig = make(chan struct{})
|
||||
iw.wg.Add(1)
|
||||
go iw.run()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (iw *IoWriter) run() {
|
||||
defer iw.wg.Done()
|
||||
|
||||
Loop:
|
||||
for {
|
||||
select {
|
||||
case <-iw.closeSig:
|
||||
break Loop
|
||||
case logs := <-iw.logChannel:
|
||||
iw.writeIo(logs)
|
||||
memPool.ReleaseBytes(logs)
|
||||
}
|
||||
}
|
||||
|
||||
for len(iw.logChannel) > 0 {
|
||||
logs := <-iw.logChannel
|
||||
iw.writeIo(logs)
|
||||
memPool.ReleaseBytes(logs)
|
||||
}
|
||||
}
|
||||
|
||||
func (iw *IoWriter) isFull() bool {
|
||||
if LogSize == 0 {
|
||||
return false
|
||||
}
|
||||
|
||||
return atomic.LoadInt64(&iw.writeBytes) >= LogSize
|
||||
}
|
||||
|
||||
func (logger *Logger) setLogChannel(logChannel int) (err error) {
|
||||
return logger.ioWriter.setLogChannel(logChannel)
|
||||
}
|
||||
|
||||
func (iw *IoWriter) switchFile() error {
|
||||
now := time.Now()
|
||||
if iw.fileCreateTime == now.Unix() {
|
||||
return nil
|
||||
}
|
||||
|
||||
if iw.fileDay == now.Day() && iw.isFull() == false {
|
||||
return nil
|
||||
}
|
||||
|
||||
if iw.filePath != "" {
|
||||
var err error
|
||||
fileName := fmt.Sprintf("%s%d%02d%02d_%02d_%02d_%02d.log",
|
||||
iw.filePrefix,
|
||||
now.Year(),
|
||||
now.Month(),
|
||||
now.Day(),
|
||||
now.Hour(),
|
||||
now.Minute(),
|
||||
now.Second())
|
||||
|
||||
filePath := path.Join(iw.filePath, fileName)
|
||||
|
||||
iw.outFile, err = os.Create(filePath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
iw.fileDay = now.Day()
|
||||
iw.fileCreateTime = now.Unix()
|
||||
atomic.StoreInt64(&iw.writeBytes, 0)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func GetDefaultHandler() IOriginHandler {
|
||||
return gLogger.(*Logger).SLogger.Handler().(IOriginHandler)
|
||||
}
|
||||
|
||||
func NewTextLogger(level slog.Level, pathName string, filePrefix string, addSource bool, logChannelCap int) (ILogger, error) {
|
||||
var logger Logger
|
||||
logger.ioWriter.filePath = pathName
|
||||
logger.ioWriter.filePrefix = filePrefix
|
||||
|
||||
logger.SLogger = slog.New(NewOriginTextHandler(level, &logger.ioWriter, addSource, defaultReplaceAttr))
|
||||
logger.setLogChannel(logChannelCap)
|
||||
err := logger.ioWriter.switchFile()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &logger, nil
|
||||
}
|
||||
|
||||
func NewJsonLogger(level slog.Level, pathName string, filePrefix string, addSource bool, logChannelCap int) (ILogger, error) {
|
||||
var logger Logger
|
||||
logger.ioWriter.filePath = pathName
|
||||
logger.ioWriter.filePrefix = filePrefix
|
||||
|
||||
logger.SLogger = slog.New(NewOriginJsonHandler(level, &logger.ioWriter, true, defaultReplaceAttr))
|
||||
logger.setLogChannel(logChannelCap)
|
||||
err := logger.ioWriter.switchFile()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &logger, nil
|
||||
}
|
||||
|
||||
// Close It's dangerous to call the method on logging
|
||||
func (logger *Logger) Close() {
|
||||
logger.ioWriter.Close()
|
||||
}
|
||||
|
||||
func (logger *Logger) Trace(msg string, args ...any) {
|
||||
logger.SLogger.Log(context.Background(), LevelTrace, msg, args...)
|
||||
}
|
||||
|
||||
func (logger *Logger) Debug(msg string, args ...any) {
|
||||
|
||||
logger.SLogger.Log(context.Background(), LevelDebug, msg, args...)
|
||||
}
|
||||
|
||||
func (logger *Logger) Info(msg string, args ...any) {
|
||||
logger.SLogger.Log(context.Background(), LevelInfo, msg, args...)
|
||||
}
|
||||
|
||||
func (logger *Logger) Warning(msg string, args ...any) {
|
||||
logger.SLogger.Log(context.Background(), LevelWarning, msg, args...)
|
||||
}
|
||||
|
||||
func (logger *Logger) Error(msg string, args ...any) {
|
||||
logger.SLogger.Log(context.Background(), LevelError, msg, args...)
|
||||
}
|
||||
|
||||
func (logger *Logger) Stack(msg string, args ...any) {
|
||||
logger.SLogger.Log(context.Background(), LevelStack, msg, args...)
|
||||
}
|
||||
|
||||
func (logger *Logger) Dump(msg string, args ...any) {
|
||||
logger.SLogger.Log(context.Background(), LevelDump, msg, args...)
|
||||
}
|
||||
|
||||
func (logger *Logger) Fatal(msg string, args ...any) {
|
||||
logger.SLogger.Log(context.Background(), LevelFatal, msg, args...)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
// SetLogger It's non-thread-safe
|
||||
func SetLogger(logger ILogger) {
|
||||
if logger != nil && isSetLogger == false {
|
||||
// 设置Logger
|
||||
func SetLogger(logger *Logger) {
|
||||
if logger != nil {
|
||||
gLogger = logger
|
||||
isSetLogger = true
|
||||
}
|
||||
}
|
||||
|
||||
func GetLogger() ILogger {
|
||||
// 设置ZapLogger
|
||||
func SetZapLogger(zapLogger *zap.Logger) {
|
||||
if zapLogger != nil {
|
||||
gLogger = &Logger{}
|
||||
gLogger.Logger = zapLogger
|
||||
isSetLogger = true
|
||||
}
|
||||
}
|
||||
|
||||
func GetLogger() *Logger {
|
||||
return gLogger
|
||||
}
|
||||
|
||||
func Trace(msg string, args ...any) {
|
||||
gLogger.Trace(msg, args...)
|
||||
func (logger *Logger) SetEncoder(encoder zapcore.Encoder) {
|
||||
logger.Encoder = encoder
|
||||
}
|
||||
|
||||
func Debug(msg string, args ...any) {
|
||||
gLogger.Debug(msg, args...)
|
||||
func (logger *Logger) SetSkip(skip int) {
|
||||
logger.Skip = skip
|
||||
}
|
||||
|
||||
func Info(msg string, args ...any) {
|
||||
gLogger.Info(msg, args...)
|
||||
}
|
||||
|
||||
func Warning(msg string, args ...any) {
|
||||
gLogger.Warning(msg, args...)
|
||||
}
|
||||
|
||||
func Error(msg string, args ...any) {
|
||||
gLogger.Error(msg, args...)
|
||||
}
|
||||
|
||||
func Stack(msg string, args ...any) {
|
||||
gLogger.Stack(msg, args...)
|
||||
}
|
||||
|
||||
func Dump(dump string, args ...any) {
|
||||
gLogger.Dump(dump, args...)
|
||||
}
|
||||
|
||||
func Fatal(msg string, args ...any) {
|
||||
gLogger.Fatal(msg, args...)
|
||||
}
|
||||
|
||||
func Close() {
|
||||
gLogger.Close()
|
||||
}
|
||||
|
||||
func ErrorAttr(key string, value error) slog.Attr {
|
||||
if value == nil {
|
||||
return slog.Attr{Key: key, Value: slog.StringValue("nil")}
|
||||
func GetJsonEncoder() zapcore.Encoder {
|
||||
encoderConfig := zap.NewProductionEncoderConfig()
|
||||
encoderConfig.EncodeCaller = zapcore.ShortCallerEncoder
|
||||
encoderConfig.EncodeLevel = zapcore.CapitalLevelEncoder
|
||||
encoderConfig.EncodeTime = func(t time.Time, enc zapcore.PrimitiveArrayEncoder) {
|
||||
enc.AppendString(t.Format("2006-01-02 15:04:05.000"))
|
||||
}
|
||||
|
||||
return slog.Attr{Key: key, Value: slog.StringValue(value.Error())}
|
||||
return zapcore.NewJSONEncoder(encoderConfig)
|
||||
}
|
||||
|
||||
func String(key, value string) slog.Attr {
|
||||
return slog.Attr{Key: key, Value: slog.StringValue(value)}
|
||||
func GetTxtEncoder() zapcore.Encoder {
|
||||
encoderConfig := zap.NewProductionEncoderConfig()
|
||||
encoderConfig.EncodeLevel = zapcore.CapitalLevelEncoder
|
||||
encoderConfig.EncodeCaller = zapcore.ShortCallerEncoder
|
||||
encoderConfig.EncodeTime = func(t time.Time, enc zapcore.PrimitiveArrayEncoder) {
|
||||
enc.AppendString(t.Format("2006-01-02 15:04:05.000"))
|
||||
}
|
||||
|
||||
return zapcore.NewConsoleEncoder(encoderConfig)
|
||||
}
|
||||
|
||||
func Int(key string, value int) slog.Attr {
|
||||
return slog.Attr{Key: key, Value: slog.Int64Value(int64(value))}
|
||||
func (logger *Logger) getLogConfig() *lumberjack.Logger {
|
||||
return &lumberjack.Logger{
|
||||
Filename: filepath.Join(LogPath, logger.FileName),
|
||||
MaxSize: MaxSize,
|
||||
MaxBackups: 0,
|
||||
MaxAge: 0,
|
||||
Compress: false,
|
||||
LocalTime: true,
|
||||
}
|
||||
}
|
||||
|
||||
func Int64(key string, value int64) slog.Attr {
|
||||
return slog.Attr{Key: key, Value: slog.Int64Value(value)}
|
||||
func NewDefaultLogger() *Logger {
|
||||
logger := Logger{}
|
||||
logger.Encoder = GetJsonEncoder()
|
||||
core := zapcore.NewCore(logger.Encoder, zapcore.AddSync(os.Stdout), zap.InfoLevel)
|
||||
logger.Logger = zap.New(core, zap.AddCaller(), zap.AddCallerSkip(1))
|
||||
|
||||
return &logger
|
||||
}
|
||||
|
||||
func Int32(key string, value int32) slog.Attr {
|
||||
return slog.Attr{Key: key, Value: slog.Int64Value(int64(value))}
|
||||
func (logger *Logger) SetSyncers(syncers ...func() zapcore.WriteSyncer) {
|
||||
logger.WriteSyncerFun = syncers
|
||||
}
|
||||
|
||||
func Int16(key string, value int16) slog.Attr {
|
||||
return slog.Attr{Key: key, Value: slog.Int64Value(int64(value))}
|
||||
func (logger *Logger) AppendSyncerFun(syncerFun func() zapcore.WriteSyncer) {
|
||||
logger.WriteSyncerFun = append(logger.WriteSyncerFun, syncerFun)
|
||||
}
|
||||
|
||||
func Int8(key string, value int8) slog.Attr {
|
||||
return slog.Attr{Key: key, Value: slog.Int64Value(int64(value))}
|
||||
func SetLogLevel(level zapcore.Level) {
|
||||
LogLevel = level
|
||||
}
|
||||
|
||||
func Uint(key string, value uint) slog.Attr {
|
||||
return slog.Attr{Key: key, Value: slog.Uint64Value(uint64(value))}
|
||||
func (logger *Logger) Enabled(zapcore.Level) bool {
|
||||
return logger.stack
|
||||
}
|
||||
|
||||
func Uint64(key string, v uint64) slog.Attr {
|
||||
return slog.Attr{Key: key, Value: slog.Uint64Value(v)}
|
||||
func (logger *Logger) NewLumberjackWriter() zapcore.WriteSyncer {
|
||||
return zapcore.AddSync(
|
||||
&lumberjack.Logger{
|
||||
Filename: filepath.Join(LogPath, logger.FileName),
|
||||
MaxSize: MaxSize,
|
||||
MaxBackups: 0,
|
||||
MaxAge: 0,
|
||||
Compress: false,
|
||||
LocalTime: true,
|
||||
})
|
||||
}
|
||||
|
||||
func Uint32(key string, value uint32) slog.Attr {
|
||||
return slog.Attr{Key: key, Value: slog.Uint64Value(uint64(value))}
|
||||
func (logger *Logger) NewRotatelogsWriter() zapcore.WriteSyncer {
|
||||
var options []rotatelogs.Option
|
||||
|
||||
if MaxSize > 0 {
|
||||
options = append(options, rotatelogs.WithRotateMaxSize(int64(MaxSize)))
|
||||
}
|
||||
if LogChanLen > 0 {
|
||||
options = append(options, rotatelogs.WithChannelLen(LogChanLen))
|
||||
}
|
||||
options = append(options, rotatelogs.WithRotationTime(time.Hour*24))
|
||||
|
||||
fileName := strings.TrimRight(logger.FileName, filepath.Ext(logger.FileName))
|
||||
rotateLogs, err := rotatelogs.NewRotateLogs(LogPath, fileName,"20060102","_20060102_150405", options...)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
return zapcore.AddSync(rotateLogs)
|
||||
}
|
||||
|
||||
func Uint16(key string, value uint16) slog.Attr {
|
||||
return slog.Attr{Key: key, Value: slog.Uint64Value(uint64(value))}
|
||||
}
|
||||
|
||||
func Uint8(key string, value uint8) slog.Attr {
|
||||
return slog.Attr{Key: key, Value: slog.Uint64Value(uint64(value))}
|
||||
}
|
||||
|
||||
func Float64(key string, v float64) slog.Attr {
|
||||
return slog.Attr{Key: key, Value: slog.Float64Value(v)}
|
||||
}
|
||||
|
||||
func Bool(key string, v bool) slog.Attr {
|
||||
return slog.Attr{Key: key, Value: slog.BoolValue(v)}
|
||||
}
|
||||
|
||||
func Time(key string, v time.Time) slog.Attr {
|
||||
return slog.Attr{Key: key, Value: slog.TimeValue(v)}
|
||||
}
|
||||
|
||||
func Duration(key string, v time.Duration) slog.Attr {
|
||||
return slog.Attr{Key: key, Value: slog.DurationValue(v)}
|
||||
}
|
||||
|
||||
func Any(key string, value any) slog.Attr {
|
||||
return slog.Attr{Key: key, Value: slog.AnyValue(value)}
|
||||
}
|
||||
|
||||
func Group(key string, args ...any) slog.Attr {
|
||||
return slog.Group(key, args...)
|
||||
}
|
||||
|
||||
func (logger *Logger) DoSPrintf(level slog.Level, a []interface{}) {
|
||||
if logger.SLogger.Enabled(context.Background(), level) == false {
|
||||
func (logger *Logger) Init() {
|
||||
if isSetLogger {
|
||||
return
|
||||
}
|
||||
|
||||
logger.SLogger.Handler().(IOriginHandler).Lock()
|
||||
defer logger.SLogger.Handler().(IOriginHandler).UnLock()
|
||||
|
||||
logger.sBuff.Reset()
|
||||
|
||||
logger.FormatHeader(&logger.sBuff, level, 3)
|
||||
|
||||
for _, s := range a {
|
||||
logger.sBuff.AppendString(slog.AnyValue(s).String())
|
||||
var syncerList []zapcore.WriteSyncer
|
||||
if logger.WriteSyncerFun == nil {
|
||||
syncerList = append(syncerList, logger.NewRotatelogsWriter())
|
||||
} else {
|
||||
for _, syncer := range logger.WriteSyncerFun {
|
||||
syncerList = append(syncerList, syncer())
|
||||
}
|
||||
}
|
||||
logger.sBuff.AppendString("\"\n")
|
||||
logger.ioWriter.Write(logger.sBuff.Bytes())
|
||||
}
|
||||
|
||||
func (logger *Logger) STrace(a ...interface{}) {
|
||||
logger.DoSPrintf(LevelTrace, a)
|
||||
}
|
||||
|
||||
func (logger *Logger) SDebug(a ...interface{}) {
|
||||
logger.DoSPrintf(LevelDebug, a)
|
||||
}
|
||||
|
||||
func (logger *Logger) SInfo(a ...interface{}) {
|
||||
logger.DoSPrintf(LevelInfo, a)
|
||||
}
|
||||
|
||||
func (logger *Logger) SWarning(a ...interface{}) {
|
||||
logger.DoSPrintf(LevelWarning, a)
|
||||
}
|
||||
|
||||
func (logger *Logger) SError(a ...interface{}) {
|
||||
logger.DoSPrintf(LevelError, a)
|
||||
}
|
||||
|
||||
func STrace(a ...interface{}) {
|
||||
gLogger.DoSPrintf(LevelTrace, a)
|
||||
}
|
||||
|
||||
func SDebug(a ...interface{}) {
|
||||
gLogger.DoSPrintf(LevelDebug, a)
|
||||
}
|
||||
|
||||
func SInfo(a ...interface{}) {
|
||||
gLogger.DoSPrintf(LevelInfo, a)
|
||||
}
|
||||
|
||||
func SWarning(a ...interface{}) {
|
||||
gLogger.DoSPrintf(LevelWarning, a)
|
||||
}
|
||||
|
||||
func SError(a ...interface{}) {
|
||||
gLogger.DoSPrintf(LevelError, a)
|
||||
}
|
||||
|
||||
func (logger *Logger) FormatHeader(buf *Buffer, level slog.Level, callDepth int) {
|
||||
t := time.Now()
|
||||
var file string
|
||||
var line int
|
||||
|
||||
// Release lock while getting caller info - it's expensive.
|
||||
var ok bool
|
||||
_, file, line, ok = runtime.Caller(callDepth)
|
||||
if !ok {
|
||||
file = "???"
|
||||
line = 0
|
||||
var coreList []zapcore.Core
|
||||
if OpenConsole == nil || *OpenConsole {
|
||||
syncerList = append(syncerList, zapcore.AddSync(os.Stdout))
|
||||
}
|
||||
file = filepath.Base(file)
|
||||
|
||||
buf.AppendString("time=\"")
|
||||
buf.AppendString(t.Format("2006/01/02 15:04:05"))
|
||||
buf.AppendString("\"")
|
||||
logger.sBuff.AppendString(" level=")
|
||||
logger.sBuff.AppendString(getStrLevel(level))
|
||||
logger.sBuff.AppendString(" source=")
|
||||
for _, writer := range syncerList {
|
||||
core := zapcore.NewCore(logger.Encoder, writer, LogLevel)
|
||||
coreList = append(coreList, core)
|
||||
}
|
||||
|
||||
buf.AppendString(file)
|
||||
buf.AppendByte(':')
|
||||
buf.AppendInt(int64(line))
|
||||
buf.AppendString(" msg=\"")
|
||||
if logger.CoreList != nil {
|
||||
coreList = append(coreList, logger.CoreList...)
|
||||
}
|
||||
|
||||
core := zapcore.NewTee(coreList...)
|
||||
logger.Logger = zap.New(core, zap.AddCaller(), zap.AddStacktrace(logger), zap.AddCallerSkip(1+logger.Skip))
|
||||
logger.SugaredLogger = logger.Logger.Sugar()
|
||||
}
|
||||
|
||||
func (logger *Logger) Debug(msg string, fields ...zap.Field) {
|
||||
logger.Logger.Debug(msg, fields...)
|
||||
}
|
||||
|
||||
func (logger *Logger) Info(msg string, fields ...zap.Field) {
|
||||
logger.Logger.Info(msg, fields...)
|
||||
}
|
||||
|
||||
func (logger *Logger) Warn(msg string, fields ...zap.Field) {
|
||||
logger.Logger.Warn(msg, fields...)
|
||||
}
|
||||
|
||||
func (logger *Logger) Error(msg string, fields ...zap.Field) {
|
||||
logger.Logger.Error(msg, fields...)
|
||||
}
|
||||
|
||||
func (logger *Logger) StackError(msg string, args ...zap.Field) {
|
||||
logger.stack = true
|
||||
logger.Logger.Log(zapcore.ErrorLevel, msg, args...)
|
||||
logger.stack = false
|
||||
}
|
||||
|
||||
func (logger *Logger) Fatal(msg string, fields ...zap.Field) {
|
||||
gLogger.stack = true
|
||||
logger.Logger.Fatal(msg, fields...)
|
||||
gLogger.stack = false
|
||||
}
|
||||
|
||||
func Debug(msg string, fields ...zap.Field) {
|
||||
gLogger.Logger.Debug(msg, fields...)
|
||||
}
|
||||
|
||||
func Info(msg string, fields ...zap.Field) {
|
||||
gLogger.Logger.Info(msg, fields...)
|
||||
}
|
||||
|
||||
func Warn(msg string, fields ...zap.Field) {
|
||||
gLogger.Logger.Warn(msg, fields...)
|
||||
}
|
||||
|
||||
func Error(msg string, fields ...zap.Field) {
|
||||
gLogger.Logger.Error(msg, fields...)
|
||||
}
|
||||
|
||||
func StackError(msg string, fields ...zap.Field) {
|
||||
gLogger.stack = true
|
||||
gLogger.Logger.Error(msg, fields...)
|
||||
gLogger.stack = false
|
||||
}
|
||||
|
||||
func Fatal(msg string, fields ...zap.Field) {
|
||||
gLogger.stack = true
|
||||
gLogger.Logger.Fatal(msg, fields...)
|
||||
gLogger.stack = false
|
||||
}
|
||||
|
||||
func Debugf(template string, args ...any) {
|
||||
gLogger.SugaredLogger.Debugf(template, args...)
|
||||
}
|
||||
|
||||
func Infof(template string, args ...any) {
|
||||
gLogger.SugaredLogger.Infof(template, args...)
|
||||
}
|
||||
|
||||
func Warnf(template string, args ...any) {
|
||||
gLogger.SugaredLogger.Warnf(template, args...)
|
||||
}
|
||||
|
||||
func Errorf(template string, args ...any) {
|
||||
gLogger.SugaredLogger.Errorf(template, args...)
|
||||
}
|
||||
|
||||
func StackErrorf(template string, args ...any) {
|
||||
gLogger.stack = true
|
||||
gLogger.SugaredLogger.Errorf(template, args...)
|
||||
gLogger.stack = false
|
||||
}
|
||||
|
||||
func Fatalf(template string, args ...any) {
|
||||
gLogger.SugaredLogger.Fatalf(template, args...)
|
||||
}
|
||||
|
||||
func (logger *Logger) SDebug(args ...interface{}) {
|
||||
logger.SugaredLogger.Debugln(args...)
|
||||
}
|
||||
|
||||
func (logger *Logger) SInfo(args ...interface{}) {
|
||||
logger.SugaredLogger.Infoln(args...)
|
||||
}
|
||||
|
||||
func (logger *Logger) SWarn(args ...interface{}) {
|
||||
logger.SugaredLogger.Warnln(args...)
|
||||
}
|
||||
|
||||
func (logger *Logger) SError(args ...interface{}) {
|
||||
logger.SugaredLogger.Errorln(args...)
|
||||
}
|
||||
|
||||
func (logger *Logger) SStackError(args ...interface{}) {
|
||||
gLogger.stack = true
|
||||
logger.SugaredLogger.Errorln(args...)
|
||||
gLogger.stack = false
|
||||
}
|
||||
|
||||
func (logger *Logger) SFatal(args ...interface{}) {
|
||||
gLogger.stack = true
|
||||
logger.SugaredLogger.Fatalln(args...)
|
||||
gLogger.stack = false
|
||||
}
|
||||
|
||||
func SDebug(args ...interface{}) {
|
||||
gLogger.SugaredLogger.Debugln(args...)
|
||||
}
|
||||
|
||||
func SInfo(args ...interface{}) {
|
||||
gLogger.SugaredLogger.Infoln(args...)
|
||||
}
|
||||
|
||||
func SWarn(args ...interface{}) {
|
||||
gLogger.SugaredLogger.Warnln(args...)
|
||||
}
|
||||
|
||||
func SError(args ...interface{}) {
|
||||
gLogger.SugaredLogger.Errorln(args...)
|
||||
}
|
||||
|
||||
func SStackError(args ...interface{}) {
|
||||
gLogger.stack = true
|
||||
gLogger.SugaredLogger.Errorln(args...)
|
||||
gLogger.stack = false
|
||||
}
|
||||
|
||||
func SFatal(args ...interface{}) {
|
||||
gLogger.stack = true
|
||||
gLogger.SugaredLogger.Fatalln(args...)
|
||||
gLogger.stack = false
|
||||
}
|
||||
|
||||
func ErrorField(key string, value error) zap.Field {
|
||||
if value == nil {
|
||||
return zap.String(key, "nil")
|
||||
}
|
||||
return zap.String(key, value.Error())
|
||||
}
|
||||
|
||||
func String(key, value string) zap.Field {
|
||||
return zap.String(key, value)
|
||||
}
|
||||
|
||||
func Int(key string, value int) zap.Field {
|
||||
return zap.Int(key, value)
|
||||
}
|
||||
|
||||
func Int64(key string, value int64) zap.Field {
|
||||
return zap.Int64(key, value)
|
||||
}
|
||||
|
||||
func Int32(key string, value int32) zap.Field {
|
||||
return zap.Int32(key, value)
|
||||
}
|
||||
|
||||
func Int16(key string, value int16) zap.Field {
|
||||
return zap.Int16(key, value)
|
||||
}
|
||||
|
||||
func Int8(key string, value int8) zap.Field {
|
||||
return zap.Int8(key, value)
|
||||
}
|
||||
|
||||
func Uint(key string, value uint) zap.Field {
|
||||
return zap.Uint(key, value)
|
||||
}
|
||||
|
||||
func Uint64(key string, v uint64) zap.Field {
|
||||
return zap.Uint64(key, v)
|
||||
}
|
||||
|
||||
func Uint32(key string, value uint32) zap.Field {
|
||||
return zap.Uint32(key, value)
|
||||
}
|
||||
|
||||
func Uint16(key string, value uint16) zap.Field {
|
||||
return zap.Uint16(key, value)
|
||||
}
|
||||
|
||||
func Uint8(key string, value uint8) zap.Field {
|
||||
return zap.Uint8(key, value)
|
||||
}
|
||||
|
||||
func Float64(key string, v float64) zap.Field {
|
||||
return zap.Float64(key, v)
|
||||
}
|
||||
|
||||
func Bool(key string, v bool) zap.Field {
|
||||
return zap.Bool(key, v)
|
||||
}
|
||||
|
||||
func Bools(key string, v []bool) zap.Field {
|
||||
return zap.Bools(key, v)
|
||||
}
|
||||
|
||||
func Time(key string, v time.Time) zap.Field {
|
||||
return zap.Time(key, v)
|
||||
}
|
||||
|
||||
func Duration(key string, v time.Duration) zap.Field {
|
||||
return zap.Duration(key, v)
|
||||
}
|
||||
|
||||
func Durations(key string, v []time.Duration) zap.Field {
|
||||
return zap.Durations(key, v)
|
||||
}
|
||||
|
||||
func Any(key string, value any) zap.Field {
|
||||
return zap.Any(key, value)
|
||||
}
|
||||
|
||||
@@ -111,15 +111,15 @@ func (netConn *NetConn) doWrite(b []byte) error {
|
||||
}
|
||||
|
||||
// b must not be modified by the others goroutines
|
||||
func (netConn *NetConn) Write(b []byte) error {
|
||||
func (netConn *NetConn) Write(b []byte) (int,error) {
|
||||
netConn.Lock()
|
||||
defer netConn.Unlock()
|
||||
if atomic.LoadInt32(&netConn.closeFlag) == 1 || b == nil {
|
||||
netConn.ReleaseReadMsg(b)
|
||||
return errors.New("conn is close")
|
||||
return 0,errors.New("conn is close")
|
||||
}
|
||||
|
||||
return netConn.doWrite(b)
|
||||
return len(b),netConn.doWrite(b)
|
||||
}
|
||||
|
||||
func (netConn *NetConn) Read(b []byte) (int, error) {
|
||||
@@ -150,7 +150,7 @@ func (netConn *NetConn) WriteMsg(args ...[]byte) error {
|
||||
if atomic.LoadInt32(&netConn.closeFlag) == 1 {
|
||||
return errors.New("conn is close")
|
||||
}
|
||||
return netConn.msgParser.Write(netConn.conn, args...)
|
||||
return netConn.msgParser.Write(netConn, args...)
|
||||
}
|
||||
|
||||
func (netConn *NetConn) WriteRawMsg(args []byte) error {
|
||||
@@ -158,7 +158,8 @@ func (netConn *NetConn) WriteRawMsg(args []byte) error {
|
||||
return errors.New("conn is close")
|
||||
}
|
||||
|
||||
return netConn.Write(args)
|
||||
_,err:= netConn.Write(args)
|
||||
return err
|
||||
}
|
||||
|
||||
func (netConn *NetConn) IsConnected() bool {
|
||||
|
||||
@@ -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))
|
||||
}
|
||||
|
||||
@@ -106,7 +106,7 @@ func (client *KCPClient) dial() net.Conn {
|
||||
return conn
|
||||
}
|
||||
|
||||
log.Warning("connect error ", log.String("error", err.Error()), log.String("Addr", client.Addr))
|
||||
log.Warn("connect error ", log.String("error", err.Error()), log.String("Addr", client.Addr))
|
||||
time.Sleep(client.ConnectInterval)
|
||||
continue
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -203,7 +205,7 @@ func (kp *KCPServer) initSession(session *kcp.UDPSession) {
|
||||
func (kp *KCPServer) run(listener *kcp.Listener) bool {
|
||||
conn, err := listener.Accept()
|
||||
if err != nil {
|
||||
log.Error("accept error", log.String("ListenAddr", kp.kcpCfg.ListenAddr), log.ErrorAttr("err", err))
|
||||
log.Error("accept error", log.String("ListenAddr", kp.kcpCfg.ListenAddr), log.ErrorField("err", err))
|
||||
return false
|
||||
}
|
||||
|
||||
@@ -211,7 +213,7 @@ func (kp *KCPServer) run(listener *kcp.Listener) bool {
|
||||
if len(kp.conns) >= kp.kcpCfg.MaxConnNum {
|
||||
kp.mutexConns.Unlock()
|
||||
conn.Close()
|
||||
log.Warning("too many connections")
|
||||
log.Warn("too many connections")
|
||||
return true
|
||||
}
|
||||
kp.conns[conn] = struct{}{}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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()
|
||||
@@ -102,7 +103,7 @@ func (client *TCPClient) dial() net.Conn {
|
||||
return conn
|
||||
}
|
||||
|
||||
log.Warning("connect error ", log.String("error", err.Error()), log.String("Addr", client.Addr))
|
||||
log.Warn("connect error ", log.String("error", err.Error()), log.String("Addr", client.Addr))
|
||||
time.Sleep(client.ConnectInterval)
|
||||
continue
|
||||
}
|
||||
|
||||
@@ -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")
|
||||
|
||||
@@ -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))
|
||||
}
|
||||
|
||||
@@ -146,7 +146,7 @@ func (server *TCPServer) run() {
|
||||
if len(server.conns) >= server.MaxConnNum {
|
||||
server.mutexConns.Unlock()
|
||||
conn.Close()
|
||||
log.Warning("too many connections")
|
||||
log.Warn("too many connections")
|
||||
continue
|
||||
}
|
||||
|
||||
|
||||
@@ -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")
|
||||
|
||||
@@ -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
|
||||
}
|
||||
@@ -68,15 +74,28 @@ func (handler *WSHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
if len(handler.conns) >= handler.maxConnNum {
|
||||
handler.mutexConns.Unlock()
|
||||
conn.Close()
|
||||
log.Warning("too many connections")
|
||||
log.Warn("too many connections")
|
||||
return
|
||||
}
|
||||
handler.conns[conn] = struct{}{}
|
||||
handler.mutexConns.Unlock()
|
||||
c, ok := conn.NetConn().(*net.TCPConn)
|
||||
if !ok {
|
||||
tlsConn, ok := conn.NetConn().(*tls.Conn)
|
||||
if !ok {
|
||||
log.Error("conn error")
|
||||
return
|
||||
}
|
||||
c, ok = tlsConn.NetConn().(*net.TCPConn)
|
||||
if !ok {
|
||||
log.Error("conn error")
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
conn.UnderlyingConn().(*net.TCPConn).SetLinger(0)
|
||||
conn.UnderlyingConn().(*net.TCPConn).SetNoDelay(true)
|
||||
wsConn := newWSConn(conn, r.Header, handler.pendingWriteNum, handler.maxMsgLen, handler.messageType)
|
||||
c.SetLinger(0)
|
||||
c.SetNoDelay(true)
|
||||
wsConn := newWSConn(conn, r.Header, handler.pendingWriteNum, handler.maxWriteMsgLen, handler.messageType)
|
||||
agent := handler.newAgent(wsConn)
|
||||
agent.Run()
|
||||
|
||||
@@ -101,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))
|
||||
@@ -110,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")
|
||||
@@ -141,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 },
|
||||
},
|
||||
}
|
||||
@@ -154,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,
|
||||
}
|
||||
|
||||
|
||||
108
node/node.go
108
node/node.go
@@ -11,6 +11,7 @@ import (
|
||||
"github.com/duanhf2012/origin/v2/util/buildtime"
|
||||
"github.com/duanhf2012/origin/v2/util/sysprocess"
|
||||
"github.com/duanhf2012/origin/v2/util/timer"
|
||||
"go.uber.org/zap/zapcore"
|
||||
"io"
|
||||
"net/http"
|
||||
_ "net/http/pprof"
|
||||
@@ -48,16 +49,16 @@ func init() {
|
||||
signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM, SingleStop, SignalRetire)
|
||||
|
||||
console.RegisterCommandBool("help", false, "<-help> This help.", usage)
|
||||
console.RegisterCommandString("name", "", "<-name nodeName> Node's name.", setName)
|
||||
console.RegisterCommandString("name", "", "<-name nodeName> node's name.", setName)
|
||||
console.RegisterCommandString("start", "", "<-start nodeid=nodeid> Run originserver.", startNode)
|
||||
console.RegisterCommandString("stop", "", "<-stop nodeid=nodeid> Stop originserver process.", stopNode)
|
||||
console.RegisterCommandString("retire", "", "<-retire nodeid=nodeid> retire originserver process.", retireNode)
|
||||
console.RegisterCommandString("config", "", "<-config path> Configuration file path.", setConfigPath)
|
||||
console.RegisterCommandString("console", "", "<-console true|false> Turn on or off screen log output.", openConsole)
|
||||
console.RegisterCommandString("loglevel", "debug", "<-loglevel debug|release|warning|error|fatal> Set loglevel.", setLevel)
|
||||
console.RegisterCommandString("loglevel", "debug", "<-loglevel debug|info|warn|error|stackerror|fatal> Set loglevel.", setLevel)
|
||||
console.RegisterCommandString("logpath", "", "<-logpath path> Set log file path.", setLogPath)
|
||||
console.RegisterCommandInt("logsize", 0, "<-logsize size> Set log size(MB).", setLogSize)
|
||||
console.RegisterCommandInt("logchannelcap", -1, "<-logchannelcap num> Set log channel cap.", setLogChannelCapNum)
|
||||
console.RegisterCommandInt("logchanlen", 0, "<-logchanlen len> Set log channel len.", setLogChanLen)
|
||||
console.RegisterCommandString("pprof", "", "<-pprof ip:port> Open performance analysis.", setPprof)
|
||||
}
|
||||
|
||||
@@ -156,12 +157,14 @@ func initNode(id string) {
|
||||
nodeId = id
|
||||
err := cluster.GetCluster().Init(GetNodeId(), Setup)
|
||||
if err != nil {
|
||||
log.Error("Init cluster fail", log.ErrorAttr("error", err))
|
||||
log.Error("Init cluster fail", log.ErrorField("error", err))
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
err = initLog()
|
||||
if err != nil {
|
||||
log.Error("Init log fail", log.ErrorField("error", err))
|
||||
os.Exit(1)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -211,22 +214,24 @@ func initNode(id string) {
|
||||
}
|
||||
|
||||
//3.service初始化
|
||||
log.Info("Start running server.")
|
||||
service.Init()
|
||||
}
|
||||
|
||||
func initLog() error {
|
||||
logger := log.GetLogger()
|
||||
if log.LogPath == "" {
|
||||
setLogPath("./log")
|
||||
err := setLogPath("./log")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
localNodeInfo := cluster.GetCluster().GetLocalNodeInfo()
|
||||
filePre := fmt.Sprintf("%s_", localNodeInfo.NodeId)
|
||||
logger, err := log.NewTextLogger(log.LogLevel, log.LogPath, filePre, true, log.LogChannelCap)
|
||||
if err != nil {
|
||||
fmt.Printf("cannot create log file!\n")
|
||||
return err
|
||||
}
|
||||
log.SetLogger(logger)
|
||||
fileName := fmt.Sprintf("%s.log", localNodeInfo.NodeId)
|
||||
logger.FileName = fileName
|
||||
|
||||
logger.Init()
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -323,34 +328,37 @@ func startNode(args interface{}) error {
|
||||
myName, mErr := sysprocess.GetMyProcessName()
|
||||
//当前进程名获取失败,不应该发生
|
||||
if mErr != nil {
|
||||
log.Info("get my process's name is error", log.ErrorAttr("err", mErr))
|
||||
log.Error("get my process's name is error", log.ErrorField("err", mErr))
|
||||
os.Exit(-1)
|
||||
}
|
||||
|
||||
//进程id存在,而且进程名也相同,被认为是当前进程重复运行
|
||||
if cErr == nil && name == myName {
|
||||
log.Info("repeat runs are not allowed", log.String("nodeId", strNodeId), log.Int("processId", processId))
|
||||
log.Error("repeat runs are not allowed", log.String("nodeId", strNodeId), log.Int("processId", processId))
|
||||
os.Exit(-1)
|
||||
}
|
||||
break
|
||||
}
|
||||
|
||||
//2.记录进程id号
|
||||
log.Info("Start running server.")
|
||||
writeProcessPid(strNodeId)
|
||||
timer.StartTimer(10*time.Millisecond, 1000000)
|
||||
|
||||
//3.初始化node
|
||||
defer log.GetLogger().Logger.Sync()
|
||||
initNode(strNodeId)
|
||||
|
||||
//4.运行service
|
||||
service.Start()
|
||||
|
||||
//5.运行集群
|
||||
cluster.GetCluster().Start()
|
||||
err := cluster.GetCluster().Start()
|
||||
if err != nil {
|
||||
log.Error(err.Error())
|
||||
os.Exit(-1)
|
||||
}
|
||||
|
||||
//6.监听程序退出信号&性能报告
|
||||
|
||||
var pProfilerTicker *time.Ticker = &time.Ticker{}
|
||||
if profilerInterval > 0 {
|
||||
pProfilerTicker = time.NewTicker(profilerInterval)
|
||||
@@ -378,7 +386,7 @@ func startNode(args interface{}) error {
|
||||
cluster.GetCluster().Stop()
|
||||
|
||||
log.Info("Server is stop.")
|
||||
log.Close()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -400,8 +408,8 @@ func SetupTemplateFunc(fs ...func() service.IService) {
|
||||
}
|
||||
}
|
||||
|
||||
func SetupTemplate[T any,P templateServicePoint[T]]() {
|
||||
SetupTemplateFunc(func() service.IService{
|
||||
func SetupTemplate[T any, P templateServicePoint[T]]() {
|
||||
SetupTemplateFunc(func() service.IService {
|
||||
var t T
|
||||
return P(&t)
|
||||
})
|
||||
@@ -430,9 +438,11 @@ func openConsole(args interface{}) error {
|
||||
}
|
||||
strOpen := strings.ToLower(strings.TrimSpace(args.(string)))
|
||||
if strOpen == "false" {
|
||||
log.OpenConsole = false
|
||||
bOpenConsole := false
|
||||
log.OpenConsole = &bOpenConsole
|
||||
} else if strOpen == "true" {
|
||||
log.OpenConsole = true
|
||||
bOpenConsole := true
|
||||
log.OpenConsole = &bOpenConsole
|
||||
} else {
|
||||
return errors.New("parameter console error")
|
||||
}
|
||||
@@ -446,20 +456,18 @@ func setLevel(args interface{}) error {
|
||||
|
||||
strlogLevel := strings.TrimSpace(args.(string))
|
||||
switch strlogLevel {
|
||||
case "trace":
|
||||
log.LogLevel = log.LevelTrace
|
||||
case "debug":
|
||||
log.LogLevel = log.LevelDebug
|
||||
log.LogLevel = zapcore.DebugLevel
|
||||
case "info":
|
||||
log.LogLevel = log.LevelInfo
|
||||
case "warning":
|
||||
log.LogLevel = log.LevelWarning
|
||||
log.LogLevel = zapcore.InfoLevel
|
||||
case "warn":
|
||||
log.LogLevel = zapcore.WarnLevel
|
||||
case "error":
|
||||
log.LogLevel = log.LevelError
|
||||
case "stack":
|
||||
log.LogLevel = log.LevelStack
|
||||
log.LogLevel = zapcore.ErrorLevel
|
||||
case "stackerror":
|
||||
log.LogLevel = zapcore.ErrorLevel
|
||||
case "fatal":
|
||||
log.LogLevel = log.LevelFatal
|
||||
log.LogLevel = zapcore.FatalLevel
|
||||
default:
|
||||
return errors.New("unknown level: " + strlogLevel)
|
||||
}
|
||||
@@ -470,52 +478,42 @@ func setLogPath(args interface{}) error {
|
||||
if args == "" {
|
||||
return nil
|
||||
}
|
||||
|
||||
log.LogPath = strings.TrimSpace(args.(string))
|
||||
dir, err := os.Stat(log.LogPath) //这个文件夹不存在
|
||||
if err == nil && dir.IsDir() == false {
|
||||
return errors.New("Not found dir " + log.LogPath)
|
||||
}
|
||||
|
||||
logPath := strings.TrimSpace(args.(string))
|
||||
_, err := os.Stat(logPath)
|
||||
if err != nil {
|
||||
err = os.Mkdir(log.LogPath, os.ModePerm)
|
||||
err = os.MkdirAll(logPath, os.ModePerm)
|
||||
if err != nil {
|
||||
return errors.New("Cannot create dir " + log.LogPath)
|
||||
return errors.New("Cannot create dir " + logPath)
|
||||
}
|
||||
}
|
||||
|
||||
log.LogPath = logPath
|
||||
return nil
|
||||
}
|
||||
|
||||
func setLogSize(args interface{}) error {
|
||||
if args == "" {
|
||||
return nil
|
||||
}
|
||||
|
||||
logSize, ok := args.(int)
|
||||
if ok == false {
|
||||
return errors.New("param logsize is error")
|
||||
}
|
||||
if logSize == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
log.LogSize = int64(logSize) * 1024 * 1024
|
||||
log.MaxSize = logSize
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func setLogChannelCapNum(args interface{}) error {
|
||||
if args == "" {
|
||||
return nil
|
||||
}
|
||||
|
||||
logChannelCap, ok := args.(int)
|
||||
func setLogChanLen(args interface{}) error {
|
||||
logChanLen, ok := args.(int)
|
||||
if ok == false {
|
||||
return errors.New("param logsize is error")
|
||||
}
|
||||
|
||||
if logChannelCap == -1 {
|
||||
if logChanLen == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
log.LogChannelCap = logChannelCap
|
||||
log.LogChanLen = logChanLen
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -75,7 +75,7 @@ func (cs *CallSet) AddPending(call *Call) {
|
||||
|
||||
if call.Seq == 0 {
|
||||
cs.pendingLock.Unlock()
|
||||
log.Stack("call is error.")
|
||||
log.StackError("call is error.")
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
@@ -32,7 +32,7 @@ type IRealClient interface {
|
||||
SetConn(conn *network.NetConn)
|
||||
Close(waitDone bool)
|
||||
|
||||
AsyncCall(NodeId string, timeout time.Duration, rpcHandler IRpcHandler, serviceMethod string, callback reflect.Value, args interface{}, replyParam interface{}, cancelable bool) (CancelRpc, error)
|
||||
AsyncCall(NodeId string, timeout time.Duration, rpcHandler IRpcHandler, serviceMethod string, callback reflect.Value, args interface{}, replyParam interface{}) (CancelRpc, error)
|
||||
Go(NodeId string, timeout time.Duration, rpcHandler IRpcHandler, noReply bool, serviceMethod string, args interface{}, reply interface{}) *Call
|
||||
RawGo(NodeId string, timeout time.Duration, rpcHandler IRpcHandler, processor IRpcProcessor, noReply bool, rpcMethodId uint32, serviceMethod string, rawArgs []byte, reply interface{}) *Call
|
||||
IsConnected() bool
|
||||
@@ -104,7 +104,7 @@ func (client *Client) processRpcResponse(responseData []byte) error {
|
||||
//rc.conn.ReleaseReadMsg(bytes)
|
||||
if err != nil {
|
||||
processor.ReleaseRpcResponse(response.RpcResponseData)
|
||||
log.Error("rpcClient Unmarshal head error", log.ErrorAttr("error", err))
|
||||
log.Error("rpcClient Unmarshal head error", log.ErrorField("error", err))
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -116,7 +116,7 @@ func (client *Client) processRpcResponse(responseData []byte) error {
|
||||
if len(response.RpcResponseData.GetReply()) > 0 {
|
||||
err = processor.Unmarshal(response.RpcResponseData.GetReply(), v.Reply)
|
||||
if err != nil {
|
||||
log.Error("rpcClient Unmarshal body failed", log.ErrorAttr("error", err))
|
||||
log.Error("rpcClient Unmarshal body failed", log.ErrorField("error", err))
|
||||
v.Err = err
|
||||
}
|
||||
}
|
||||
@@ -203,7 +203,7 @@ func (client *Client) rawGo(nodeId string, w IWriter, timeout time.Duration, rpc
|
||||
}
|
||||
if err != nil {
|
||||
client.RemovePending(call.Seq)
|
||||
log.Error("WriteMsg is fail", log.ErrorAttr("error", err))
|
||||
log.Error("WriteMsg is fail", log.ErrorField("error", err))
|
||||
call.Seq = 0
|
||||
call.DoError(err)
|
||||
}
|
||||
@@ -211,7 +211,7 @@ func (client *Client) rawGo(nodeId string, w IWriter, timeout time.Duration, rpc
|
||||
return call
|
||||
}
|
||||
|
||||
func (client *Client) asyncCall(nodeId string, w IWriter, timeout time.Duration, rpcHandler IRpcHandler, serviceMethod string, callback reflect.Value, args interface{}, replyParam interface{}, cancelable bool) (CancelRpc, error) {
|
||||
func (client *Client) asyncCall(nodeId string, w IWriter, timeout time.Duration, rpcHandler IRpcHandler, serviceMethod string, callback reflect.Value, args interface{}, replyParam interface{}) (CancelRpc, error) {
|
||||
processorType, processor := GetProcessorType(args)
|
||||
InParam, herr := processor.Marshal(args)
|
||||
if herr != nil {
|
||||
@@ -264,10 +264,7 @@ func (client *Client) asyncCall(nodeId string, w IWriter, timeout time.Duration,
|
||||
return emptyCancelRpc, err
|
||||
}
|
||||
|
||||
if cancelable {
|
||||
rpcCancel := RpcCancel{CallSeq: seq, Cli: client}
|
||||
return rpcCancel.CancelRpc, nil
|
||||
}
|
||||
|
||||
return emptyCancelRpc, nil
|
||||
rpcCancel := RpcCancel{CallSeq: seq, Cli: client}
|
||||
return rpcCancel.CancelRpc, nil
|
||||
}
|
||||
|
||||
@@ -90,7 +90,7 @@ func (lc *LClient) RawGo(nodeId string, timeout time.Duration, rpcHandler IRpcHa
|
||||
return pLocalRpcServer.selfNodeRpcHandlerGo(timeout, processor, lc.selfClient, true, serviceName, rpcMethodId, serviceName, nil, nil, rawArgs)
|
||||
}
|
||||
|
||||
func (lc *LClient) AsyncCall(nodeId string, timeout time.Duration, rpcHandler IRpcHandler, serviceMethod string, callback reflect.Value, args interface{}, reply interface{}, cancelable bool) (CancelRpc, error) {
|
||||
func (lc *LClient) AsyncCall(nodeId string, timeout time.Duration, rpcHandler IRpcHandler, serviceMethod string, callback reflect.Value, args interface{}, reply interface{}) (CancelRpc, error) {
|
||||
pLocalRpcServer := rpcHandler.GetRpcServer()()
|
||||
|
||||
//判断是否是同一服务
|
||||
@@ -109,7 +109,7 @@ func (lc *LClient) AsyncCall(nodeId string, timeout time.Duration, rpcHandler IR
|
||||
}
|
||||
|
||||
//其他的rpcHandler的处理器
|
||||
cancelRpc, err := pLocalRpcServer.selfNodeRpcHandlerAsyncGo(timeout, lc.selfClient, rpcHandler, false, serviceName, serviceMethod, args, reply, callback, cancelable)
|
||||
cancelRpc, err := pLocalRpcServer.selfNodeRpcHandlerAsyncGo(timeout, lc.selfClient, rpcHandler, false, serviceName, serviceMethod, args, reply, callback)
|
||||
if err != nil {
|
||||
callback.Call([]reflect.Value{reflect.ValueOf(reply), reflect.ValueOf(err)})
|
||||
}
|
||||
@@ -121,9 +121,6 @@ func NewLClient(localNodeId string, callSet *CallSet) *Client {
|
||||
client := &Client{}
|
||||
client.clientId = atomic.AddUint32(&clientSeq, 1)
|
||||
client.targetNodeId = localNodeId
|
||||
//client.maxCheckCallRpcCount = DefaultMaxCheckCallRpcCount
|
||||
//client.callRpcTimeout = DefaultRpcTimeout
|
||||
|
||||
lClient := &LClient{}
|
||||
lClient.selfClient = client
|
||||
client.IRealClient = lClient
|
||||
|
||||
@@ -42,7 +42,7 @@ func (server *BaseServer) selfNodeRpcHandlerGo(timeout time.Duration, processor
|
||||
rpcHandler := server.rpcHandleFinder.FindRpcHandler(handlerName)
|
||||
if rpcHandler == nil {
|
||||
err := errors.New("service method " + serviceMethod + " not config!")
|
||||
log.Error("service method not config", log.String("serviceMethod", serviceMethod), log.ErrorAttr("error", err))
|
||||
log.Error("service method not config", log.String("serviceMethod", serviceMethod), log.ErrorField("error", err))
|
||||
pCall.Seq = 0
|
||||
pCall.DoError(err)
|
||||
|
||||
@@ -74,7 +74,7 @@ func (server *BaseServer) selfNodeRpcHandlerGo(timeout time.Duration, processor
|
||||
var err error
|
||||
req.inParam, err = rpcHandler.UnmarshalInParam(processor, serviceMethod, rpcMethodId, rawArgs)
|
||||
if err != nil {
|
||||
log.Error("unmarshalInParam is failed", log.String("serviceMethod", serviceMethod), log.Uint32("rpcMethodId", rpcMethodId), log.ErrorAttr("error", err))
|
||||
log.Error("unmarshalInParam is failed", log.String("serviceMethod", serviceMethod), log.Uint32("rpcMethodId", rpcMethodId), log.ErrorField("error", err))
|
||||
pCall.Seq = 0
|
||||
pCall.DoError(err)
|
||||
ReleaseRpcRequest(req)
|
||||
@@ -90,12 +90,12 @@ func (server *BaseServer) selfNodeRpcHandlerGo(timeout time.Duration, processor
|
||||
byteReturns, err := req.rpcProcessor.Marshal(Returns)
|
||||
if err != nil {
|
||||
Err = ConvertError(err)
|
||||
log.Error("returns data cannot be marshal", log.Uint64("seq", callSeq), log.ErrorAttr("error", err))
|
||||
log.Error("returns data cannot be marshal", log.Uint64("seq", callSeq), log.ErrorField("error", err))
|
||||
} else {
|
||||
err = req.rpcProcessor.Unmarshal(byteReturns, reply)
|
||||
if err != nil {
|
||||
Err = ConvertError(err)
|
||||
log.Error("returns data cannot be Unmarshal", log.Uint64("seq", callSeq), log.ErrorAttr("error", err))
|
||||
log.Error("returns data cannot be Unmarshal", log.Uint64("seq", callSeq), log.ErrorField("error", err))
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -127,7 +127,7 @@ func (server *BaseServer) selfNodeRpcHandlerGo(timeout time.Duration, processor
|
||||
return pCall
|
||||
}
|
||||
|
||||
func (server *BaseServer) selfNodeRpcHandlerAsyncGo(timeout time.Duration, client *Client, callerRpcHandler IRpcHandler, noReply bool, handlerName string, serviceMethod string, args interface{}, reply interface{}, callback reflect.Value, cancelable bool) (CancelRpc, error) {
|
||||
func (server *BaseServer) selfNodeRpcHandlerAsyncGo(timeout time.Duration, client *Client, callerRpcHandler IRpcHandler, noReply bool, handlerName string, serviceMethod string, args interface{}, reply interface{}, callback reflect.Value) (CancelRpc, error) {
|
||||
rpcHandler := server.rpcHandleFinder.FindRpcHandler(handlerName)
|
||||
if rpcHandler == nil {
|
||||
err := errors.New("service method " + serviceMethod + " not config!")
|
||||
@@ -266,7 +266,7 @@ func (server *BaseServer) processRpcRequest(data []byte, connTag string, wrRespo
|
||||
req.inParam, err = rpcHandler.UnmarshalInParam(req.rpcProcessor, req.RpcRequestData.GetServiceMethod(), req.RpcRequestData.GetRpcMethodId(), req.RpcRequestData.GetInParam())
|
||||
if err != nil {
|
||||
rErr := "Call Rpc " + req.RpcRequestData.GetServiceMethod() + " Param error " + err.Error()
|
||||
log.Error("call rpc param error", log.String("serviceMethod", req.RpcRequestData.GetServiceMethod()), log.ErrorAttr("error", err))
|
||||
log.Error("call rpc param error", log.String("serviceMethod", req.RpcRequestData.GetServiceMethod()), log.ErrorField("error", err))
|
||||
if req.requestHandle != nil {
|
||||
req.requestHandle(nil, RpcError(rErr))
|
||||
} else {
|
||||
|
||||
@@ -50,7 +50,7 @@ func (nc *NatsClient) Go(nodeId string, timeout time.Duration, rpcHandler IRpcHa
|
||||
_, processor := GetProcessorType(args)
|
||||
InParam, err := processor.Marshal(args)
|
||||
if err != nil {
|
||||
log.Error("Marshal is fail", log.ErrorAttr("error", err))
|
||||
log.Error("Marshal is fail", log.ErrorField("error", err))
|
||||
call := MakeCall()
|
||||
call.DoError(err)
|
||||
return call
|
||||
@@ -63,8 +63,8 @@ func (nc *NatsClient) RawGo(nodeId string, timeout time.Duration, rpcHandler IRp
|
||||
return nc.client.rawGo(nodeId, nc, timeout, rpcHandler, processor, noReply, rpcMethodId, serviceMethod, rawArgs, reply)
|
||||
}
|
||||
|
||||
func (nc *NatsClient) AsyncCall(nodeId string, timeout time.Duration, rpcHandler IRpcHandler, serviceMethod string, callback reflect.Value, args interface{}, replyParam interface{}, cancelable bool) (CancelRpc, error) {
|
||||
cancelRpc, err := nc.client.asyncCall(nodeId, nc, timeout, rpcHandler, serviceMethod, callback, args, replyParam, cancelable)
|
||||
func (nc *NatsClient) AsyncCall(nodeId string, timeout time.Duration, rpcHandler IRpcHandler, serviceMethod string, callback reflect.Value, args interface{}, replyParam interface{}) (CancelRpc, error) {
|
||||
cancelRpc, err := nc.client.asyncCall(nodeId, nc, timeout, rpcHandler, serviceMethod, callback, args, replyParam)
|
||||
if err != nil {
|
||||
callback.Call([]reflect.Value{reflect.ValueOf(replyParam), reflect.ValueOf(err)})
|
||||
}
|
||||
|
||||
@@ -48,7 +48,7 @@ func (ns *NatsServer) Start() error {
|
||||
|
||||
ns.natsConn, err = nats.Connect(ns.natsUrl, options...)
|
||||
if err != nil {
|
||||
log.Error("Connect to nats fail", log.String("natsUrl", ns.natsUrl), log.ErrorAttr("err", err))
|
||||
log.Error("Connect to nats fail", log.String("natsUrl", ns.natsUrl), log.ErrorField("err", err))
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -77,7 +77,7 @@ func (ns *NatsServer) WriteResponse(processor IRpcProcessor, nodeId string, serv
|
||||
defer processor.ReleaseRpcResponse(rpcResponse.RpcResponseData)
|
||||
|
||||
if err != nil {
|
||||
log.Error("marshal RpcResponseData failed", log.String("serviceMethod", serviceMethod), log.ErrorAttr("error", err))
|
||||
log.Error("marshal RpcResponseData failed", log.String("serviceMethod", serviceMethod), log.ErrorField("error", err))
|
||||
return
|
||||
}
|
||||
|
||||
@@ -86,7 +86,7 @@ func (ns *NatsServer) WriteResponse(processor IRpcProcessor, nodeId string, serv
|
||||
if ns.compressBytesLen > 0 && len(bytes) >= ns.compressBytesLen {
|
||||
compressBuff, err = compressor.CompressBlock(bytes)
|
||||
if err != nil {
|
||||
log.Error("CompressBlock failed", log.String("serviceMethod", serviceMethod), log.ErrorAttr("error", err))
|
||||
log.Error("CompressBlock failed", log.String("serviceMethod", serviceMethod), log.ErrorField("error", err))
|
||||
return
|
||||
}
|
||||
if len(compressBuff) < len(bytes) {
|
||||
@@ -106,7 +106,7 @@ func (ns *NatsServer) WriteResponse(processor IRpcProcessor, nodeId string, serv
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
log.Error("WriteMsg error,Rpc return is fail", log.String("nodeId", nodeId), log.String("serviceMethod", serviceMethod), log.ErrorAttr("error", err))
|
||||
log.Error("WriteMsg error,Rpc return is fail", log.String("nodeId", nodeId), log.String("serviceMethod", serviceMethod), log.ErrorField("error", err))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -6,7 +6,7 @@ import (
|
||||
"github.com/duanhf2012/origin/v2/network"
|
||||
"math"
|
||||
"reflect"
|
||||
"runtime"
|
||||
|
||||
"sync/atomic"
|
||||
"time"
|
||||
)
|
||||
@@ -49,7 +49,7 @@ func (rc *RClient) Go(nodeId string, timeout time.Duration, rpcHandler IRpcHandl
|
||||
_, processor := GetProcessorType(args)
|
||||
InParam, err := processor.Marshal(args)
|
||||
if err != nil {
|
||||
log.Error("Marshal is fail", log.ErrorAttr("error", err))
|
||||
log.Error("Marshal is fail", log.ErrorField("error", err))
|
||||
call := MakeCall()
|
||||
call.DoError(err)
|
||||
return call
|
||||
@@ -62,8 +62,8 @@ func (rc *RClient) RawGo(nodeId string, timeout time.Duration, rpcHandler IRpcHa
|
||||
return rc.selfClient.rawGo(nodeId, rc, timeout, rpcHandler, processor, noReply, rpcMethodId, serviceMethod, rawArgs, reply)
|
||||
}
|
||||
|
||||
func (rc *RClient) AsyncCall(nodeId string, timeout time.Duration, rpcHandler IRpcHandler, serviceMethod string, callback reflect.Value, args interface{}, replyParam interface{}, cancelable bool) (CancelRpc, error) {
|
||||
cancelRpc, err := rc.selfClient.asyncCall(nodeId, rc, timeout, rpcHandler, serviceMethod, callback, args, replyParam, cancelable)
|
||||
func (rc *RClient) AsyncCall(nodeId string, timeout time.Duration, rpcHandler IRpcHandler, serviceMethod string, callback reflect.Value, args interface{}, replyParam interface{}) (CancelRpc, error) {
|
||||
cancelRpc, err := rc.selfClient.asyncCall(nodeId, rc, timeout, rpcHandler, serviceMethod, callback, args, replyParam)
|
||||
if err != nil {
|
||||
callback.Call([]reflect.Value{reflect.ValueOf(replyParam), reflect.ValueOf(err)})
|
||||
}
|
||||
@@ -74,10 +74,7 @@ func (rc *RClient) AsyncCall(nodeId string, timeout time.Duration, rpcHandler IR
|
||||
func (rc *RClient) Run() {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
buf := make([]byte, 4096)
|
||||
l := runtime.Stack(buf, false)
|
||||
errString := fmt.Sprint(r)
|
||||
log.Dump(string(buf[:l]), log.String("error", errString))
|
||||
log.StackError(fmt.Sprint(r))
|
||||
}
|
||||
}()
|
||||
|
||||
@@ -89,7 +86,7 @@ func (rc *RClient) Run() {
|
||||
for {
|
||||
bytes, err := rc.conn.ReadMsg()
|
||||
if err != nil {
|
||||
log.Error("RClient read msg is failed", log.ErrorAttr("error", err))
|
||||
log.Error("RClient read msg is failed", log.ErrorField("error", err))
|
||||
return
|
||||
}
|
||||
|
||||
@@ -130,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
|
||||
|
||||
@@ -6,7 +6,7 @@ import (
|
||||
"github.com/duanhf2012/origin/v2/event"
|
||||
"github.com/duanhf2012/origin/v2/log"
|
||||
"reflect"
|
||||
"runtime"
|
||||
|
||||
"strings"
|
||||
"time"
|
||||
"unicode"
|
||||
@@ -164,13 +164,6 @@ func (handler *RpcHandler) suitableMethods(method reflect.Method) error {
|
||||
//取出输入参数类型
|
||||
var rpcMethodInfo RpcMethodInfo
|
||||
typ := method.Type
|
||||
if typ.NumOut() != 1 {
|
||||
return fmt.Errorf("%s The number of returned arguments must be 1", method.Name)
|
||||
}
|
||||
|
||||
if typ.Out(0).String() != "error" {
|
||||
return fmt.Errorf("%s The return parameter must be of type error", method.Name)
|
||||
}
|
||||
|
||||
if typ.NumIn() < 2 || typ.NumIn() > 4 {
|
||||
return fmt.Errorf("%s Unsupported parameter format", method.Name)
|
||||
@@ -183,6 +176,18 @@ func (handler *RpcHandler) suitableMethods(method reflect.Method) error {
|
||||
rpcMethodInfo.hasResponder = true
|
||||
}
|
||||
|
||||
if rpcMethodInfo.hasResponder && typ.NumOut() > 0 {
|
||||
return fmt.Errorf("%s should not have return parameters", method.Name)
|
||||
}
|
||||
|
||||
if !rpcMethodInfo.hasResponder && typ.NumOut() != 1 {
|
||||
return fmt.Errorf("%s The number of returned arguments must be 1", method.Name)
|
||||
}
|
||||
|
||||
if !rpcMethodInfo.hasResponder && typ.Out(0).String() != "error" {
|
||||
return fmt.Errorf("%s The return parameter must be of type error", method.Name)
|
||||
}
|
||||
|
||||
for i := parIdx; i < typ.NumIn(); i++ {
|
||||
if handler.isExportedOrBuiltinType(typ.In(i)) == false {
|
||||
return fmt.Errorf("%s Unsupported parameter types", method.Name)
|
||||
@@ -220,10 +225,7 @@ func (handler *RpcHandler) RegisterRpc(rpcHandler IRpcHandler) error {
|
||||
func (handler *RpcHandler) HandlerRpcResponseCB(call *Call) {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
buf := make([]byte, 4096)
|
||||
l := runtime.Stack(buf, false)
|
||||
errString := fmt.Sprint(r)
|
||||
log.Dump(string(buf[:l]), log.String("error", errString))
|
||||
log.StackError(fmt.Sprint(r))
|
||||
}
|
||||
}()
|
||||
|
||||
@@ -242,10 +244,7 @@ func (handler *RpcHandler) HandlerRpcRequest(request *RpcRequest) {
|
||||
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
buf := make([]byte, 4096)
|
||||
l := runtime.Stack(buf, false)
|
||||
errString := fmt.Sprint(r)
|
||||
log.Dump(string(buf[:l]), log.String("error", errString))
|
||||
log.StackError(fmt.Sprint(r))
|
||||
rpcErr := RpcError("call error : core dumps")
|
||||
if request.requestHandle != nil {
|
||||
request.requestHandle(nil, rpcErr)
|
||||
@@ -313,9 +312,11 @@ func (handler *RpcHandler) HandlerRpcRequest(request *RpcRequest) {
|
||||
|
||||
requestHandle := request.requestHandle
|
||||
returnValues := v.method.Func.Call(paramList)
|
||||
errInter := returnValues[0].Interface()
|
||||
if errInter != nil {
|
||||
err = errInter.(error)
|
||||
if len(returnValues) > 0 {
|
||||
errInter := returnValues[0].Interface()
|
||||
if errInter != nil {
|
||||
err = errInter.(error)
|
||||
}
|
||||
}
|
||||
|
||||
if v.hasResponder == false && requestHandle != nil {
|
||||
@@ -439,7 +440,7 @@ func (handler *RpcHandler) goRpc(processor IRpcProcessor, bCast bool, nodeId str
|
||||
err, pClientList := handler.funcRpcClient(nodeId, serviceMethod, false, pClientList)
|
||||
if len(pClientList) == 0 {
|
||||
if err != nil {
|
||||
log.Error("call serviceMethod is failed", log.String("serviceMethod", serviceMethod), log.ErrorAttr("error", err))
|
||||
log.Error("call serviceMethod is failed", log.String("serviceMethod", serviceMethod), log.ErrorField("error", err))
|
||||
} else {
|
||||
log.Error("cannot find serviceMethod", log.String("serviceMethod", serviceMethod))
|
||||
}
|
||||
@@ -468,7 +469,7 @@ func (handler *RpcHandler) callRpc(timeout time.Duration, nodeId string, service
|
||||
pClientList := make([]*Client, 0, maxClusterNode)
|
||||
err, pClientList := handler.funcRpcClient(nodeId, serviceMethod, false, pClientList)
|
||||
if err != nil {
|
||||
log.Error("Call serviceMethod is failed", log.ErrorAttr("error", err))
|
||||
log.Error("Call serviceMethod is failed", log.ErrorField("error", err))
|
||||
return err
|
||||
} else if len(pClientList) <= 0 {
|
||||
err = errors.New("Call serviceMethod is error:cannot find " + serviceMethod)
|
||||
@@ -532,8 +533,7 @@ func (handler *RpcHandler) asyncCallRpc(timeout time.Duration, nodeId string, se
|
||||
}
|
||||
|
||||
//2.rpcClient调用
|
||||
//如果调用本结点服务
|
||||
return pClientList[0].AsyncCall(pClientList[0].GetTargetNodeId(), timeout, handler.rpcHandler, serviceMethod, fVal, args, reply, false)
|
||||
return pClientList[0].AsyncCall(pClientList[0].GetTargetNodeId(), timeout, handler.rpcHandler, serviceMethod, fVal, args, reply, )
|
||||
}
|
||||
|
||||
func (handler *RpcHandler) GetName() string {
|
||||
@@ -592,7 +592,7 @@ func (handler *RpcHandler) RawGoNode(rpcProcessorType RpcProcessorType, nodeId s
|
||||
pClientList := make([]*Client, 0, 1)
|
||||
err, pClientList := handler.funcRpcClient(nodeId, serviceName, false, pClientList)
|
||||
if len(pClientList) == 0 || err != nil {
|
||||
log.Error("call serviceMethod is failed", log.ErrorAttr("error", err))
|
||||
log.Error("call serviceMethod is failed", log.ErrorField("error", err))
|
||||
return err
|
||||
}
|
||||
if len(pClientList) > 1 {
|
||||
|
||||
@@ -27,9 +27,6 @@ func (rn *RpcNats) NewNatsClient(targetNodeId string,localNodeId string,callSet
|
||||
|
||||
client.clientId = atomic.AddUint32(&clientSeq, 1)
|
||||
client.targetNodeId = targetNodeId
|
||||
//client.maxCheckCallRpcCount = DefaultMaxCheckCallRpcCount
|
||||
//client.callRpcTimeout = DefaultRpcTimeout
|
||||
|
||||
natsClient := &rn.NatsClient
|
||||
natsClient.localNodeId = localNodeId
|
||||
natsClient.client = &client
|
||||
|
||||
@@ -7,7 +7,7 @@ import (
|
||||
"math"
|
||||
"net"
|
||||
"reflect"
|
||||
"runtime"
|
||||
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
@@ -31,7 +31,7 @@ type IServer interface {
|
||||
|
||||
selfNodeRpcHandlerGo(timeout time.Duration, processor IRpcProcessor, client *Client, noReply bool, handlerName string, rpcMethodId uint32, serviceMethod string, args interface{}, reply interface{}, rawArgs []byte) *Call
|
||||
myselfRpcHandlerGo(client *Client, handlerName string, serviceMethod string, args interface{}, callBack reflect.Value, reply interface{}) error
|
||||
selfNodeRpcHandlerAsyncGo(timeout time.Duration, client *Client, callerRpcHandler IRpcHandler, noReply bool, handlerName string, serviceMethod string, args interface{}, reply interface{}, callback reflect.Value, cancelable bool) (CancelRpc, error)
|
||||
selfNodeRpcHandlerAsyncGo(timeout time.Duration, client *Client, callerRpcHandler IRpcHandler, noReply bool, handlerName string, serviceMethod string, args interface{}, reply interface{}, callback reflect.Value) (CancelRpc, error)
|
||||
}
|
||||
|
||||
type writeResponse func(processor IRpcProcessor, connTag string, serviceMethod string, seq uint64, reply interface{}, rpcError RpcError)
|
||||
@@ -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
|
||||
@@ -130,7 +132,7 @@ func (agent *RpcAgent) WriteResponse(processor IRpcProcessor, connTag string, se
|
||||
defer processor.ReleaseRpcResponse(rpcResponse.RpcResponseData)
|
||||
|
||||
if errM != nil {
|
||||
log.Error("marshal RpcResponseData failed", log.String("serviceMethod", serviceMethod), log.ErrorAttr("error", errM))
|
||||
log.Error("marshal RpcResponseData failed", log.String("serviceMethod", serviceMethod), log.ErrorField("error", errM))
|
||||
return
|
||||
}
|
||||
|
||||
@@ -141,7 +143,7 @@ func (agent *RpcAgent) WriteResponse(processor IRpcProcessor, connTag string, se
|
||||
|
||||
compressBuff, cErr = compressor.CompressBlock(bytes)
|
||||
if cErr != nil {
|
||||
log.Error("CompressBlock failed", log.String("serviceMethod", serviceMethod), log.ErrorAttr("error", cErr))
|
||||
log.Error("CompressBlock failed", log.String("serviceMethod", serviceMethod), log.ErrorField("error", cErr))
|
||||
return
|
||||
}
|
||||
if len(compressBuff) < len(bytes) {
|
||||
@@ -155,17 +157,14 @@ func (agent *RpcAgent) WriteResponse(processor IRpcProcessor, connTag string, se
|
||||
compressor.CompressBufferCollection(compressBuff)
|
||||
}
|
||||
if errM != nil {
|
||||
log.Error("WriteMsg error,Rpc return is fail", log.String("serviceMethod", serviceMethod), log.ErrorAttr("error", errM))
|
||||
log.Error("WriteMsg error,Rpc return is fail", log.String("serviceMethod", serviceMethod), log.ErrorField("error", errM))
|
||||
}
|
||||
}
|
||||
|
||||
func (agent *RpcAgent) Run() {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
buf := make([]byte, 4096)
|
||||
l := runtime.Stack(buf, false)
|
||||
errString := fmt.Sprint(r)
|
||||
log.Dump(string(buf[:l]), log.String("error", errString))
|
||||
log.StackError(fmt.Sprint(r))
|
||||
}
|
||||
}()
|
||||
|
||||
@@ -173,7 +172,7 @@ func (agent *RpcAgent) Run() {
|
||||
data, err := agent.conn.ReadMsg()
|
||||
if err != nil {
|
||||
//will close conn
|
||||
log.Error("read message is error", log.String("remoteAddress", agent.conn.RemoteAddr().String()), log.ErrorAttr("error", err))
|
||||
log.Error("read message is error", log.String("remoteAddress", agent.conn.RemoteAddr().String()), log.ErrorField("error", err))
|
||||
break
|
||||
}
|
||||
|
||||
@@ -181,7 +180,7 @@ func (agent *RpcAgent) Run() {
|
||||
if err != nil {
|
||||
//will close conn
|
||||
agent.conn.ReleaseReadMsg(data)
|
||||
log.Error("processRpcRequest is error", log.String("remoteAddress", agent.conn.RemoteAddr().String()), log.ErrorAttr("error", err))
|
||||
log.Error("processRpcRequest is error", log.String("remoteAddress", agent.conn.RemoteAddr().String()), log.ErrorField("error", err))
|
||||
|
||||
break
|
||||
}
|
||||
|
||||
@@ -11,6 +11,7 @@ import (
|
||||
"github.com/duanhf2012/origin/v2/log"
|
||||
rpcHandle "github.com/duanhf2012/origin/v2/rpc"
|
||||
"github.com/duanhf2012/origin/v2/util/timer"
|
||||
"slices"
|
||||
)
|
||||
|
||||
const InitModuleId = 1e9
|
||||
@@ -46,7 +47,7 @@ type Module struct {
|
||||
moduleName string //模块名称
|
||||
parent IModule //父亲
|
||||
self IModule //自己
|
||||
child map[uint32]IModule //孩子们
|
||||
child []IModule //孩子们
|
||||
mapActiveTimer map[timer.ITimer]struct{}
|
||||
mapActiveIdTimer map[uint64]timer.ITimer
|
||||
dispatcher *timer.Dispatcher //timer
|
||||
@@ -93,10 +94,7 @@ func (m *Module) AddModule(module IModule) (uint32, error) {
|
||||
pAddModule.moduleId = m.NewModuleId()
|
||||
}
|
||||
|
||||
if m.child == nil {
|
||||
m.child = map[uint32]IModule{}
|
||||
}
|
||||
_, ok := m.child[module.GetModuleId()]
|
||||
_,ok := m.ancestor.getBaseModule().(*Module).descendants[module.GetModuleId()]
|
||||
if ok == true {
|
||||
return 0, fmt.Errorf("exists module id %d", module.GetModuleId())
|
||||
}
|
||||
@@ -109,29 +107,33 @@ func (m *Module) AddModule(module IModule) (uint32, error) {
|
||||
pAddModule.eventHandler = event.NewEventHandler()
|
||||
pAddModule.eventHandler.Init(m.eventHandler.GetEventProcessor())
|
||||
pAddModule.IConcurrent = m.IConcurrent
|
||||
|
||||
m.child = append(m.child,module)
|
||||
m.ancestor.getBaseModule().(*Module).descendants[module.GetModuleId()] = module
|
||||
|
||||
err := module.OnInit()
|
||||
if err != nil {
|
||||
delete(m.ancestor.getBaseModule().(*Module).descendants, module.GetModuleId())
|
||||
m.child = m.child[:len(m.child)-1]
|
||||
log.Error("module OnInit error",log.String("ModuleName",module.GetModuleName()),log.ErrorField("err",err))
|
||||
return 0, err
|
||||
}
|
||||
|
||||
m.child[module.GetModuleId()] = module
|
||||
m.ancestor.getBaseModule().(*Module).descendants[module.GetModuleId()] = module
|
||||
|
||||
log.Debug("Add module " + module.GetModuleName() + " completed")
|
||||
return module.GetModuleId(), nil
|
||||
}
|
||||
|
||||
func (m *Module) ReleaseModule(moduleId uint32) {
|
||||
pModule := m.GetModule(moduleId).getBaseModule().(*Module)
|
||||
pModule.self.OnRelease()
|
||||
log.Debug("Release module " + pModule.GetModuleName())
|
||||
|
||||
//释放子孙
|
||||
for id := range pModule.child {
|
||||
m.ReleaseModule(id)
|
||||
for i:=len(pModule.child)-1; i>=0; i-- {
|
||||
m.ReleaseModule(pModule.child[i].GetModuleId())
|
||||
}
|
||||
|
||||
pModule.self.OnRelease()
|
||||
pModule.GetEventHandler().Destroy()
|
||||
log.Debug("Release module " + pModule.GetModuleName())
|
||||
|
||||
for pTimer := range pModule.mapActiveTimer {
|
||||
pTimer.Cancel()
|
||||
}
|
||||
@@ -140,7 +142,10 @@ func (m *Module) ReleaseModule(moduleId uint32) {
|
||||
t.Cancel()
|
||||
}
|
||||
|
||||
delete(m.child, moduleId)
|
||||
m.child = slices.DeleteFunc(m.child, func(module IModule) bool {
|
||||
return module.GetModuleId() == moduleId
|
||||
})
|
||||
|
||||
delete(m.ancestor.getBaseModule().(*Module).descendants, moduleId)
|
||||
|
||||
//清理被删除的Module
|
||||
@@ -281,7 +286,7 @@ func (m *Module) SafeNewTicker(tickerId *uint64, d time.Duration, AdditionData i
|
||||
|
||||
func (m *Module) CancelTimerId(timerId *uint64) bool {
|
||||
if timerId == nil || *timerId == 0 {
|
||||
log.Warning("timerId is invalid")
|
||||
log.Warn("timerId is invalid")
|
||||
return false
|
||||
}
|
||||
|
||||
@@ -292,7 +297,7 @@ func (m *Module) CancelTimerId(timerId *uint64) bool {
|
||||
|
||||
t, ok := m.mapActiveIdTimer[*timerId]
|
||||
if ok == false {
|
||||
log.Stack("cannot find timer id ", log.Uint64("timerId", *timerId))
|
||||
log.StackError("cannot find timer id ", log.Uint64("timerId", *timerId))
|
||||
return false
|
||||
}
|
||||
|
||||
|
||||
@@ -11,7 +11,6 @@ import (
|
||||
"github.com/duanhf2012/origin/v2/rpc"
|
||||
"github.com/duanhf2012/origin/v2/util/timer"
|
||||
"reflect"
|
||||
"runtime"
|
||||
"strconv"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
@@ -193,7 +192,7 @@ func (s *Service) run() {
|
||||
break
|
||||
}
|
||||
if s.profiler != nil {
|
||||
analyzer = s.profiler.Push("[Req]" + rpcRequest.RpcRequestData.GetServiceMethod())
|
||||
analyzer = s.profiler.Push("[RpcReq]" + rpcRequest.RpcRequestData.GetServiceMethod()+"."+strconv.Itoa(int(rpcRequest.RpcRequestData.GetRpcMethodId())))
|
||||
}
|
||||
|
||||
s.GetRpcHandler().HandlerRpcRequest(rpcRequest)
|
||||
@@ -261,17 +260,16 @@ func (s *Service) SetName(serviceName string) {
|
||||
func (s *Service) Release() {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
buf := make([]byte, 4096)
|
||||
l := runtime.Stack(buf, false)
|
||||
errString := fmt.Sprint(r)
|
||||
log.Dump(string(buf[:l]), log.String("error", errString))
|
||||
log.StackError(fmt.Sprint(r))
|
||||
}
|
||||
}()
|
||||
|
||||
if atomic.AddInt32(&s.isRelease, -1) == -1 {
|
||||
s.self.OnRelease()
|
||||
for i:=len(s.child)-1; i>=0; i-- {
|
||||
s.ReleaseModule(s.child[i].GetModuleId())
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func (s *Service) OnRelease() {
|
||||
@@ -436,6 +434,7 @@ func (s *Service) SetEventChannelNum(num int) {
|
||||
}
|
||||
}
|
||||
|
||||
// Deprecated: replace it with the OpenConcurrent function
|
||||
func (s *Service) SetGoRoutineNum(goroutineNum int32) bool {
|
||||
//已经开始状态不允许修改协程数量,打开性能分析器不允许开多线程
|
||||
if s.startStatus == true || s.profiler != nil {
|
||||
|
||||
@@ -23,7 +23,7 @@ func Init() {
|
||||
for _,s := range setupServiceList {
|
||||
err := s.OnInit()
|
||||
if err != nil {
|
||||
log.Error("Failed to initialize "+s.GetName()+" service",log.ErrorAttr("err",err))
|
||||
log.Error("Failed to initialize "+s.GetName()+" service",log.ErrorField("err",err))
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
74
sysmodule/blueprintmodule/BlueprintModule.go
Normal file
74
sysmodule/blueprintmodule/BlueprintModule.go
Normal file
@@ -0,0 +1,74 @@
|
||||
package blueprintmodule
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/duanhf2012/origin/v2/service"
|
||||
"github.com/duanhf2012/origin/v2/util/blueprint"
|
||||
"sync/atomic"
|
||||
)
|
||||
|
||||
type BlueprintModule struct {
|
||||
service.Module
|
||||
|
||||
bp blueprint.Blueprint
|
||||
|
||||
execDefFilePath string
|
||||
graphFilePath string
|
||||
|
||||
seedGraphID int64
|
||||
|
||||
mapGraph map[int64]blueprint.IGraph
|
||||
}
|
||||
|
||||
func (m *BlueprintModule) Init(execDefFilePath string, graphFilePath string) error {
|
||||
m.execDefFilePath = execDefFilePath
|
||||
m.graphFilePath = graphFilePath
|
||||
|
||||
m.mapGraph = make(map[int64]blueprint.IGraph, 1024)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *BlueprintModule) OnInit() error {
|
||||
if m.execDefFilePath == "" || m.graphFilePath == "" {
|
||||
return fmt.Errorf("execDefFilePath or graphFilePath is empty")
|
||||
}
|
||||
|
||||
m.seedGraphID = 1
|
||||
return m.bp.Init(m.execDefFilePath, m.graphFilePath, m)
|
||||
}
|
||||
|
||||
func (m *BlueprintModule) CreateGraph(graphName string) int64 {
|
||||
graphID := atomic.AddInt64(&m.seedGraphID, 1)
|
||||
graph := m.bp.Create(graphName, graphID)
|
||||
if graph == nil {
|
||||
return 0
|
||||
}
|
||||
m.mapGraph[graphID] = graph
|
||||
|
||||
return graphID
|
||||
}
|
||||
|
||||
func (m *BlueprintModule) GetGraph(graphID int64) (blueprint.IGraph, error) {
|
||||
graph, ok := m.mapGraph[graphID]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("graph not found,graphID:%d", graphID)
|
||||
}
|
||||
return graph, nil
|
||||
}
|
||||
|
||||
func (m *BlueprintModule) Do(graphID int64, entranceID int64, args ...any) error {
|
||||
graph, err := m.GetGraph(graphID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return graph.Do(entranceID, args...)
|
||||
}
|
||||
|
||||
func (m *BlueprintModule) TriggerEvent(graphID int64, eventID int64, args ...any) error {
|
||||
graph, err := m.GetGraph(graphID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return graph.Do(eventID, args...)
|
||||
}
|
||||
@@ -248,7 +248,7 @@ func (ch *ConsumerGroupHandler) ConsumeClaim(session sarama.ConsumerGroupSession
|
||||
select {
|
||||
case msg := <-claim.Messages():
|
||||
if msg == nil {
|
||||
log.SWarning("claim will exit", log.Any("topic", claim.Topic()), log.Any("Partition", claim.Partition()))
|
||||
log.SWarn("claim will exit", log.Any("topic", claim.Topic()), log.Any("Partition", claim.Partition()))
|
||||
return nil
|
||||
}
|
||||
ch.AppendMsg(session, msg)
|
||||
|
||||
@@ -86,7 +86,7 @@ func (p *Producer) asyncRun() {
|
||||
asyncReturn := sm.Metadata.(*AsyncReturn)
|
||||
asyncReturn.chanReturn <- asyncReturn
|
||||
case em := <-p.Errors():
|
||||
log.Error("async kafkamodule error", log.ErrorAttr("err", em.Err))
|
||||
log.Error("async kafkamodule error", log.ErrorField("err", em.Err))
|
||||
if em.Msg.Metadata == nil {
|
||||
break
|
||||
}
|
||||
|
||||
@@ -95,7 +95,7 @@ func (m *MySQLModule) Begin() (*Tx, error) {
|
||||
var txDBModule Tx
|
||||
txDb, err := m.db.Begin()
|
||||
if err != nil {
|
||||
log.Error("Begin error:%s", err.Error())
|
||||
log.Error("Begin error", log.ErrorField("err",err))
|
||||
return &txDBModule, err
|
||||
}
|
||||
txDBModule.slowDuration = m.slowDuration
|
||||
@@ -155,7 +155,7 @@ func (m *MySQLModule) runPing() {
|
||||
for {
|
||||
select {
|
||||
case <-m.pingCoroutine.pintExit:
|
||||
log.Error("RunPing stopping %s...", fmt.Sprintf("%T", m))
|
||||
log.Error("RunPing stopping",log.String("url", m.url),log.String("dbname", m.dbname))
|
||||
return
|
||||
case <-m.pingCoroutine.tickerPing.C:
|
||||
if m.db != nil {
|
||||
@@ -221,12 +221,12 @@ func query(slowDuration time.Duration, db dbControl, strQuery string, args ...in
|
||||
datasetList.blur = true
|
||||
|
||||
if checkArgs(args) != nil {
|
||||
log.Error("CheckArgs is error :%s", strQuery)
|
||||
log.Error("CheckArgs is error",log.String("sql",strQuery))
|
||||
return &datasetList, fmt.Errorf("checkArgs is error")
|
||||
}
|
||||
|
||||
if db == nil {
|
||||
log.Error("cannot connect database:%s", strQuery)
|
||||
log.Error("cannot connect database",log.String("sql", strQuery))
|
||||
return &datasetList, fmt.Errorf("cannot connect database")
|
||||
}
|
||||
|
||||
@@ -235,10 +235,10 @@ func query(slowDuration time.Duration, db dbControl, strQuery string, args ...in
|
||||
timeFuncPass := time.Since(TimeFuncStart)
|
||||
|
||||
if checkSlow(slowDuration, timeFuncPass) {
|
||||
log.Error("DBModule QueryEx Time %s , Query :%s , args :%+v", timeFuncPass, strQuery, args)
|
||||
log.Error("Query slow",log.Int64("time_ms",timeFuncPass.Milliseconds()),log.String("sql", strQuery), log.Any("args",args))
|
||||
}
|
||||
if err != nil {
|
||||
log.Error("Query:%s(%v)", strQuery, err)
|
||||
log.Error("Query error", log.String("sql",strQuery),log.ErrorField("err",err))
|
||||
if rows != nil {
|
||||
rows.Close()
|
||||
}
|
||||
@@ -278,8 +278,8 @@ func query(slowDuration time.Duration, db dbControl, strQuery string, args ...in
|
||||
hasRet := rows.NextResultSet()
|
||||
|
||||
if hasRet == false {
|
||||
if rows.Err() != nil {
|
||||
log.Error("Query:%s(%+v)", strQuery, rows)
|
||||
if rowErr :=rows.Err();rowErr != nil {
|
||||
log.Error("NextResultSet error", log.String("sql",strQuery), log.ErrorField("err",rowErr))
|
||||
}
|
||||
break
|
||||
}
|
||||
@@ -291,12 +291,12 @@ func query(slowDuration time.Duration, db dbControl, strQuery string, args ...in
|
||||
func exec(slowDuration time.Duration, db dbControl, strSql string, args ...interface{}) (*DBResult, error) {
|
||||
ret := &DBResult{}
|
||||
if db == nil {
|
||||
log.Error("cannot connect database:%s", strSql)
|
||||
log.Error("cannot connect database", log.String("sql",strSql))
|
||||
return ret, fmt.Errorf("cannot connect database")
|
||||
}
|
||||
|
||||
if checkArgs(args) != nil {
|
||||
log.Error("CheckArgs is error :%s", strSql)
|
||||
log.Error("CheckArgs is error", log.String("sql",strSql))
|
||||
return ret, fmt.Errorf("checkArgs is error")
|
||||
}
|
||||
|
||||
@@ -304,10 +304,10 @@ func exec(slowDuration time.Duration, db dbControl, strSql string, args ...inter
|
||||
res, err := db.Exec(strSql, args...)
|
||||
timeFuncPass := time.Since(TimeFuncStart)
|
||||
if checkSlow(slowDuration, timeFuncPass) {
|
||||
log.Error("DBModule QueryEx Time %s , Query :%s , args :%+v", timeFuncPass, strSql, args)
|
||||
log.Error("Exec slow",log.Int64("time_ms",timeFuncPass.Milliseconds()),log.String("sql",strSql),log.Any("args",args) )
|
||||
}
|
||||
if err != nil {
|
||||
log.Error("Exec:%s(%v)", strSql, err)
|
||||
log.Error("Exec error",log.String("sql",strSql),log.ErrorField("err", err))
|
||||
return nil, err
|
||||
}
|
||||
|
||||
|
||||
1
sysmodule/mysqlmodule/timernode.go
Normal file
1
sysmodule/mysqlmodule/timernode.go
Normal file
@@ -0,0 +1 @@
|
||||
package mysqlmodule
|
||||
@@ -7,7 +7,6 @@ import (
|
||||
"github.com/duanhf2012/origin/v2/service"
|
||||
"github.com/gin-gonic/gin"
|
||||
"io"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
@@ -69,28 +68,28 @@ func (gm *GinModule) eventHandler(ev event.IEvent) {
|
||||
|
||||
func (gm *GinModule) Start() {
|
||||
gm.srv.Addr = gm.listenAddr
|
||||
log.Info("http start listen", slog.Any("addr", gm.listenAddr))
|
||||
log.Info("http start listen", log.Any("addr", gm.listenAddr))
|
||||
go func() {
|
||||
err := gm.srv.ListenAndServe()
|
||||
if err != nil {
|
||||
log.Error("ListenAndServe error", slog.Any("error", err.Error()))
|
||||
log.Error("ListenAndServe error", log.Any("error", err.Error()))
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
func (gm *GinModule) StartTLS(certFile, keyFile string) {
|
||||
log.Info("http start listen", slog.Any("addr", gm.listenAddr))
|
||||
log.Info("http start listen", log.Any("addr", gm.listenAddr))
|
||||
go func() {
|
||||
err := gm.srv.ListenAndServeTLS(certFile, keyFile)
|
||||
if err != nil {
|
||||
log.Fatal("ListenAndServeTLS error", slog.Any("error", err.Error()))
|
||||
log.Fatal("ListenAndServeTLS error", log.Any("error", err.Error()))
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
func (gm *GinModule) Stop(ctx context.Context) {
|
||||
if err := gm.srv.Shutdown(ctx); err != nil {
|
||||
log.Error("Server Shutdown", slog.Any("error", err))
|
||||
log.Error("Server Shutdown", log.Any("error", err))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -210,7 +209,7 @@ func (gm *GinModule) handleMethod(httpMethod, relativePath string, handlers ...S
|
||||
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
log.Error("GinModule process timeout", slog.Any("path", c.Request.URL.Path))
|
||||
log.Error("GinModule process timeout", log.Any("path", c.Request.URL.Path))
|
||||
c.AbortWithStatus(http.StatusRequestTimeout)
|
||||
case <-chanWait:
|
||||
}
|
||||
|
||||
@@ -9,7 +9,6 @@ import (
|
||||
"github.com/duanhf2012/origin/v2/service"
|
||||
"github.com/xtaci/kcp-go/v5"
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
"runtime"
|
||||
"sync"
|
||||
)
|
||||
|
||||
@@ -20,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
|
||||
@@ -57,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
|
||||
}
|
||||
|
||||
@@ -66,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()
|
||||
}
|
||||
@@ -78,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)
|
||||
@@ -112,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
|
||||
|
||||
@@ -167,10 +175,7 @@ func (km *KcpModule) NewAgent(conn network.Conn) network.Agent {
|
||||
func (c *Client) Run() {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
buf := make([]byte, 4096)
|
||||
l := runtime.Stack(buf, false)
|
||||
errString := fmt.Sprint(r)
|
||||
log.Dump(string(buf[:l]), log.String("error", errString))
|
||||
log.StackError(fmt.Sprint(r))
|
||||
}
|
||||
}()
|
||||
|
||||
@@ -179,7 +184,7 @@ func (c *Client) Run() {
|
||||
c.kcpConn.SetReadDeadline(*c.kcpModule.kcpCfg.ReadDeadlineMill)
|
||||
msgBuff, err := c.kcpConn.ReadMsg()
|
||||
if err != nil {
|
||||
log.Debug("read client failed", log.ErrorAttr("error", err), log.String("clientId", c.id))
|
||||
log.Debug("read client failed", log.ErrorField("error", err), log.String("clientId", c.id))
|
||||
break
|
||||
}
|
||||
|
||||
|
||||
@@ -8,7 +8,6 @@ import (
|
||||
"github.com/duanhf2012/origin/v2/network/processor"
|
||||
"github.com/duanhf2012/origin/v2/service"
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
"runtime"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
@@ -21,6 +20,7 @@ type TcpModule struct {
|
||||
mapClient map[string]*Client
|
||||
process processor.IRawProcessor
|
||||
tcpCfg *TcpCfg
|
||||
newClientIdHandler func() string
|
||||
}
|
||||
|
||||
type TcpPackType int8
|
||||
@@ -36,6 +36,7 @@ type TcpPack struct {
|
||||
Type TcpPackType //0表示连接 1表示断开 2表示数据
|
||||
ClientId string
|
||||
Data interface{}
|
||||
rawData []byte
|
||||
RecyclerReaderBytes func(data []byte)
|
||||
}
|
||||
|
||||
@@ -52,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 //写超时
|
||||
}
|
||||
@@ -69,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)
|
||||
@@ -88,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()
|
||||
}
|
||||
@@ -100,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)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -110,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
|
||||
@@ -125,10 +139,7 @@ func (slf *Client) GetId() string {
|
||||
func (slf *Client) Run() {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
buf := make([]byte, 4096)
|
||||
l := runtime.Stack(buf, false)
|
||||
errString := fmt.Sprint(r)
|
||||
log.Dump(string(buf[:l]), log.String("error", errString))
|
||||
log.StackError(fmt.Sprint(r))
|
||||
}
|
||||
}()
|
||||
|
||||
@@ -137,15 +148,15 @@ func (slf *Client) Run() {
|
||||
slf.tcpConn.SetReadDeadline(slf.tcpModule.tcpServer.ReadDeadline)
|
||||
bytes, err := slf.tcpConn.ReadMsg()
|
||||
if err != nil {
|
||||
log.Debug("read client failed", log.ErrorAttr("error", err), log.String("clientId", slf.id))
|
||||
log.Debug("read client failed", log.ErrorField("error", err), log.String("clientId", slf.id))
|
||||
break
|
||||
}
|
||||
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()}})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -185,7 +196,7 @@ func (tm *TcpModule) Close(clientId string) {
|
||||
client.tcpConn.Close()
|
||||
}
|
||||
|
||||
log.SWarning("close client:", clientId)
|
||||
log.SWarn("close client:", clientId)
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
@@ -2,24 +2,27 @@ 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 {
|
||||
service.Module
|
||||
|
||||
wsServer network.WSServer
|
||||
WSServer network.WSServer
|
||||
|
||||
mapClientLocker sync.RWMutex
|
||||
mapClient map[string]*WSClient
|
||||
process processor.IRawProcessor
|
||||
wsCfg *WSCfg
|
||||
newClientIdHandler func() string
|
||||
}
|
||||
|
||||
type WSClient struct {
|
||||
@@ -32,8 +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
|
||||
@@ -57,18 +67,31 @@ func (ws *WSModule) OnInit() error {
|
||||
return fmt.Errorf("please call the Init function correctly")
|
||||
}
|
||||
|
||||
ws.wsServer.MaxConnNum = ws.wsCfg.MaxConnNum
|
||||
ws.wsServer.PendingWriteNum = ws.wsCfg.PendingWriteNum
|
||||
ws.wsServer.MaxMsgLen = ws.wsCfg.MaxMsgLen
|
||||
ws.wsServer.Addr = ws.wsCfg.ListenAddr
|
||||
ws.WSServer.MaxConnNum = ws.wsCfg.MaxConnNum
|
||||
ws.WSServer.PendingWriteNum = ws.wsCfg.PendingWriteNum
|
||||
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
|
||||
}
|
||||
|
||||
//3.设置解析处理器
|
||||
// 设置解析处理器
|
||||
ws.process.SetByteOrder(ws.wsCfg.LittleEndian)
|
||||
|
||||
ws.mapClient = make(map[string]*WSClient, ws.wsServer.MaxConnNum)
|
||||
ws.wsServer.NewAgent = ws.NewWSClient
|
||||
ws.mapClient = make(map[string]*WSClient, ws.WSServer.MaxConnNum)
|
||||
ws.WSServer.NewAgent = ws.NewWSClient
|
||||
|
||||
//4.设置网络事件处理
|
||||
// 设置网络事件处理
|
||||
ws.GetEventProcessor().RegEventReceiverFunc(event.Sys_Event_WebSocket, ws.GetEventHandler(), ws.wsEventHandler)
|
||||
|
||||
return nil
|
||||
@@ -79,8 +102,12 @@ 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()
|
||||
return ws.WSServer.Start()
|
||||
}
|
||||
|
||||
func (ws *WSModule) wsEventHandler(ev event.IEvent) {
|
||||
@@ -91,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)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -103,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
|
||||
|
||||
@@ -120,12 +146,12 @@ func (wc *WSClient) Run() {
|
||||
for {
|
||||
bytes, err := wc.wsConn.ReadMsg()
|
||||
if err != nil {
|
||||
log.Debug("read client is error", log.String("clientId", wc.id), log.ErrorAttr("err", err))
|
||||
log.Debug("read client is error", log.String("clientId", wc.id), log.ErrorField("err", err))
|
||||
break
|
||||
}
|
||||
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}})
|
||||
@@ -143,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()
|
||||
@@ -197,3 +239,7 @@ func (ws *WSModule) SendRawMsg(clientId string, msg []byte) error {
|
||||
ws.mapClientLocker.Unlock()
|
||||
return client.wsConn.WriteMsg(msg)
|
||||
}
|
||||
|
||||
func (ws *WSModule) SetMessageType(messageType int) {
|
||||
ws.WSServer.SetMessageType(messageType)
|
||||
}
|
||||
|
||||
@@ -66,7 +66,7 @@ func (m *RedisModule) Init(redisCfg *ConfigRedis) {
|
||||
}
|
||||
c, err := redis.Dial("tcp", redisServer, opt...)
|
||||
if err != nil {
|
||||
log.Error("Connect redis fail reason:%v", err)
|
||||
log.Error("Connect redis fail", log.ErrorField("err",err))
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -79,7 +79,7 @@ func (m *RedisModule) Init(redisCfg *ConfigRedis) {
|
||||
}
|
||||
_, err := c.Do("PING")
|
||||
if err != nil {
|
||||
log.Error("Do PING fail reason:%v", err)
|
||||
log.Error("Do PING fail reason", log.ErrorField("err",err))
|
||||
return err
|
||||
}
|
||||
return err
|
||||
@@ -101,7 +101,7 @@ func (m *RedisModule) getConn() (redis.Conn, error) {
|
||||
if conn.Err() != nil {
|
||||
err := conn.Err()
|
||||
if err != nil {
|
||||
log.Error("get Conn have error,reason:%v", err)
|
||||
log.Error("get Conn have error", log.ErrorField("err",err))
|
||||
}
|
||||
conn.Close()
|
||||
return nil, err
|
||||
@@ -118,7 +118,7 @@ func (m *RedisModule) TestPingRedis() error {
|
||||
|
||||
err = m.redisPool.TestOnBorrow(conn, time.Now())
|
||||
if err != nil {
|
||||
log.Error("TestOnBorrow fail,reason:%v", err)
|
||||
log.Error("TestOnBorrow fail", log.ErrorField("err",err))
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -171,7 +171,7 @@ func (m *RedisModule) setStringByExpire(key, value, expire interface{}) error {
|
||||
}
|
||||
|
||||
if retErr != nil {
|
||||
log.Error("setStringByExpire fail,reason:%v", retErr)
|
||||
log.Error("setStringByExpire fail", log.ErrorField("err",retErr))
|
||||
return retErr
|
||||
}
|
||||
|
||||
@@ -254,7 +254,7 @@ func (m *RedisModule) setMuchStringByExpire(mapInfo map[interface{}]interface{},
|
||||
}
|
||||
|
||||
if serr != nil {
|
||||
log.Error("setMuchStringByExpire fail,reason:%v", serr)
|
||||
log.Error("setMuchStringByExpire fail",log.ErrorField("err",serr))
|
||||
conn.Do("DISCARD")
|
||||
return serr
|
||||
} else {
|
||||
@@ -262,7 +262,7 @@ func (m *RedisModule) setMuchStringByExpire(mapInfo map[interface{}]interface{},
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
log.Error("setMuchStringByExpire fail,reason:%v", err)
|
||||
log.Error("setMuchStringByExpire fail", log.ErrorField("err",err))
|
||||
}
|
||||
|
||||
return err
|
||||
@@ -277,7 +277,7 @@ func (m *RedisModule) GetString(key interface{}) (string, error) {
|
||||
|
||||
ret, err := conn.Do("GET", key)
|
||||
if err != nil {
|
||||
log.Error("GetString fail,reason:%v", err)
|
||||
log.Error("GetString fail", log.ErrorField("err",err))
|
||||
return "", err
|
||||
}
|
||||
|
||||
@@ -298,7 +298,7 @@ func (m *RedisModule) GetStringJSON(key string, st interface{}) error {
|
||||
|
||||
ret, err := conn.Do("GET", key)
|
||||
if err != nil {
|
||||
log.Error("GetStringJSON fail,reason:%v", err)
|
||||
log.Error("GetStringJSON fail", log.ErrorField("err",err))
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -315,7 +315,7 @@ func (m *RedisModule) GetStringJSON(key string, st interface{}) error {
|
||||
}
|
||||
|
||||
if err = json.Unmarshal(str, st); err != nil {
|
||||
log.Error("GetStringJSON fail json.Unmarshal is error:%s,%s,reason:%v", key, string(str), err)
|
||||
log.Errorf("GetStringJSON fail json.Unmarshal is error:%s,%s,reason:%v", key, string(str), err)
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -336,13 +336,13 @@ func (m *RedisModule) GetStringMap(keys []string) (retMap map[string]string, err
|
||||
// 开始Send数据
|
||||
err = conn.Send("MULTI")
|
||||
if err != nil {
|
||||
log.Error("GetMuchString fail %v", err)
|
||||
log.Errorf("GetMuchString fail %v", err)
|
||||
return nil, err
|
||||
}
|
||||
for _, val := range keys {
|
||||
err = conn.Send("GET", val)
|
||||
if err != nil {
|
||||
log.Error("GetMuchString fail,reason:%v", err)
|
||||
log.Errorf("GetMuchString fail,reason:%v", err)
|
||||
conn.Do("DISCARD")
|
||||
return nil, err
|
||||
}
|
||||
@@ -351,7 +351,7 @@ func (m *RedisModule) GetStringMap(keys []string) (retMap map[string]string, err
|
||||
// 执行命令
|
||||
ret, err := conn.Do("EXEC")
|
||||
if err != nil {
|
||||
log.Error("GetMuchString fail %v", err)
|
||||
log.Errorf("GetMuchString fail %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -383,7 +383,7 @@ func (m *RedisModule) ExistsKey(key interface{}) (bool, error) {
|
||||
|
||||
ret, err := conn.Do("EXISTS", key)
|
||||
if err != nil {
|
||||
log.Error("ExistsKey fail, reason:%v", err)
|
||||
log.Errorf("ExistsKey fail, reason:%v", err)
|
||||
return false, err
|
||||
}
|
||||
retValue, ok := ret.(int64)
|
||||
@@ -404,7 +404,7 @@ func (m *RedisModule) DelString(key interface{}) error {
|
||||
|
||||
ret, err := conn.Do("DEL", key)
|
||||
if err != nil {
|
||||
log.Error("DelString fail, reason:%v", err)
|
||||
log.Errorf("DelString fail, reason:%v", err)
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -439,7 +439,7 @@ func (m *RedisModule) DelStringKeyList(keys []interface{}) (map[interface{}]bool
|
||||
for _, val := range keys {
|
||||
err = conn.Send("DEL", val)
|
||||
if err != nil {
|
||||
log.Error("DelMuchString fail,reason:%v", err)
|
||||
log.Errorf("DelMuchString fail,reason:%v", err)
|
||||
conn.Do("DISCARD")
|
||||
return nil, err
|
||||
}
|
||||
@@ -448,7 +448,7 @@ func (m *RedisModule) DelStringKeyList(keys []interface{}) (map[interface{}]bool
|
||||
ret, err := conn.Do("EXEC")
|
||||
|
||||
if err != nil {
|
||||
log.Error("DelMuchString fail,reason:%v", err)
|
||||
log.Errorf("DelMuchString fail,reason:%v", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -484,7 +484,7 @@ func (m *RedisModule) SetHash(redisKey, hashKey, value interface{}) error {
|
||||
|
||||
_, retErr := conn.Do("HSET", redisKey, hashKey, value)
|
||||
if retErr != nil {
|
||||
log.Error("SetHash fail,reason:%v", retErr)
|
||||
log.Errorf("SetHash fail,reason:%v", retErr)
|
||||
}
|
||||
|
||||
return retErr
|
||||
@@ -502,7 +502,7 @@ func (m *RedisModule) GetAllHashJSON(redisKey string) (map[string]string, error)
|
||||
|
||||
value, err := conn.Do("HGETALL", redisKey)
|
||||
if err != nil {
|
||||
log.Error("GetAllHashJSON fail,reason:%v", err)
|
||||
log.Errorf("GetAllHashJSON fail,reason:%v", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -522,7 +522,7 @@ func (m *RedisModule) GetHash(redisKey interface{}, fieldKey interface{}) (strin
|
||||
|
||||
value, err := conn.Do("HGET", redisKey, fieldKey)
|
||||
if err != nil {
|
||||
log.Error("GetHashValueByKey fail,reason:%v", err)
|
||||
log.Errorf("GetHashValueByKey fail,reason:%v", err)
|
||||
return "", err
|
||||
}
|
||||
if value == nil {
|
||||
@@ -545,7 +545,7 @@ func (m *RedisModule) GetMuchHash(args ...interface{}) ([]string, error) {
|
||||
|
||||
value, err := conn.Do("HMGET", args...)
|
||||
if err != nil {
|
||||
log.Error("GetHashValueByKey fail,reason:%v", err)
|
||||
log.Errorf("GetHashValueByKey fail,reason:%v", err)
|
||||
return nil, err
|
||||
}
|
||||
if value == nil {
|
||||
@@ -582,7 +582,7 @@ func (m *RedisModule) ScanMatchKeys(cursorValue int, redisKey string, count int)
|
||||
|
||||
value, err := conn.Do("SCAN", cursorValue, "match", redisKey, "count", count)
|
||||
if err != nil {
|
||||
log.Error("GetHashValueByKey fail,reason:%v", err)
|
||||
log.Errorf("GetHashValueByKey fail,reason:%v", err)
|
||||
return nextCursorValue, nil, err
|
||||
}
|
||||
if value == nil {
|
||||
@@ -618,7 +618,7 @@ func (m *RedisModule) SetHashMapJSON(redisKey string, mapFieldValue map[interfac
|
||||
if err == nil {
|
||||
_, err = conn.Do("HSET", redisKey, symbol, temp)
|
||||
if err != nil {
|
||||
log.Error("SetMuchHashJSON fail,reason:%v", err)
|
||||
log.Errorf("SetMuchHashJSON fail,reason:%v", err)
|
||||
conn.Send("DISCARD")
|
||||
return err
|
||||
}
|
||||
@@ -627,7 +627,7 @@ func (m *RedisModule) SetHashMapJSON(redisKey string, mapFieldValue map[interfac
|
||||
// 执行命令
|
||||
_, err = conn.Do("EXEC")
|
||||
if err != nil {
|
||||
log.Error("SetMuchHashJSON fail,reason:%v", err)
|
||||
log.Errorf("SetMuchHashJSON fail,reason:%v", err)
|
||||
conn.Send("DISCARD")
|
||||
}
|
||||
return err
|
||||
@@ -642,7 +642,7 @@ func (m *RedisModule) DelHash(args ...interface{}) error {
|
||||
|
||||
_, retErr := conn.Do("HDEL", args...)
|
||||
if retErr != nil {
|
||||
log.Error("DelMuchHash fail,reason:%v", retErr)
|
||||
log.Errorf("DelMuchHash fail,reason:%v", retErr)
|
||||
}
|
||||
return retErr
|
||||
}
|
||||
@@ -678,7 +678,7 @@ func (m *RedisModule) setListPush(setType string, args ...interface{}) error {
|
||||
|
||||
_, retErr := conn.Do(setType, args...)
|
||||
if retErr != nil {
|
||||
log.Error("setList fail,reason:%v", retErr)
|
||||
log.Errorf("setList fail,reason:%v", retErr)
|
||||
}
|
||||
return retErr
|
||||
}
|
||||
@@ -705,7 +705,7 @@ func (m *RedisModule) LRangeList(key string, start, end int) ([]string, error) {
|
||||
|
||||
reply, err := conn.Do("lrange", key, start, end)
|
||||
if err != nil {
|
||||
log.Error("SetListJSONRpush fail,reason:%v", err)
|
||||
log.Errorf("SetListJSONRpush fail,reason:%v", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -722,7 +722,7 @@ func (m *RedisModule) GetListLen(key string) (int, error) {
|
||||
|
||||
reply, err := conn.Do("LLEN", key)
|
||||
if err != nil {
|
||||
log.Error("GetListLen fail,reason:%v", err)
|
||||
log.Errorf("GetListLen fail,reason:%v", err)
|
||||
return -1, err
|
||||
}
|
||||
return redis.Int(reply, err)
|
||||
@@ -748,7 +748,7 @@ func (m *RedisModule) LTrimList(key string, start, end int) error {
|
||||
|
||||
_, err = conn.Do("LTRIM", key, start, end)
|
||||
if err != nil {
|
||||
log.Error("LtrimListValue fail,reason:%v", err)
|
||||
log.Errorf("LtrimListValue fail,reason:%v", err)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
@@ -849,7 +849,7 @@ func (m *RedisModule) ZADDInsertJson(key string, score float64, value interface{
|
||||
}
|
||||
_, err = conn.Do("ZADD", key, score, JsonValue)
|
||||
if err != nil {
|
||||
log.Error("ZADDInsertJson fail,reason:%v", err)
|
||||
log.Errorf("ZADDInsertJson fail,reason:%v", err)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
@@ -865,7 +865,7 @@ func (m *RedisModule) ZADDInsert(key string, score float64, Data interface{}) er
|
||||
|
||||
_, err = conn.Do("ZADD", key, score, Data)
|
||||
if err != nil {
|
||||
log.Error("ZADDInsert fail,reason:%v", err)
|
||||
log.Errorf("ZADDInsert fail,reason:%v", err)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
@@ -1088,7 +1088,7 @@ func (m *RedisModule) HincrbyHashInt(redisKey, hashKey string, value int) error
|
||||
|
||||
_, retErr := conn.Do("HINCRBY", redisKey, hashKey, value)
|
||||
if retErr != nil {
|
||||
log.Error("HincrbyHashInt fail,reason:%v", retErr)
|
||||
log.Errorf("HincrbyHashInt fail,reason:%v", retErr)
|
||||
}
|
||||
|
||||
return retErr
|
||||
@@ -1103,7 +1103,7 @@ func (m *RedisModule) EXPlREInsert(key string, TTl int) error {
|
||||
|
||||
_, err = conn.Do("expire", key, TTl)
|
||||
if err != nil {
|
||||
log.Error("expire fail,reason:%v", err)
|
||||
log.Errorf("expire fail,reason:%v", err)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
@@ -1129,7 +1129,7 @@ func (m *RedisModule) Keys(key string) ([]string, error) {
|
||||
|
||||
ret, err := conn.Do("KEYS", key)
|
||||
if err != nil {
|
||||
log.Error("KEYS fail, reason:%v", err)
|
||||
log.Errorf("KEYS fail, reason:%v", err)
|
||||
return nil, err
|
||||
}
|
||||
retList, ok := ret.([]interface{})
|
||||
|
||||
@@ -83,6 +83,7 @@ type HttpSession struct {
|
||||
sessionDone chan *HttpSession
|
||||
}
|
||||
|
||||
// Deprecated: replace it with the GinModule
|
||||
type HttpService struct {
|
||||
service.Service
|
||||
|
||||
|
||||
@@ -104,11 +104,11 @@ func (cs *CustomerSubscriber) UnSubscribe() {
|
||||
func (cs *CustomerSubscriber) LoadLastIndex() {
|
||||
for {
|
||||
if atomic.LoadInt32(&cs.isStop) != 0 {
|
||||
log.Info("topic ", cs.topic, " out of subscription")
|
||||
log.SInfo("topic ", cs.topic, " out of subscription")
|
||||
break
|
||||
}
|
||||
|
||||
log.Info("customer ", cs.customerId, " start load last index ")
|
||||
log.SInfo("customer ", cs.customerId, " start load last index ")
|
||||
lastIndex, ret := cs.subscriber.dataPersist.LoadCustomerIndex(cs.topic, cs.customerId)
|
||||
if ret == true {
|
||||
if lastIndex > 0 {
|
||||
@@ -116,18 +116,18 @@ func (cs *CustomerSubscriber) LoadLastIndex() {
|
||||
} else {
|
||||
//否则直接使用客户端发回来的
|
||||
}
|
||||
log.Info("customer ", cs.customerId, " load finish,start index is ", cs.StartIndex)
|
||||
log.SInfo("customer ", cs.customerId, " load finish,start index is ", cs.StartIndex)
|
||||
break
|
||||
}
|
||||
|
||||
log.Info("customer ", cs.customerId, " load last index is fail...")
|
||||
log.SInfo("customer ", cs.customerId, " load last index is fail...")
|
||||
time.Sleep(5 * time.Second)
|
||||
}
|
||||
}
|
||||
|
||||
func (cs *CustomerSubscriber) SubscribeRun() {
|
||||
defer cs.subscriber.queueWait.Done()
|
||||
log.Info("topic ", cs.topic, " start subscription")
|
||||
log.SInfo("topic ", cs.topic, " start subscription")
|
||||
|
||||
//加载之前的位置
|
||||
if cs.subscribeMethod == MethodLast {
|
||||
@@ -136,7 +136,7 @@ func (cs *CustomerSubscriber) SubscribeRun() {
|
||||
|
||||
for {
|
||||
if atomic.LoadInt32(&cs.isStop) != 0 {
|
||||
log.Info("topic ", cs.topic, " out of subscription")
|
||||
log.SInfo("topic ", cs.topic, " out of subscription")
|
||||
break
|
||||
}
|
||||
|
||||
@@ -146,14 +146,14 @@ func (cs *CustomerSubscriber) SubscribeRun() {
|
||||
|
||||
//todo 检测退出
|
||||
if cs.subscribe() == false {
|
||||
log.Info("topic ", cs.topic, " out of subscription")
|
||||
log.SInfo("topic ", cs.topic, " out of subscription")
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
//删除订阅关系
|
||||
cs.subscriber.removeCustomer(cs.customerId, cs)
|
||||
log.Info("topic ", cs.topic, " unsubscription")
|
||||
log.SInfo("topic ", cs.topic, " unsubscription")
|
||||
}
|
||||
|
||||
func (cs *CustomerSubscriber) subscribe() bool {
|
||||
|
||||
@@ -63,7 +63,7 @@ func (ms *MessageQueueService) ReadCfg() error {
|
||||
maxProcessTopicBacklogNum, ok := mapDBServiceCfg["MaxProcessTopicBacklogNum"]
|
||||
if ok == false {
|
||||
ms.maxProcessTopicBacklogNum = DefaultMaxTopicBacklogNum
|
||||
log.Info("MaxProcessTopicBacklogNum config is set to the default value of ", maxProcessTopicBacklogNum)
|
||||
log.SInfo("MaxProcessTopicBacklogNum config is set to the default value of ", maxProcessTopicBacklogNum)
|
||||
} else {
|
||||
ms.maxProcessTopicBacklogNum = int32(maxProcessTopicBacklogNum.(float64))
|
||||
}
|
||||
@@ -71,7 +71,7 @@ func (ms *MessageQueueService) ReadCfg() error {
|
||||
memoryQueueLen, ok := mapDBServiceCfg["MemoryQueueLen"]
|
||||
if ok == false {
|
||||
ms.memoryQueueLen = DefaultMemoryQueueLen
|
||||
log.Info("MemoryQueueLen config is set to the default value of ", DefaultMemoryQueueLen)
|
||||
log.SInfo("MemoryQueueLen config is set to the default value of ", DefaultMemoryQueueLen)
|
||||
} else {
|
||||
ms.memoryQueueLen = int32(memoryQueueLen.(float64))
|
||||
}
|
||||
|
||||
@@ -237,7 +237,7 @@ func (mp *MongoPersist) findTopicData(topic string, startIndex uint64, limit int
|
||||
defer cancelAll()
|
||||
err = cursor.All(ctxAll, &res)
|
||||
if err != nil {
|
||||
log.Error("find collect name ", topic, " is error", log.ErrorAttr("err", err))
|
||||
log.Error("find collect name error",log.String("topic",topic) ,log.ErrorField("err",err))
|
||||
return nil, false
|
||||
}
|
||||
|
||||
@@ -246,7 +246,7 @@ func (mp *MongoPersist) findTopicData(topic string, startIndex uint64, limit int
|
||||
rawData, errM := bson.Marshal(res[i])
|
||||
if errM != nil {
|
||||
if errM != nil {
|
||||
log.Error("collect name ", topic, " Marshal is error", log.ErrorAttr("err", err))
|
||||
log.Error("Marshal error",log.String("topic",topic) , log.ErrorField("err", err))
|
||||
return nil, false
|
||||
}
|
||||
continue
|
||||
@@ -391,7 +391,7 @@ func (mp *MongoPersist) GetIndex(topicData *TopicData) uint64 {
|
||||
if e.Key == "_id" {
|
||||
errC, seq := convertToNumber[uint64](e.Value)
|
||||
if errC != nil {
|
||||
log.Error("value is error:%s,%+v, ", errC.Error(), e.Value)
|
||||
log.Error("value is error", log.ErrorField("err",errC), log.Any("val",e.Value))
|
||||
}
|
||||
|
||||
return seq
|
||||
|
||||
@@ -56,9 +56,9 @@ func (ss *Subscriber) TopicSubscribe(rpcHandler rpc.IRpcHandler, subScribeType r
|
||||
}
|
||||
|
||||
if ok == true {
|
||||
log.Info("repeat subscription for customer ", customerId)
|
||||
log.SInfo("repeat subscription for customer ", customerId)
|
||||
} else {
|
||||
log.Info("subscription for customer ", customerId)
|
||||
log.SInfo("subscription for customer ", customerId)
|
||||
}
|
||||
|
||||
}
|
||||
@@ -72,7 +72,7 @@ func (ss *Subscriber) UnSubscribe(customerId string) {
|
||||
|
||||
customerSubscriber, ok := ss.mapCustomer[customerId]
|
||||
if ok == false {
|
||||
log.SWarning("failed to unsubscribe customer " + customerId)
|
||||
log.SWarn("failed to unsubscribe customer ", customerId)
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
@@ -93,7 +93,7 @@ func (tr *TopicRoom) Stop() {
|
||||
func (tr *TopicRoom) topicRoomRun() {
|
||||
defer tr.queueWait.Done()
|
||||
|
||||
log.Info("topic room ", tr.topic, " is running..")
|
||||
log.SInfo("topic room ", tr.topic, " is running..")
|
||||
for {
|
||||
if atomic.LoadInt32(&tr.isStop) != 0 {
|
||||
break
|
||||
@@ -145,5 +145,5 @@ func (tr *TopicRoom) topicRoomRun() {
|
||||
}
|
||||
tr.customerLocker.Unlock()
|
||||
|
||||
log.Info("topic room ", tr.topic, " is stop")
|
||||
log.SInfo("topic room ", tr.topic, " is stop")
|
||||
}
|
||||
|
||||
@@ -142,13 +142,13 @@ func (mp *MongoPersist) OnSetupRank(manual bool, rankSkip *RankSkip) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
log.Info("start load rank ", rankSkip.GetRankName(), " from mongodb.")
|
||||
log.SInfo("start load rank ", rankSkip.GetRankName(), " from mongodb.")
|
||||
err := mp.loadFromDB(rankSkip.GetRankID(), rankSkip.GetRankName())
|
||||
if err != nil {
|
||||
log.SError("load from db is fail :%s", err.Error())
|
||||
return err
|
||||
}
|
||||
log.Info("finish load rank ", rankSkip.GetRankName(), " from mongodb.")
|
||||
log.SInfo("finish load rank ", rankSkip.GetRankName(), " from mongodb.")
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -296,7 +296,7 @@ func (mp *MongoPersist) saveToDB() {
|
||||
buf := make([]byte, 4096)
|
||||
l := runtime.Stack(buf, false)
|
||||
errString := fmt.Sprint(r)
|
||||
log.Dump(string(buf[:l]), log.String("error", errString))
|
||||
log.StackError(string(buf[:l]), log.String("error", errString))
|
||||
}
|
||||
}()
|
||||
|
||||
|
||||
@@ -9,12 +9,12 @@ import (
|
||||
"github.com/duanhf2012/origin/v2/service"
|
||||
"github.com/duanhf2012/origin/v2/util/bytespool"
|
||||
"github.com/google/uuid"
|
||||
"runtime"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Deprecated: replace it with the TcpModule
|
||||
type TcpService struct {
|
||||
tcpServer network.TCPServer
|
||||
service.Service
|
||||
@@ -140,10 +140,7 @@ func (slf *Client) GetId() string {
|
||||
func (slf *Client) Run() {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
buf := make([]byte, 4096)
|
||||
l := runtime.Stack(buf, false)
|
||||
errString := fmt.Sprint(r)
|
||||
log.Dump(string(buf[:l]), log.String("error", errString))
|
||||
log.StackError(fmt.Sprint(r))
|
||||
}
|
||||
}()
|
||||
|
||||
@@ -156,7 +153,7 @@ func (slf *Client) Run() {
|
||||
slf.tcpConn.SetReadDeadline(slf.tcpService.tcpServer.ReadDeadline)
|
||||
bytes, err := slf.tcpConn.ReadMsg()
|
||||
if err != nil {
|
||||
log.Debug("read client failed", log.ErrorAttr("error", err), log.String("clientId", slf.id))
|
||||
log.Debug("read client failed", log.ErrorField("error", err), log.String("clientId", slf.id))
|
||||
break
|
||||
}
|
||||
data, err := slf.tcpService.process.Unmarshal(slf.id, bytes)
|
||||
|
||||
@@ -12,6 +12,7 @@ import (
|
||||
"sync"
|
||||
)
|
||||
|
||||
// Deprecated: replace it with the WSModule
|
||||
type WSService struct {
|
||||
service.Service
|
||||
wsServer network.WSServer
|
||||
@@ -129,7 +130,7 @@ func (slf *WSClient) Run() {
|
||||
for {
|
||||
bytes, err := slf.wsConn.ReadMsg()
|
||||
if err != nil {
|
||||
log.Debug("read client id %s is error:%+v", slf.id, err)
|
||||
log.Debugf("read client id %s is error:%+v", slf.id, err)
|
||||
break
|
||||
}
|
||||
data, err := slf.wsService.process.Unmarshal(slf.id, bytes)
|
||||
|
||||
223
util/blueprint/blueprint.go
Normal file
223
util/blueprint/blueprint.go
Normal file
@@ -0,0 +1,223 @@
|
||||
package blueprint
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sync/atomic"
|
||||
"github.com/duanhf2012/origin/v2/log"
|
||||
)
|
||||
|
||||
type Blueprint struct {
|
||||
execNodes []IExecNode // 注册的定义执行结点
|
||||
execNodeList []func() IExecNode
|
||||
execPool *ExecPool
|
||||
graphPool *GraphPool
|
||||
|
||||
blueprintModule IBlueprintModule
|
||||
mapGraph map[int64]IGraph
|
||||
seedID int64
|
||||
cancelTimer func(*uint64) bool
|
||||
|
||||
execDefFilePath string // 执行结点定义文件路径
|
||||
graphFilePath string // 蓝图文件路径
|
||||
}
|
||||
|
||||
func (bm *Blueprint) RegisterExecNode(execNodeFunc func() IExecNode) {
|
||||
bm.execNodeList = append(bm.execNodeList, execNodeFunc)
|
||||
}
|
||||
|
||||
type IExecNodeType[T any] interface {
|
||||
*T
|
||||
IExecNode
|
||||
}
|
||||
|
||||
// 生成一个泛型函数,返回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
|
||||
}
|
||||
|
||||
// 注册系统执行结点
|
||||
bm.regSysNodes()
|
||||
|
||||
// 将注册的实际执行结点与innerExecNode进行关联
|
||||
for _, newExec := range bm.execNodeList {
|
||||
e := newExec()
|
||||
if !execPool.Register(e) {
|
||||
return fmt.Errorf("register exec failed,exec:%s", e.GetName())
|
||||
}
|
||||
}
|
||||
|
||||
// 加载所有的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.execDefFilePath = execDefFilePath
|
||||
bm.graphFilePath = graphFilePath
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (bm *Blueprint) Create(graphName string) int64 {
|
||||
if graphName == "" {
|
||||
return 0
|
||||
}
|
||||
|
||||
graphID := atomic.AddInt64(&bm.seedID, 1)
|
||||
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 {
|
||||
graph := bm.mapGraph[graphID]
|
||||
if graph == nil {
|
||||
return fmt.Errorf("can not find graph:%d", graphID)
|
||||
}
|
||||
|
||||
_, err := graph.Do(eventID, args...)
|
||||
return err
|
||||
}
|
||||
|
||||
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 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 {
|
||||
return
|
||||
}
|
||||
|
||||
graph.Release()
|
||||
}
|
||||
|
||||
func (bm *Blueprint) CancelTimerId(graphID int64, timerId *uint64) bool {
|
||||
tId := *timerId
|
||||
bm.cancelTimer(timerId)
|
||||
|
||||
graph := bm.mapGraph[graphID]
|
||||
if graph == nil {
|
||||
return false
|
||||
}
|
||||
|
||||
gr, ok := graph.(*Graph)
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
|
||||
delete(gr.mapTimerID, tId)
|
||||
return true
|
||||
}
|
||||
34
util/blueprint/blueprint_test.go
Normal file
34
util/blueprint/blueprint_test.go
Normal file
@@ -0,0 +1,34 @@
|
||||
package blueprint
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
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)
|
||||
//if err != nil {
|
||||
// t.Fatalf("Do EntranceID_IntParam failed,err:%v", err)
|
||||
//}
|
||||
|
||||
//graphTest2 := bp.Create("test2")
|
||||
//
|
||||
//err = graphTest2.Do(EntranceID_IntParam, 1, 2, 3)
|
||||
//if err != nil {
|
||||
// t.Fatalf("Do EntranceID_IntParam failed,err:%v", err)
|
||||
//}
|
||||
|
||||
//graph := bp.Create("test1")
|
||||
//err = graph.Do(EntranceID_IntParam, 1, 2, 3)
|
||||
//if err != nil {
|
||||
// t.Fatalf("do failed,err:%v", err)
|
||||
//}
|
||||
|
||||
//graph.Release()
|
||||
}
|
||||
10
util/blueprint/context.go
Normal file
10
util/blueprint/context.go
Normal file
@@ -0,0 +1,10 @@
|
||||
package blueprint
|
||||
|
||||
type ExecContext struct {
|
||||
InputPorts []IPort
|
||||
OutputPorts []IPort
|
||||
}
|
||||
|
||||
func (ec *ExecContext) Reset() {
|
||||
*ec = ExecContext{}
|
||||
}
|
||||
581
util/blueprint/exec.go
Normal file
581
util/blueprint/exec.go
Normal file
@@ -0,0 +1,581 @@
|
||||
package blueprint
|
||||
|
||||
import "fmt"
|
||||
|
||||
// IInnerExecNode 配置生成的结点
|
||||
type IInnerExecNode interface {
|
||||
GetName() string
|
||||
SetExec(exec IExecNode)
|
||||
IsInPortExec(index int) bool
|
||||
IsOutPortExec(index int) bool
|
||||
GetInPortCount() int
|
||||
GetOutPortCount() int
|
||||
CloneInOutPort() ([]IPort, []IPort)
|
||||
|
||||
GetInPort(index int) IPort
|
||||
GetOutPort(index int) IPort
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
// 配置对应的基础信息+端口数据
|
||||
type innerExecNode struct {
|
||||
Name string
|
||||
Title string
|
||||
Package string
|
||||
Description string
|
||||
|
||||
inPort []IPort // 下标即为portId
|
||||
outPort []IPort // 下标即为portId
|
||||
|
||||
outPortParamStartIndex int // 输出参数的起始索引,用于排除执行出口
|
||||
|
||||
IExecNode // 实际注册的执行结点
|
||||
}
|
||||
|
||||
type BaseExecNode struct {
|
||||
*innerExecNode // 内部注册的执行结点
|
||||
|
||||
// 执行时初始化的数据
|
||||
*ExecContext
|
||||
gr *Graph
|
||||
execNode *execNode
|
||||
}
|
||||
|
||||
type InputConfig struct {
|
||||
Name string `json:"name"`
|
||||
PortType string `json:"type"`
|
||||
DataType string `json:"data_type"`
|
||||
HasInput bool `json:"has_input"`
|
||||
PinWidget string `json:"pin_widget"`
|
||||
PortId int `json:"port_id"`
|
||||
}
|
||||
|
||||
type OutputConfig struct {
|
||||
Name string `json:"name"`
|
||||
PortType string `json:"type"`
|
||||
DataType string `json:"data_type"`
|
||||
HasInput bool `json:"has_input"`
|
||||
PortId int `json:"port_id"`
|
||||
}
|
||||
|
||||
type BaseExecConfig struct {
|
||||
Name string `json:"name"`
|
||||
Title string `json:"title"`
|
||||
Package string `json:"package"`
|
||||
Description string `json:"description"`
|
||||
IsPure bool `json:"is_pure"`
|
||||
Inputs []InputConfig `json:"inputs"`
|
||||
Outputs []OutputConfig `json:"outputs"`
|
||||
}
|
||||
|
||||
func (bc *BaseExecConfig) GetMaxInPortId() int {
|
||||
maxPortId := -1
|
||||
for i := range bc.Inputs {
|
||||
if bc.Inputs[i].PortId > maxPortId {
|
||||
maxPortId = bc.Inputs[i].PortId
|
||||
}
|
||||
}
|
||||
|
||||
return maxPortId
|
||||
}
|
||||
|
||||
func (bc *BaseExecConfig) GetMaxOutPortId() int {
|
||||
maxPortId := -1
|
||||
for i := range bc.Outputs {
|
||||
if bc.Outputs[i].PortId > maxPortId {
|
||||
maxPortId = bc.Outputs[i].PortId
|
||||
}
|
||||
}
|
||||
|
||||
return maxPortId
|
||||
}
|
||||
|
||||
func (em *innerExecNode) PrepareMaxInPortId(maxInPortId int) {
|
||||
em.inPort = make([]IPort, maxInPortId+1)
|
||||
}
|
||||
|
||||
func (em *innerExecNode) PrepareMaxOutPortId(maxOutPortId int) {
|
||||
em.outPort = make([]IPort, maxOutPortId+1)
|
||||
}
|
||||
|
||||
func (em *innerExecNode) SetInPortById(id int, port IPort) bool {
|
||||
if id < 0 || id >= len(em.inPort) {
|
||||
return false
|
||||
}
|
||||
em.inPort[id] = port
|
||||
return true
|
||||
}
|
||||
|
||||
func (em *innerExecNode) SetOutPortById(id int, port IPort) bool {
|
||||
if id < 0 || id >= len(em.outPort) {
|
||||
return false
|
||||
}
|
||||
em.outPort[id] = port
|
||||
|
||||
// 分析执行的
|
||||
em.outPortParamStartIndex = -1
|
||||
for i := range em.outPort {
|
||||
if em.outPort[i] == nil {
|
||||
continue
|
||||
}
|
||||
|
||||
// 遇到非Exec结点,即为输出参数开始位置
|
||||
if !em.outPort[i].IsPortExec() {
|
||||
em.outPortParamStartIndex = i
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
func (em *innerExecNode) GetOutPortParamStartIndex() int {
|
||||
return em.outPortParamStartIndex
|
||||
}
|
||||
|
||||
func (em *innerExecNode) GetName() string {
|
||||
return em.Name
|
||||
}
|
||||
|
||||
func (em *innerExecNode) SetExec(exec IExecNode) {
|
||||
em.IExecNode = exec
|
||||
}
|
||||
|
||||
func (em *innerExecNode) CloneInOutPort() ([]IPort, []IPort) {
|
||||
inPorts := make([]IPort, 0, 2)
|
||||
for _, port := range em.inPort {
|
||||
if port == nil {
|
||||
inPorts = append(inPorts, nil)
|
||||
}
|
||||
|
||||
if port.IsPortExec() {
|
||||
// 执行入口, 不需要克隆,占位处理
|
||||
inPorts = append(inPorts, nil)
|
||||
continue
|
||||
}
|
||||
|
||||
inPorts = append(inPorts, port.Clone())
|
||||
}
|
||||
outPorts := make([]IPort, 0, 2)
|
||||
|
||||
for _, port := range em.outPort {
|
||||
if port == nil {
|
||||
outPorts = append(outPorts, nil)
|
||||
}
|
||||
|
||||
if port.IsPortExec() {
|
||||
outPorts = append(outPorts, nil)
|
||||
continue
|
||||
}
|
||||
outPorts = append(outPorts, port.Clone())
|
||||
}
|
||||
|
||||
return inPorts, outPorts
|
||||
}
|
||||
|
||||
func (em *innerExecNode) IsInPortExec(index int) bool {
|
||||
if index >= len(em.inPort) || index < 0 {
|
||||
return false
|
||||
}
|
||||
|
||||
return em.inPort[index].IsPortExec()
|
||||
}
|
||||
|
||||
func (em *innerExecNode) IsOutPortExec(index int) bool {
|
||||
if index >= len(em.outPort) || index < 0 {
|
||||
return false
|
||||
}
|
||||
|
||||
return em.outPort[index].IsPortExec()
|
||||
}
|
||||
|
||||
func (em *innerExecNode) GetInPortCount() int {
|
||||
return len(em.inPort)
|
||||
}
|
||||
|
||||
func (em *innerExecNode) GetOutPortCount() int {
|
||||
return len(em.outPort)
|
||||
}
|
||||
|
||||
func (em *innerExecNode) GetInPort(index int) IPort {
|
||||
if index >= len(em.inPort) || index < 0 {
|
||||
return nil
|
||||
}
|
||||
return em.inPort[index]
|
||||
}
|
||||
|
||||
func (em *innerExecNode) GetOutPort(index int) IPort {
|
||||
if index >= len(em.outPort) || index < 0 {
|
||||
return nil
|
||||
}
|
||||
return em.outPort[index]
|
||||
}
|
||||
|
||||
func (en *BaseExecNode) GetVariableName() string {
|
||||
return en.execNode.variableName
|
||||
}
|
||||
|
||||
func (en *BaseExecNode) GetBluePrintModule() IBlueprintModule {
|
||||
return en.gr.IBlueprintModule
|
||||
}
|
||||
|
||||
func (en *BaseExecNode) initInnerExecNode(innerNode *innerExecNode) {
|
||||
en.innerExecNode = innerNode
|
||||
}
|
||||
|
||||
func (en *BaseExecNode) getExecNodeInfo() (*ExecContext, *execNode) {
|
||||
return en.ExecContext, en.execNode
|
||||
}
|
||||
|
||||
func (en *BaseExecNode) setExecNodeInfo(c *ExecContext, e *execNode) {
|
||||
en.ExecContext = c
|
||||
en.execNode = e
|
||||
}
|
||||
|
||||
func (en *BaseExecNode) initExecNode(gr *Graph, node *execNode) error {
|
||||
ctx, ok := gr.context[node.Id]
|
||||
if !ok {
|
||||
return fmt.Errorf("node %s not found", node.Id)
|
||||
}
|
||||
|
||||
en.ExecContext = ctx
|
||||
en.gr = gr
|
||||
en.execNode = node
|
||||
return nil
|
||||
}
|
||||
|
||||
func (en *BaseExecNode) GetPorts() ([]IPort, []IPort) {
|
||||
return en.InputPorts, en.OutputPorts
|
||||
}
|
||||
|
||||
func (en *BaseExecNode) GetInPort(index int) IPort {
|
||||
if en.InputPorts == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
if index >= len(en.InputPorts) || index < 0 {
|
||||
return nil
|
||||
}
|
||||
return en.InputPorts[index]
|
||||
}
|
||||
|
||||
func (en *BaseExecNode) GetOutPort(index int) IPort {
|
||||
if en.OutputPorts == nil {
|
||||
return nil
|
||||
}
|
||||
if index >= len(en.OutputPorts) || index < 0 {
|
||||
return nil
|
||||
}
|
||||
return en.OutputPorts[index]
|
||||
}
|
||||
|
||||
func (en *BaseExecNode) SetOutPort(index int, val IPort) bool {
|
||||
if index >= len(en.OutputPorts) || index < 0 {
|
||||
return false
|
||||
}
|
||||
en.OutputPorts[index].SetValue(val)
|
||||
return true
|
||||
}
|
||||
|
||||
func (en *BaseExecNode) GetInPortInt(index int) (Port_Int, bool) {
|
||||
port := en.GetInPort(index)
|
||||
if port == nil {
|
||||
return 0, false
|
||||
}
|
||||
|
||||
return port.GetInt()
|
||||
}
|
||||
|
||||
func (en *BaseExecNode) GetInPortFloat(index int) (Port_Float, bool) {
|
||||
port := en.GetInPort(index)
|
||||
if port == nil {
|
||||
return 0, false
|
||||
}
|
||||
return port.GetFloat()
|
||||
}
|
||||
|
||||
func (en *BaseExecNode) GetInPortStr(index int) (Port_Str, bool) {
|
||||
port := en.GetInPort(index)
|
||||
if port == nil {
|
||||
return "", false
|
||||
}
|
||||
return port.GetStr()
|
||||
}
|
||||
|
||||
func (en *BaseExecNode) GetInPortArray(index int) (Port_Array, bool) {
|
||||
port := en.GetInPort(index)
|
||||
if port == nil {
|
||||
return nil, false
|
||||
}
|
||||
return port.GetArray()
|
||||
}
|
||||
|
||||
func (en *BaseExecNode) GetInPortArrayValInt(index int, idx int) (Port_Int, bool) {
|
||||
port := en.GetInPort(index)
|
||||
if port == nil {
|
||||
return 0, false
|
||||
}
|
||||
return port.GetArrayValInt(idx)
|
||||
}
|
||||
|
||||
func (en *BaseExecNode) GetInPortArrayValStr(idx int) (Port_Str, bool) {
|
||||
port := en.GetInPort(idx)
|
||||
if port == nil {
|
||||
return "", false
|
||||
}
|
||||
return port.GetArrayValStr(idx)
|
||||
}
|
||||
|
||||
func (en *BaseExecNode) GetInPortBool(index int) (Port_Bool, bool) {
|
||||
port := en.GetInPort(index)
|
||||
if port == nil {
|
||||
return false, false
|
||||
}
|
||||
return port.GetBool()
|
||||
}
|
||||
|
||||
func (en *BaseExecNode) GetOutPortInt(index int) (Port_Int, bool) {
|
||||
port := en.GetOutPort(index)
|
||||
if port == nil {
|
||||
return 0, false
|
||||
}
|
||||
return port.GetInt()
|
||||
}
|
||||
|
||||
func (en *BaseExecNode) GetOutPortFloat(index int) (Port_Float, bool) {
|
||||
port := en.GetOutPort(index)
|
||||
if port == nil {
|
||||
return 0, false
|
||||
}
|
||||
return port.GetFloat()
|
||||
}
|
||||
|
||||
func (en *BaseExecNode) GetOutPortStr(index int) (Port_Str, bool) {
|
||||
port := en.GetOutPort(index)
|
||||
if port == nil {
|
||||
return "", false
|
||||
}
|
||||
return port.GetStr()
|
||||
}
|
||||
|
||||
func (en *BaseExecNode) GetOutPortArrayValInt(index int, idx int) (Port_Int, bool) {
|
||||
port := en.GetOutPort(index)
|
||||
if port == nil {
|
||||
return 0, false
|
||||
}
|
||||
return port.GetArrayValInt(idx)
|
||||
}
|
||||
|
||||
func (en *BaseExecNode) GetOutPortArrayValStr(index int, idx int) (Port_Str, bool) {
|
||||
port := en.GetOutPort(index)
|
||||
if port == nil {
|
||||
return "", false
|
||||
}
|
||||
return port.GetArrayValStr(idx)
|
||||
}
|
||||
|
||||
func (en *BaseExecNode) GetOutPortBool(index int) (Port_Bool, bool) {
|
||||
port := en.GetInPort(index)
|
||||
if port == nil {
|
||||
return false, false
|
||||
}
|
||||
return port.GetBool()
|
||||
}
|
||||
|
||||
func (en *BaseExecNode) SetInPortInt(index int, val Port_Int) bool {
|
||||
port := en.GetInPort(index)
|
||||
if port == nil {
|
||||
return false
|
||||
}
|
||||
return port.SetInt(val)
|
||||
}
|
||||
|
||||
func (en *BaseExecNode) SetInPortFloat(index int, val Port_Float) bool {
|
||||
port := en.GetInPort(index)
|
||||
if port == nil {
|
||||
return false
|
||||
}
|
||||
return port.SetFloat(val)
|
||||
}
|
||||
|
||||
func (en *BaseExecNode) SetInPortStr(index int, val Port_Str) bool {
|
||||
port := en.GetInPort(index)
|
||||
if port == nil {
|
||||
return false
|
||||
}
|
||||
return port.SetStr(val)
|
||||
}
|
||||
|
||||
func (en *BaseExecNode) SetInBool(index int, val Port_Bool) bool {
|
||||
port := en.GetInPort(index)
|
||||
if port == nil {
|
||||
return false
|
||||
}
|
||||
return port.SetBool(val)
|
||||
}
|
||||
|
||||
func (en *BaseExecNode) SetInPortArrayValInt(index int, idx int, val Port_Int) bool {
|
||||
port := en.GetInPort(index)
|
||||
if port == nil {
|
||||
return false
|
||||
}
|
||||
return port.SetArrayValInt(idx, val)
|
||||
}
|
||||
|
||||
func (en *BaseExecNode) SetInPortArrayValStr(index int, idx int, val Port_Str) bool {
|
||||
port := en.GetInPort(index)
|
||||
if port == nil {
|
||||
return false
|
||||
}
|
||||
return port.SetArrayValStr(idx, val)
|
||||
}
|
||||
|
||||
func (en *BaseExecNode) AppendInPortArrayValInt(index int, val Port_Int) bool {
|
||||
port := en.GetInPort(index)
|
||||
if port == nil {
|
||||
return false
|
||||
}
|
||||
return port.AppendArrayValInt(val)
|
||||
}
|
||||
|
||||
func (en *BaseExecNode) AppendInPortArrayValStr(index int, val Port_Str) bool {
|
||||
port := en.GetInPort(index)
|
||||
if port == nil {
|
||||
return false
|
||||
}
|
||||
return port.AppendArrayValStr(val)
|
||||
}
|
||||
|
||||
func (en *BaseExecNode) GetInPortArrayLen(index int) Port_Int {
|
||||
port := en.GetInPort(index)
|
||||
if port == nil {
|
||||
return 0
|
||||
}
|
||||
return port.GetArrayLen()
|
||||
}
|
||||
|
||||
func (en *BaseExecNode) SetOutPortInt(index int, val Port_Int) bool {
|
||||
port := en.GetOutPort(index)
|
||||
if port == nil {
|
||||
return false
|
||||
}
|
||||
return port.SetInt(val)
|
||||
}
|
||||
|
||||
func (en *BaseExecNode) SetOutPortFloat(index int, val Port_Float) bool {
|
||||
port := en.GetOutPort(index)
|
||||
if port == nil {
|
||||
return false
|
||||
}
|
||||
return port.SetFloat(val)
|
||||
}
|
||||
|
||||
func (en *BaseExecNode) SetOutPortStr(index int, val Port_Str) bool {
|
||||
port := en.GetOutPort(index)
|
||||
if port == nil {
|
||||
return false
|
||||
}
|
||||
return port.SetStr(val)
|
||||
}
|
||||
|
||||
func (en *BaseExecNode) SetOutPortBool(index int, val Port_Bool) bool {
|
||||
port := en.GetOutPort(index)
|
||||
if port == nil {
|
||||
return false
|
||||
}
|
||||
return port.SetBool(val)
|
||||
}
|
||||
|
||||
func (en *BaseExecNode) SetOutPortArrayValInt(index int, idx int, val Port_Int) bool {
|
||||
port := en.GetOutPort(index)
|
||||
if port == nil {
|
||||
return false
|
||||
}
|
||||
return port.SetArrayValInt(idx, val)
|
||||
}
|
||||
|
||||
func (en *BaseExecNode) SetOutPortArrayValStr(index int, idx int, val Port_Str) bool {
|
||||
port := en.GetOutPort(index)
|
||||
if port == nil {
|
||||
return false
|
||||
}
|
||||
return port.SetArrayValStr(idx, val)
|
||||
}
|
||||
|
||||
func (en *BaseExecNode) AppendOutPortArrayValInt(index int, val Port_Int) bool {
|
||||
port := en.GetOutPort(index)
|
||||
if port == nil {
|
||||
return false
|
||||
}
|
||||
return port.AppendArrayValInt(val)
|
||||
}
|
||||
|
||||
func (en *BaseExecNode) AppendOutPortArrayValStr(index int, val Port_Str) bool {
|
||||
port := en.GetOutPort(index)
|
||||
if port == nil {
|
||||
return false
|
||||
}
|
||||
return port.AppendArrayValStr(val)
|
||||
}
|
||||
|
||||
func (en *BaseExecNode) GetOutPortArrayLen(index int) Port_Int {
|
||||
port := en.GetOutPort(index)
|
||||
if port == nil {
|
||||
return 0
|
||||
}
|
||||
return port.GetArrayLen()
|
||||
}
|
||||
|
||||
func (en *BaseExecNode) DoNext(index int) error {
|
||||
// -1 表示中断运行
|
||||
if index == -1 {
|
||||
return nil
|
||||
}
|
||||
|
||||
if index < 0 || index >= len(en.execNode.nextNode) {
|
||||
return fmt.Errorf("next index %d not found", index)
|
||||
}
|
||||
|
||||
if en.execNode.nextNode[index] == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
return en.execNode.nextNode[index].Do(en.gr)
|
||||
}
|
||||
|
||||
func (en *BaseExecNode) GetNextExecLen() int {
|
||||
return len(en.execNode.nextNode)
|
||||
}
|
||||
|
||||
func (en *BaseExecNode) getInnerExecNode() IInnerExecNode {
|
||||
return en.innerExecNode.IExecNode.(IInnerExecNode)
|
||||
}
|
||||
|
||||
func (en *BaseExecNode) GetBlueprintModule() IBlueprintModule {
|
||||
if en.gr == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
return en.gr.IBlueprintModule
|
||||
}
|
||||
383
util/blueprint/execpool.go
Normal file
383
util/blueprint/execpool.go
Normal file
@@ -0,0 +1,383 @@
|
||||
package blueprint
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Entrance 格式:Entrance_XXXX_ID
|
||||
const (
|
||||
Entrance = "Entrance_"
|
||||
)
|
||||
|
||||
type ExecPool struct {
|
||||
innerExecNodeMap map[string]IInnerExecNode // 所有配置对应的结点信息
|
||||
execNodeMap map[string]IExecNode // 实际注册的执行结点
|
||||
}
|
||||
|
||||
func (em *ExecPool) Load(execDefFilePath string) error {
|
||||
em.innerExecNodeMap = make(map[string]IInnerExecNode, 512)
|
||||
em.execNodeMap = make(map[string]IExecNode, 512)
|
||||
|
||||
// 检查路径是否存在
|
||||
stat, err := os.Stat(execDefFilePath)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to access path %s: %v", execDefFilePath, err)
|
||||
}
|
||||
|
||||
// 如果是单个文件,直接处理
|
||||
if !stat.IsDir() {
|
||||
return fmt.Errorf("%s is not a directory", execDefFilePath)
|
||||
}
|
||||
|
||||
// 遍历目录及其子目录
|
||||
err = filepath.Walk(execDefFilePath, func(path string, info os.FileInfo, err error) error {
|
||||
if err != nil {
|
||||
fmt.Printf("访问路径出错 %s: %v\n", path, err)
|
||||
return nil // 继续遍历其他文件
|
||||
}
|
||||
|
||||
// 如果是目录,继续遍历
|
||||
if info.IsDir() {
|
||||
return nil
|
||||
}
|
||||
|
||||
// 只处理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)
|
||||
}
|
||||
|
||||
// 生成变量配置对应的配置结点GetVar_类型、SetVar_类型
|
||||
return em.regVariablesNode()
|
||||
}
|
||||
|
||||
// 处理单个JSON文件
|
||||
func (em *ExecPool) processJSONFile(filePath string) error {
|
||||
// 打开文件
|
||||
file, err := os.Open(filePath)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to open file %s: %v", filePath, err)
|
||||
}
|
||||
|
||||
defer func(file *os.File) {
|
||||
err = file.Close()
|
||||
if err != nil {
|
||||
fmt.Printf("failed to close file %s: %v\n", filePath, err)
|
||||
return
|
||||
}
|
||||
}(file)
|
||||
|
||||
var baseExecConfig []BaseExecConfig
|
||||
decoder := json.NewDecoder(file)
|
||||
if err = decoder.Decode(&baseExecConfig); err != nil {
|
||||
return fmt.Errorf("failed to decode JSON from file %s: %v", filePath, err)
|
||||
}
|
||||
|
||||
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
|
||||
})
|
||||
|
||||
// 根据配置的结点信息,创建innerExecNode
|
||||
var execError error
|
||||
exec, execError := em.createExecFromJSON(baseExecConfig[i])
|
||||
if execError != nil {
|
||||
return execError
|
||||
}
|
||||
|
||||
// 加载到innerExecNodeMap中
|
||||
if !em.addInnerExec(exec) {
|
||||
return fmt.Errorf("exec %s already registered", exec.GetName())
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (em *ExecPool) createPortByDataType(nodeName, portName, dataType string) (IPort, error) {
|
||||
switch strings.ToLower(dataType) {
|
||||
case Config_DataType_Int, Config_DataType_Integer:
|
||||
return NewPortInt(), nil
|
||||
case Config_DataType_Float:
|
||||
return NewPortFloat(), nil
|
||||
case Config_DataType_Str:
|
||||
return NewPortStr(), nil
|
||||
case Config_DataType_Boolean, Config_DataType_Bool:
|
||||
return NewPortBool(), nil
|
||||
case Config_DataType_Array:
|
||||
return NewPortArray(), nil
|
||||
}
|
||||
|
||||
return nil, fmt.Errorf("invalid data type %s,node %s port %s", dataType, nodeName, portName)
|
||||
}
|
||||
|
||||
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
|
||||
} else {
|
||||
baseExec.Name = baseExecConfig.Name
|
||||
}
|
||||
baseExec.Title = baseExecConfig.Title
|
||||
baseExec.Package = baseExecConfig.Package
|
||||
baseExec.Description = baseExecConfig.Description
|
||||
baseExec.PrepareMaxInPortId(baseExecConfig.GetMaxInPortId())
|
||||
baseExec.PrepareMaxOutPortId(baseExecConfig.GetMaxOutPortId())
|
||||
|
||||
// 初始化所有的输入端口
|
||||
inExecNum := 0
|
||||
for index, input := range baseExecConfig.Inputs {
|
||||
portType := strings.ToLower(input.PortType)
|
||||
if portType != Config_PortType_Exec && portType != Config_PortType_Data {
|
||||
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)
|
||||
}
|
||||
if index > 0 {
|
||||
return nil, fmt.Errorf("the exec port is only allowed to be placed on the first one,node name %s", baseExec.Name)
|
||||
}
|
||||
|
||||
inExecNum++
|
||||
// 设置执行端口
|
||||
baseExec.SetInPortById(input.PortId, 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)
|
||||
if portType != Config_PortType_Exec && portType != Config_PortType_Data {
|
||||
return nil, fmt.Errorf("output %s data type %s not support,node name %s", output.Name, output.DataType, baseExec.Name)
|
||||
}
|
||||
|
||||
// Exec出口只能先Exec,再Data,不能穿插,如果是Data类型,但遇到Exec入口则不允许
|
||||
if hasData && portType == Config_PortType_Exec {
|
||||
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 {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
baseExec.SetOutPortById(output.PortId, port)
|
||||
}
|
||||
return &baseExec, nil
|
||||
}
|
||||
|
||||
func (em *ExecPool) addInnerExec(exec IInnerExecNode) bool {
|
||||
if _, ok := em.innerExecNodeMap[exec.GetName()]; ok {
|
||||
return false
|
||||
}
|
||||
em.innerExecNodeMap[exec.GetName()] = exec
|
||||
return true
|
||||
}
|
||||
|
||||
func (em *ExecPool) Register(exec IExecNode) bool {
|
||||
baseExec, ok := exec.(IExecNode)
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
|
||||
innerNode, ok := em.innerExecNodeMap[baseExec.GetName()]
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
|
||||
if _, ok = em.execNodeMap[innerNode.GetName()]; ok {
|
||||
return false
|
||||
}
|
||||
|
||||
baseExecNode, ok := exec.(IBaseExecNode)
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
|
||||
// 设置实际执行结点中innerExecNode变量,BaseExecNode.innerExecNode = innerNode
|
||||
baseExecNode.initInnerExecNode(innerNode.(*innerExecNode))
|
||||
|
||||
// innerNode设置实际的exec变量,innerExecNode.IExecNode = exec
|
||||
innerNode.SetExec(exec)
|
||||
|
||||
// 将实际的执行结点保存到execNodeMap中
|
||||
em.execNodeMap[baseExec.GetName()] = exec
|
||||
return true
|
||||
}
|
||||
|
||||
func (em *ExecPool) GetExec(name string) IInnerExecNode {
|
||||
if exec, ok := em.execNodeMap[name]; ok {
|
||||
return exec.getInnerExecNode()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// regVariablesNode 注册变量结点GetVar_类型、SetVar_类型
|
||||
func (em *ExecPool) regVariablesNode() error {
|
||||
var err error
|
||||
if err = em.regGetVariables(Config_DataType_Int); err != nil {
|
||||
return err
|
||||
}
|
||||
if err = em.regGetVariables(Config_DataType_Integer); err != nil {
|
||||
return err
|
||||
}
|
||||
if err = em.regGetVariables(Config_DataType_Float); err != nil {
|
||||
return err
|
||||
}
|
||||
if err = em.regGetVariables(Config_DataType_Str); err != nil {
|
||||
return err
|
||||
}
|
||||
if err = em.regGetVariables(Config_DataType_Boolean); err != nil {
|
||||
return err
|
||||
}
|
||||
if err = em.regGetVariables(Config_DataType_Bool); err != nil {
|
||||
return err
|
||||
}
|
||||
if err = em.regGetVariables(Config_DataType_Array); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err = em.regSetVariables(Config_DataType_Int); err != nil {
|
||||
return err
|
||||
}
|
||||
if err = em.regSetVariables(Config_DataType_Integer); err != nil {
|
||||
return err
|
||||
}
|
||||
if err = em.regSetVariables(Config_DataType_Float); err != nil {
|
||||
return err
|
||||
}
|
||||
if err = em.regSetVariables(Config_DataType_Str); err != nil {
|
||||
return err
|
||||
}
|
||||
if err = em.regSetVariables(Config_DataType_Boolean); err != nil {
|
||||
return err
|
||||
}
|
||||
if err = em.regSetVariables(Config_DataType_Bool); err != nil {
|
||||
return err
|
||||
}
|
||||
if err = em.regSetVariables(Config_DataType_Array); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (em *ExecPool) regGetVariables(typ string) error {
|
||||
var baseExec innerExecNode
|
||||
baseExec.Name = genGetVariablesNodeName(typ)
|
||||
baseExec.PrepareMaxOutPortId(0)
|
||||
|
||||
outPort := NewPortByType(typ)
|
||||
if outPort == nil {
|
||||
return fmt.Errorf("invalid type %s", typ)
|
||||
}
|
||||
baseExec.SetOutPortById(0, outPort)
|
||||
|
||||
var getVariablesNode GetVariablesNode
|
||||
getVariablesNode.nodeName = baseExec.GetName()
|
||||
|
||||
if !em.addInnerExec(&baseExec) {
|
||||
return fmt.Errorf("exec %s already registered", baseExec.GetName())
|
||||
}
|
||||
if !em.Register(&getVariablesNode) {
|
||||
return fmt.Errorf("exec %s already registered", baseExec.GetName())
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func genSetVariablesNodeName(typ string) string {
|
||||
return fmt.Sprintf("%s_%s", SetVariables, typ)
|
||||
}
|
||||
|
||||
func genGetVariablesNodeName(typ string) string {
|
||||
return fmt.Sprintf("%s_%s", GetVariables, typ)
|
||||
}
|
||||
|
||||
func (em *ExecPool) regSetVariables(typ string) error {
|
||||
var baseExec innerExecNode
|
||||
baseExec.Name = genSetVariablesNodeName(typ)
|
||||
|
||||
inExecPort := NewPortByType(Config_PortType_Exec)
|
||||
inPort := NewPortByType(typ)
|
||||
outExecPort := NewPortByType(Config_PortType_Exec)
|
||||
outPort := NewPortByType(typ)
|
||||
baseExec.PrepareMaxInPortId(1)
|
||||
baseExec.PrepareMaxOutPortId(1)
|
||||
|
||||
baseExec.SetInPortById(0, inExecPort)
|
||||
baseExec.SetInPortById(1, inPort)
|
||||
|
||||
baseExec.SetOutPortById(0, outExecPort)
|
||||
baseExec.SetOutPortById(1, outPort)
|
||||
|
||||
baseExec.IExecNode = &SetVariablesNode{nodeName: baseExec.GetName()}
|
||||
if !em.addInnerExec(&baseExec) {
|
||||
return fmt.Errorf("exec %s already registered", baseExec.GetName())
|
||||
}
|
||||
if !em.Register(baseExec.IExecNode) {
|
||||
return fmt.Errorf("exec %s already registered", baseExec.GetName())
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func getEntranceNodeNameAndID(className string) (string, int64, bool) {
|
||||
if !strings.HasPrefix(className, Entrance) {
|
||||
return "", 0, false
|
||||
}
|
||||
|
||||
parts := strings.Split(className, "_")
|
||||
if len(parts) != 3 {
|
||||
return "", 0, false
|
||||
}
|
||||
|
||||
entranceID, err := strconv.Atoi(parts[2])
|
||||
if err != nil {
|
||||
return "", 0, false
|
||||
}
|
||||
|
||||
return parts[0] + "_" + parts[1], int64(entranceID), true
|
||||
}
|
||||
231
util/blueprint/graph.go
Normal file
231
util/blueprint/graph.go
Normal file
@@ -0,0 +1,231 @@
|
||||
package blueprint
|
||||
|
||||
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 {
|
||||
SafeAfterFunc(timerId *uint64, d time.Duration, AdditionData interface{}, cb func(uint64, interface{}))
|
||||
TriggerEvent(graphID int64, eventID int64, args ...any) error
|
||||
CancelTimerId(graphID int64, timerId *uint64) bool
|
||||
GetGameService() service.IService
|
||||
GetBattleService() service.IService
|
||||
}
|
||||
|
||||
type baseGraph struct {
|
||||
entrance map[int64]*execNode // 入口
|
||||
}
|
||||
|
||||
type Graph struct {
|
||||
graphFileName string
|
||||
graphID int64
|
||||
*baseGraph
|
||||
graphContext
|
||||
IBlueprintModule
|
||||
mapTimerID map[uint64]struct{}
|
||||
}
|
||||
|
||||
type graphContext struct {
|
||||
context map[string]*ExecContext // 上下文
|
||||
variables map[string]IPort // 变量
|
||||
globalVariables map[string]IPort // 全局变量,g_Return,为执行返回值
|
||||
}
|
||||
|
||||
type nodeConfig struct {
|
||||
Id string `json:"id"`
|
||||
Class string `json:"class"`
|
||||
Module string `json:"module"`
|
||||
//Pos []float64 `json:"pos"`
|
||||
PortDefault map[string]interface{} `json:"port_defaultv"`
|
||||
}
|
||||
|
||||
type edgeConfig struct {
|
||||
EdgeID string `json:"edge_id"`
|
||||
SourceNodeID string `json:"source_node_id"`
|
||||
DesNodeId string `json:"des_node_id"`
|
||||
|
||||
SourcePortId int `json:"source_port_id"`
|
||||
DesPortId int `json:"des_port_id"`
|
||||
}
|
||||
|
||||
type MultiTypeValue struct {
|
||||
Value any
|
||||
}
|
||||
|
||||
// 实现json.Unmarshaler接口,自定义解码逻辑
|
||||
func (v *MultiTypeValue) UnmarshalJSON(data []byte) error {
|
||||
// 尝试将数据解析为字符串
|
||||
var strVal string
|
||||
if err := json.Unmarshal(data, &strVal); err == nil {
|
||||
v.Value = strVal
|
||||
return nil
|
||||
}
|
||||
|
||||
// 如果不是字符串,尝试解析为数字
|
||||
var intVal int
|
||||
if err := json.Unmarshal(data, &intVal); err == nil {
|
||||
v.Value = intVal
|
||||
return nil
|
||||
}
|
||||
|
||||
// 如果不是字符串,尝试解析为数字
|
||||
var boolVal bool
|
||||
if err := json.Unmarshal(data, &boolVal); err == nil {
|
||||
v.Value = boolVal
|
||||
return nil
|
||||
}
|
||||
|
||||
// 如果不是字符串,尝试解析为数字
|
||||
var float64Val float64
|
||||
if err := json.Unmarshal(data, &float64Val); err == nil {
|
||||
v.Value = float64Val
|
||||
return nil
|
||||
}
|
||||
|
||||
var arrayVal []any
|
||||
if err := json.Unmarshal(data, &arrayVal); err == nil {
|
||||
v.Value = arrayVal
|
||||
return nil
|
||||
}
|
||||
// 如果都失败,返回错误
|
||||
return fmt.Errorf("cannot unmarshal JSON value: %s", string(data))
|
||||
}
|
||||
|
||||
type variablesConfig struct {
|
||||
Name string `json:"name"`
|
||||
Type string `json:"type"`
|
||||
Value MultiTypeValue `json:"value"`
|
||||
}
|
||||
|
||||
type graphConfig struct {
|
||||
GraphName string `json:"graph_name"`
|
||||
Time string `json:"time"`
|
||||
|
||||
Nodes []nodeConfig `json:"nodes"`
|
||||
Edges []edgeConfig `json:"edges"`
|
||||
Variables []variablesConfig `json:"variables"`
|
||||
}
|
||||
|
||||
func (gc *graphConfig) GetVariablesByName(varName string) *variablesConfig {
|
||||
for _, varCfg := range gc.Variables {
|
||||
if varCfg.Name == varName {
|
||||
return &varCfg
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (gc *graphConfig) GetNodeByID(nodeID string) *nodeConfig {
|
||||
for _, node := range gc.Nodes {
|
||||
if node.Id == nodeID {
|
||||
return &node
|
||||
}
|
||||
}
|
||||
|
||||
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...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if gr.globalVariables != nil {
|
||||
port := gr.globalVariables[ReturnVarial]
|
||||
if port != nil {
|
||||
array, ok := port.GetArray()
|
||||
if ok {
|
||||
return array, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (gr *Graph) GetNodeInPortValue(nodeID string, inPortIndex int) IPort {
|
||||
if ctx, ok := gr.context[nodeID]; ok {
|
||||
if inPortIndex >= len(ctx.InputPorts) || inPortIndex < 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
return ctx.InputPorts[inPortIndex]
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (gr *Graph) GetNodeOutPortValue(nodeID string, outPortIndex int) IPort {
|
||||
if ctx, ok := gr.context[nodeID]; ok {
|
||||
if outPortIndex >= len(ctx.OutputPorts) || outPortIndex < 0 {
|
||||
return nil
|
||||
}
|
||||
return ctx.OutputPorts[outPortIndex]
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (gr *Graph) Release() {
|
||||
// 有定时器关闭定时器
|
||||
for timerID := range gr.mapTimerID {
|
||||
gr.CancelTimerId(gr.graphID, &timerID)
|
||||
}
|
||||
gr.mapTimerID = nil
|
||||
|
||||
// 清理掉所有数据
|
||||
*gr = Graph{}
|
||||
}
|
||||
|
||||
func (gr *Graph) HotReload(newBaseGraph *baseGraph) {
|
||||
gr.baseGraph = newBaseGraph
|
||||
}
|
||||
|
||||
func (gr *Graph) GetGraphFileName() string{
|
||||
return gr.graphFileName
|
||||
}
|
||||
315
util/blueprint/graphpool.go
Normal file
315
util/blueprint/graphpool.go
Normal file
@@ -0,0 +1,315 @@
|
||||
package blueprint
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/goccy/go-json"
|
||||
)
|
||||
|
||||
type GraphPool struct {
|
||||
mapGraphs map[string]*baseGraph
|
||||
execPool *ExecPool
|
||||
blueprintModule IBlueprintModule
|
||||
}
|
||||
|
||||
func (gp *GraphPool) Load(execPool *ExecPool, graphFilePath string, blueprintModule IBlueprintModule) error {
|
||||
gp.execPool = execPool
|
||||
gp.mapGraphs = make(map[string]*baseGraph, 1024)
|
||||
gp.blueprintModule = blueprintModule
|
||||
|
||||
// 检查路径是否存在
|
||||
stat, err := os.Stat(graphFilePath)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to access path %s: %v", graphFilePath, err)
|
||||
}
|
||||
|
||||
// 如果是单个文件,直接处理
|
||||
if !stat.IsDir() {
|
||||
return fmt.Errorf("%s is not a directory", graphFilePath)
|
||||
}
|
||||
|
||||
// 遍历目录及其子目录
|
||||
return filepath.Walk(graphFilePath, func(path string, info os.FileInfo, err error) error {
|
||||
if err != nil {
|
||||
fmt.Printf("访问路径出错 %s: %v\n", path, err)
|
||||
return nil // 继续遍历其他文件
|
||||
}
|
||||
|
||||
// 如果是目录,继续遍历
|
||||
if info.IsDir() {
|
||||
return nil
|
||||
}
|
||||
|
||||
// 只处理JSON文件
|
||||
if filepath.Ext(path) == ".vgf" {
|
||||
return gp.processJSONFile(path)
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
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 {
|
||||
return nil
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
func (gp *GraphPool) processJSONFile(filePath string) error {
|
||||
// 打开文件
|
||||
file, err := os.Open(filePath)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to open file %s: %v", filePath, err)
|
||||
}
|
||||
|
||||
defer func() {
|
||||
if err = file.Close(); err != nil {
|
||||
fmt.Printf("关闭文件 %s 时出错: %v\n", filePath, err)
|
||||
}
|
||||
}()
|
||||
|
||||
fileName := filepath.Base(filePath)
|
||||
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 {
|
||||
return fmt.Errorf("failed to decode JSON from file %s: %v", filePath, err)
|
||||
}
|
||||
|
||||
// 预处理蓝图
|
||||
return gp.prepareGraph(name, &gConfig)
|
||||
}
|
||||
|
||||
func (gp *GraphPool) prepareGraph(graphName string, graphConfig *graphConfig) error {
|
||||
// 找到所有的入口
|
||||
for _, node := range graphConfig.Nodes {
|
||||
_, entranceID, ok := getEntranceNodeNameAndID(node.Class)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
|
||||
// 对入口进行预处理
|
||||
err := gp.prepareOneEntrance(graphName, entranceID, &node, graphConfig)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (gp *GraphPool) genVarExec(nodeCfg *nodeConfig, graphConfig *graphConfig) (IInnerExecNode, string) {
|
||||
// 是否为Get_或Set_开头
|
||||
if !strings.HasPrefix(nodeCfg.Class, "Get_") && !strings.HasPrefix(nodeCfg.Class, "Set_") {
|
||||
return gp.execPool.GetExec(nodeCfg.Class), ""
|
||||
}
|
||||
|
||||
// 获取Get_或Set_结尾字符串
|
||||
var nodeName string
|
||||
var varName string
|
||||
if strings.HasPrefix(nodeCfg.Class, "Get_") {
|
||||
var typ string
|
||||
varName = strings.TrimPrefix(nodeCfg.Class, "Get_")
|
||||
varCfg := graphConfig.GetVariablesByName(varName)
|
||||
if varCfg != nil {
|
||||
typ = varCfg.Type
|
||||
}
|
||||
nodeName = genGetVariablesNodeName(typ)
|
||||
} else if strings.HasPrefix(nodeCfg.Class, "Set_") {
|
||||
var typ string
|
||||
varName = strings.TrimPrefix(nodeCfg.Class, "Set_")
|
||||
varCfg := graphConfig.GetVariablesByName(varName)
|
||||
if varCfg != nil {
|
||||
typ = varCfg.Type
|
||||
}
|
||||
nodeName = genSetVariablesNodeName(typ)
|
||||
}
|
||||
|
||||
e := gp.execPool.GetExec(nodeName)
|
||||
return e, varName
|
||||
}
|
||||
|
||||
func (gp *GraphPool) genAllNode(graphConfig *graphConfig) (map[string]*execNode, error) {
|
||||
nodes := make(map[string]*execNode)
|
||||
for _, node := range graphConfig.Nodes {
|
||||
var varName string
|
||||
className := node.Class
|
||||
if name, _, ok := getEntranceNodeNameAndID(className); ok {
|
||||
className = name
|
||||
}
|
||||
|
||||
// 获取不到node,则获取变量node
|
||||
exec := gp.execPool.GetExec(className)
|
||||
if exec == nil {
|
||||
exec, varName = gp.genVarExec(&node, graphConfig)
|
||||
if exec == nil {
|
||||
return nil, fmt.Errorf("%s node has not been registered", node.Class)
|
||||
}
|
||||
}
|
||||
|
||||
nodes[node.Id] = &execNode{
|
||||
Id: node.Id,
|
||||
execNode: exec,
|
||||
preInPort: make([]*prePortNode, exec.GetInPortCount()),
|
||||
inPortDefaultValue: node.PortDefault,
|
||||
variableName: varName,
|
||||
}
|
||||
}
|
||||
|
||||
return nodes, nil
|
||||
}
|
||||
|
||||
func (gp *GraphPool) prepareOneNode(mapNodeExec map[string]*execNode, nodeExec *execNode, graphConfig *graphConfig, recursion *int) error {
|
||||
*recursion++
|
||||
if *recursion > 256 {
|
||||
return fmt.Errorf("recursion too deep")
|
||||
}
|
||||
|
||||
// 找到所有出口
|
||||
var idx int
|
||||
for ; nodeExec.execNode.IsOutPortExec(idx) && idx < nodeExec.execNode.GetOutPortCount(); idx++ {
|
||||
// 找到出口结点
|
||||
nextExecNode := gp.findOutNextNode(graphConfig, mapNodeExec, nodeExec.Id, idx)
|
||||
nodeExec.nextNode = append(nodeExec.nextNode, nextExecNode)
|
||||
if nextExecNode != nil {
|
||||
nextExecNode.beConnect = true
|
||||
}
|
||||
}
|
||||
|
||||
// 将所有的next填充next
|
||||
for _, nextOne := range nodeExec.nextNode {
|
||||
if nextOne == nil {
|
||||
continue
|
||||
}
|
||||
// 对出口进行预处理
|
||||
err := gp.prepareOneNode(mapNodeExec, nextOne, graphConfig, recursion)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (gp *GraphPool) findOutNextNode(graphConfig *graphConfig, mapNodeExec map[string]*execNode, sourceNodeID string, sourcePortIdx int) *execNode {
|
||||
// 找到出口的NodeID
|
||||
for _, edge := range graphConfig.Edges {
|
||||
if edge.SourceNodeID == sourceNodeID && edge.SourcePortId == sourcePortIdx {
|
||||
return mapNodeExec[edge.DesNodeId]
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// prepareOneEntrance 先处理执行Exec入出口连线
|
||||
func (gp *GraphPool) prepareOneEntrance(graphName string, entranceID int64, nodeCfg *nodeConfig, graphConfig *graphConfig) error {
|
||||
// 将所有的Node执行结点生成出来
|
||||
mapNodes, err := gp.genAllNode(graphConfig)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// 从入口结点开始做预处理,将next结点都统一生成
|
||||
nodeExec := mapNodes[nodeCfg.Id]
|
||||
if nodeExec == nil {
|
||||
return fmt.Errorf("entrance node %s not found", nodeCfg.Id)
|
||||
}
|
||||
|
||||
nodeExec.isEntrance = true
|
||||
err = gp.prepareOneNode(mapNodes, nodeExec, graphConfig, new(int))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// 处理inPort前置结点
|
||||
err = gp.prepareInPort(mapNodes, graphConfig)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
gr, ok := gp.mapGraphs[graphName]
|
||||
if !ok {
|
||||
gr = &baseGraph{}
|
||||
gr.entrance = make(map[int64]*execNode, 16)
|
||||
gp.mapGraphs[graphName] = gr
|
||||
}
|
||||
|
||||
gr.entrance[entranceID] = nodeExec
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (gp *GraphPool) findPreInPortNode(mapNodes map[string]*execNode, nodeExec *execNode, graphConfig *graphConfig, portIdx int) *prePortNode {
|
||||
for _, edge := range graphConfig.Edges {
|
||||
if edge.DesNodeId == nodeExec.Id && edge.DesPortId == portIdx {
|
||||
srcNode := mapNodes[edge.SourceNodeID]
|
||||
if srcNode == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
var preNode prePortNode
|
||||
preNode.node = srcNode
|
||||
preNode.outPortId = edge.SourcePortId
|
||||
|
||||
return &preNode
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (gp *GraphPool) preparePreInPortNode(mapNodes map[string]*execNode, nodeExec *execNode, graphConfig *graphConfig) error {
|
||||
// 找到当前结点的所有inPort的前一个端口
|
||||
for i := 0; i < nodeExec.execNode.GetInPortCount(); i++ {
|
||||
// 如果是执行结点,则跳过
|
||||
if nodeExec.execNode.IsInPortExec(i) {
|
||||
continue
|
||||
}
|
||||
|
||||
// 找到入口的上一个结点
|
||||
preNode := gp.findPreInPortNode(mapNodes, nodeExec, graphConfig, i)
|
||||
if preNode == nil {
|
||||
continue
|
||||
}
|
||||
nodeExec.preInPort[i] = preNode
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (gp *GraphPool) prepareInPort(mapNodeExec map[string]*execNode, graphConfig *graphConfig) error {
|
||||
for _, e := range mapNodeExec {
|
||||
// 对当前结点的入口进行预处理
|
||||
err := gp.preparePreInPortNode(mapNodeExec, e, graphConfig)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
296
util/blueprint/mathnode.go
Normal file
296
util/blueprint/mathnode.go
Normal file
@@ -0,0 +1,296 @@
|
||||
package blueprint
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math/rand"
|
||||
)
|
||||
|
||||
|
||||
// AddInt 加(int)
|
||||
type AddInt struct {
|
||||
BaseExecNode
|
||||
}
|
||||
|
||||
func (em *AddInt) GetName() string {
|
||||
return "AddInt"
|
||||
}
|
||||
|
||||
func (em *AddInt) Exec() (int, error) {
|
||||
inPortA := em.GetInPort(0)
|
||||
if inPortA == nil {
|
||||
return -1, fmt.Errorf("AddInt inParam 1 not found")
|
||||
}
|
||||
|
||||
inPortB := em.GetInPort(1)
|
||||
if inPortB == nil {
|
||||
return -1, fmt.Errorf("AddInt inParam 2 not found")
|
||||
}
|
||||
|
||||
outPortRet := em.GetOutPort(0)
|
||||
if outPortRet == nil {
|
||||
return -1, fmt.Errorf("AddInt outParam not found")
|
||||
}
|
||||
|
||||
inA, ok := inPortA.GetInt()
|
||||
if !ok {
|
||||
return -1, fmt.Errorf("AddInt inParam 1 not found")
|
||||
}
|
||||
inB, ok := inPortB.GetInt()
|
||||
if !ok {
|
||||
return -1, fmt.Errorf("AddInt inParam 2 not found")
|
||||
}
|
||||
ret := inA + inB
|
||||
outPortRet.SetInt(ret)
|
||||
|
||||
return -1, nil
|
||||
}
|
||||
|
||||
// SubInt 减(int)
|
||||
type SubInt struct {
|
||||
BaseExecNode
|
||||
}
|
||||
|
||||
func (em *SubInt) GetName() string {
|
||||
return "SubInt"
|
||||
}
|
||||
|
||||
func (em *SubInt) Exec() (int, error) {
|
||||
inPortA := em.GetInPort(0)
|
||||
if inPortA == nil {
|
||||
return -1, fmt.Errorf("AddInt inParam 1 not found")
|
||||
}
|
||||
|
||||
inPortB := em.GetInPort(1)
|
||||
if inPortB == nil {
|
||||
return -1, fmt.Errorf("AddInt inParam 2 not found")
|
||||
}
|
||||
|
||||
inPortAbs := em.GetInPort(2)
|
||||
if inPortAbs == nil {
|
||||
return -1, fmt.Errorf("AddInt inParam 2 not found")
|
||||
}
|
||||
|
||||
outPortRet := em.GetOutPort(0)
|
||||
if outPortRet == nil {
|
||||
return -1, fmt.Errorf("AddInt outParam not found")
|
||||
}
|
||||
|
||||
inA, ok := inPortA.GetInt()
|
||||
if !ok {
|
||||
return -1, fmt.Errorf("AddInt inParam 1 not found")
|
||||
}
|
||||
inB, ok := inPortB.GetInt()
|
||||
if !ok {
|
||||
return -1, fmt.Errorf("AddInt inParam 2 not found")
|
||||
}
|
||||
isAbs, ok := inPortAbs.GetBool()
|
||||
if !ok {
|
||||
return -1, fmt.Errorf("AddInt inParam 2 not found")
|
||||
}
|
||||
ret := inA - inB
|
||||
if isAbs && ret < 0 {
|
||||
ret *= -1
|
||||
}
|
||||
|
||||
outPortRet.SetInt(ret)
|
||||
|
||||
return -1, nil
|
||||
}
|
||||
|
||||
// MulInt 乘(int)
|
||||
type MulInt struct {
|
||||
BaseExecNode
|
||||
}
|
||||
|
||||
func (em *MulInt) GetName() string {
|
||||
return "MulInt"
|
||||
}
|
||||
|
||||
func (em *MulInt) Exec() (int, error) {
|
||||
inPortA := em.GetInPort(0)
|
||||
if inPortA == nil {
|
||||
return -1, fmt.Errorf("AddInt inParam 1 not found")
|
||||
}
|
||||
|
||||
inPortB := em.GetInPort(1)
|
||||
if inPortB == nil {
|
||||
return -1, fmt.Errorf("AddInt inParam 2 not found")
|
||||
}
|
||||
|
||||
outPortRet := em.GetOutPort(0)
|
||||
if outPortRet == nil {
|
||||
return -1, fmt.Errorf("AddInt outParam not found")
|
||||
}
|
||||
|
||||
inA, ok := inPortA.GetInt()
|
||||
if !ok {
|
||||
return -1, fmt.Errorf("AddInt inParam 1 not found")
|
||||
}
|
||||
inB, ok := inPortB.GetInt()
|
||||
if !ok {
|
||||
return -1, fmt.Errorf("AddInt inParam 2 not found")
|
||||
}
|
||||
|
||||
outPortRet.SetInt(inA * inB)
|
||||
|
||||
return -1, nil
|
||||
}
|
||||
|
||||
// DivInt 除(int)
|
||||
type DivInt struct {
|
||||
BaseExecNode
|
||||
}
|
||||
|
||||
func (em *DivInt) GetName() string {
|
||||
return "DivInt"
|
||||
}
|
||||
|
||||
func (em *DivInt) Exec() (int, error) {
|
||||
inPortA := em.GetInPort(0)
|
||||
if inPortA == nil {
|
||||
return -1, fmt.Errorf("AddInt inParam 1 not found")
|
||||
}
|
||||
|
||||
inPortB := em.GetInPort(1)
|
||||
if inPortB == nil {
|
||||
return -1, fmt.Errorf("AddInt inParam 2 not found")
|
||||
}
|
||||
|
||||
inPortRound := em.GetInPort(2)
|
||||
if inPortRound == nil {
|
||||
return -1, fmt.Errorf("AddInt inParam 2 not found")
|
||||
}
|
||||
|
||||
outPortRet := em.GetOutPort(0)
|
||||
if outPortRet == nil {
|
||||
return -1, fmt.Errorf("AddInt outParam not found")
|
||||
}
|
||||
|
||||
inA, ok := inPortA.GetInt()
|
||||
if !ok {
|
||||
return -1, fmt.Errorf("AddInt inParam 1 not found")
|
||||
}
|
||||
inB, ok := inPortB.GetInt()
|
||||
if !ok {
|
||||
return -1, fmt.Errorf("AddInt inParam 2 not found")
|
||||
}
|
||||
isRound, ok := inPortRound.GetBool()
|
||||
if !ok {
|
||||
return -1, fmt.Errorf("AddInt inParam 2 not found")
|
||||
}
|
||||
|
||||
if inB == 0 {
|
||||
return -1, fmt.Errorf("div zero error")
|
||||
}
|
||||
|
||||
var ret int64
|
||||
if isRound {
|
||||
ret = (inA + inB/2) / inB
|
||||
} else {
|
||||
ret = inA / inB
|
||||
}
|
||||
|
||||
outPortRet.SetInt(ret)
|
||||
|
||||
return -1, nil
|
||||
}
|
||||
|
||||
// ModInt 取模(int)
|
||||
type ModInt struct {
|
||||
BaseExecNode
|
||||
}
|
||||
|
||||
func (em *ModInt) GetName() string {
|
||||
return "ModInt"
|
||||
}
|
||||
|
||||
func (em *ModInt) Exec() (int, error) {
|
||||
inPortA := em.GetInPort(0)
|
||||
if inPortA == nil {
|
||||
return -1, fmt.Errorf("AddInt inParam 1 not found")
|
||||
}
|
||||
|
||||
inPortB := em.GetInPort(1)
|
||||
if inPortB == nil {
|
||||
return -1, fmt.Errorf("AddInt inParam 2 not found")
|
||||
}
|
||||
|
||||
outPortRet := em.GetOutPort(0)
|
||||
if outPortRet == nil {
|
||||
return -1, fmt.Errorf("AddInt outParam not found")
|
||||
}
|
||||
|
||||
inA, ok := inPortA.GetInt()
|
||||
if !ok {
|
||||
return -1, fmt.Errorf("AddInt inParam 1 not found")
|
||||
}
|
||||
inB, ok := inPortB.GetInt()
|
||||
if !ok {
|
||||
return -1, fmt.Errorf("AddInt inParam 2 not found")
|
||||
}
|
||||
if inB == 0 {
|
||||
return -1, fmt.Errorf("mod zero error")
|
||||
}
|
||||
|
||||
outPortRet.SetInt(inA % inB)
|
||||
|
||||
return -1, nil
|
||||
}
|
||||
|
||||
// RandNumber 范围随机[0,99]
|
||||
type RandNumber struct {
|
||||
BaseExecNode
|
||||
}
|
||||
|
||||
func (em *RandNumber) GetName() string {
|
||||
return "RandNumber"
|
||||
}
|
||||
|
||||
func (em *RandNumber) Exec() (int, error) {
|
||||
inPortSeed := em.GetInPort(0)
|
||||
if inPortSeed == nil {
|
||||
return -1, fmt.Errorf("AddInt inParam 1 not found")
|
||||
}
|
||||
|
||||
inPortMin := em.GetInPort(1)
|
||||
if inPortMin == nil {
|
||||
return -1, fmt.Errorf("AddInt inParam 2 not found")
|
||||
}
|
||||
|
||||
inPortMax := em.GetInPort(2)
|
||||
if inPortMax == nil {
|
||||
return -1, fmt.Errorf("AddInt inParam 2 not found")
|
||||
}
|
||||
|
||||
outPortRet := em.GetOutPort(0)
|
||||
if outPortRet == nil {
|
||||
return -1, fmt.Errorf("AddInt outParam not found")
|
||||
}
|
||||
|
||||
inSeed, ok := inPortSeed.GetInt()
|
||||
if !ok {
|
||||
return -1, fmt.Errorf("AddInt inParam 1 not found")
|
||||
}
|
||||
inMin, ok := inPortMin.GetInt()
|
||||
if !ok {
|
||||
return -1, fmt.Errorf("AddInt inParam 2 not found")
|
||||
}
|
||||
inMax, ok := inPortMax.GetInt()
|
||||
if !ok {
|
||||
return -1, fmt.Errorf("AddInt inParam 2 not found")
|
||||
}
|
||||
|
||||
var ret int64
|
||||
if inSeed > 0 {
|
||||
r := rand.New(rand.NewSource(inSeed))
|
||||
if r == nil {
|
||||
return -1, fmt.Errorf("RandNumber fail")
|
||||
}
|
||||
ret = int64(r.Intn(int(inMax-inMin+1))) + inMin
|
||||
} else {
|
||||
ret = int64(rand.Intn(int(inMax-inMin+1))) + inMin
|
||||
}
|
||||
|
||||
outPortRet.SetInt(ret)
|
||||
return -1, nil
|
||||
}
|
||||
204
util/blueprint/node.go
Normal file
204
util/blueprint/node.go
Normal file
@@ -0,0 +1,204 @@
|
||||
package blueprint
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/duanhf2012/origin/v2/log"
|
||||
)
|
||||
|
||||
type prePortNode struct {
|
||||
node *execNode // 上个结点
|
||||
outPortId int // 对应上一个结点的OutPortId
|
||||
}
|
||||
|
||||
type execNode struct {
|
||||
Id string
|
||||
execNode IInnerExecNode
|
||||
|
||||
nextNode []*execNode
|
||||
nextIdx int
|
||||
|
||||
preInPort []*prePortNode // Port的上一个结点
|
||||
inPortDefaultValue map[string]any
|
||||
|
||||
variableName string // 如果是变量,则有变量名
|
||||
beConnect bool // 是否有被连线
|
||||
isEntrance bool // 是否是入口结点
|
||||
}
|
||||
|
||||
// HasBeConnectLine 是否有被连线
|
||||
func (en *execNode) HasBeConnectLine() bool {
|
||||
return en.beConnect
|
||||
}
|
||||
|
||||
// HasInPortExec 有前置执行入口
|
||||
func (en *execNode) HasInPortExec() bool {
|
||||
return en.execNode.IsInPortExec(0)
|
||||
}
|
||||
|
||||
// HasOutPortExec 有前置执行入口
|
||||
func (en *execNode) HasOutPortExec() bool {
|
||||
return en.execNode.IsOutPortExec(0)
|
||||
}
|
||||
|
||||
func (en *execNode) GetInPortDefaultValue(index int) any {
|
||||
key := fmt.Sprintf("%d", index)
|
||||
v, ok := en.inPortDefaultValue[key]
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
return v
|
||||
}
|
||||
|
||||
func (en *execNode) GetInPortDefaultIntArrayValue(index int) []int64 {
|
||||
val := en.GetInPortDefaultValue(index)
|
||||
if val == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
var arrayInt []int64
|
||||
arrayVal := val.([]any)
|
||||
for i := range arrayVal {
|
||||
if intVal, ok := arrayVal[i].(float64); ok {
|
||||
arrayInt = append(arrayInt, int64(intVal))
|
||||
}
|
||||
}
|
||||
return arrayInt
|
||||
}
|
||||
|
||||
func (en *execNode) GetInPortDefaultStringArrayValue(index int) []string {
|
||||
val := en.GetInPortDefaultValue(index)
|
||||
if val == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
return val.([]string)
|
||||
}
|
||||
|
||||
func (en *execNode) Next() *execNode {
|
||||
if en.nextIdx >= len(en.nextNode) {
|
||||
return nil
|
||||
}
|
||||
|
||||
return en.nextNode[en.nextIdx]
|
||||
}
|
||||
|
||||
func (en *execNode) exec(gr *Graph) (int, error) {
|
||||
e, ok := en.execNode.(IExecNode)
|
||||
if !ok {
|
||||
return -1, fmt.Errorf("exec node %s not exec", en.execNode.GetName())
|
||||
}
|
||||
|
||||
node, ok := en.execNode.(IBaseExecNode)
|
||||
if !ok {
|
||||
return -1, fmt.Errorf("exec node %s not exec", en.execNode.GetName())
|
||||
}
|
||||
|
||||
// 执行完,要恢复上下文,结点的BaseExecNode会被执行时修改
|
||||
ctx, exNode := node.getExecNodeInfo()
|
||||
defer func() {
|
||||
node.setExecNodeInfo(ctx, exNode)
|
||||
}()
|
||||
|
||||
if err := node.initExecNode(gr, en); err != nil {
|
||||
return -1, err
|
||||
}
|
||||
|
||||
return e.Exec()
|
||||
}
|
||||
|
||||
func (en *execNode) doSetInPort(gr *Graph, index int, inPort IPort) error {
|
||||
// 找到当前Node的InPort的index的前一个结点
|
||||
preNode := en.preInPort[index]
|
||||
// 如果前一个结点为空,则填充默认值
|
||||
if preNode == nil {
|
||||
err := inPort.setAnyVale(en.GetInPortDefaultValue(index))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
if _, ok := gr.context[preNode.node.Id]; !ok ||
|
||||
(!preNode.node.HasBeConnectLine() && !preNode.node.isEntrance) {
|
||||
// 如果前一个结点没有执行过,则递归执行前一个结点
|
||||
err := preNode.node.Do(gr)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
// 判断上一个结点是否已经执行过
|
||||
if _, ok := gr.context[preNode.node.Id]; ok {
|
||||
outPort := gr.GetNodeOutPortValue(preNode.node.Id, preNode.outPortId)
|
||||
if outPort == nil {
|
||||
return fmt.Errorf("pre node %s out port index %d not found", preNode.node.Id, preNode.outPortId)
|
||||
}
|
||||
|
||||
inPort.SetValue(outPort)
|
||||
return nil
|
||||
}
|
||||
|
||||
return fmt.Errorf("pre node %s not exec", preNode.node.Id)
|
||||
}
|
||||
|
||||
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{
|
||||
InputPorts: inPorts,
|
||||
OutputPorts: outPorts,
|
||||
}
|
||||
|
||||
startOutIdx := en.execNode.GetOutPortParamStartIndex()
|
||||
for i := 0; i < len(outPortArgs); i++ {
|
||||
if i+startOutIdx >= len(outPorts) {
|
||||
return fmt.Errorf("args %d not found in node %s", i, en.execNode.GetName())
|
||||
}
|
||||
|
||||
if err := outPorts[i+startOutIdx].setAnyVale(outPortArgs[i]); err != nil {
|
||||
return fmt.Errorf("args %d set value error: %w", i, err)
|
||||
}
|
||||
}
|
||||
|
||||
// 处理InPort结点值
|
||||
var err error
|
||||
for portId := range inPorts {
|
||||
if en.execNode.IsInPortExec(portId) {
|
||||
continue
|
||||
}
|
||||
|
||||
err = en.doSetInPort(gr, portId, inPorts[portId])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
// 设置执行器相关的上下文信息
|
||||
// 如果是变量设置变量名
|
||||
// 执行本结点
|
||||
nextIndex, err := en.exec(gr)
|
||||
if err != nil {
|
||||
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
|
||||
}
|
||||
|
||||
if nextIndex < 0 || nextIndex >= len(en.nextNode) {
|
||||
return fmt.Errorf("next index %d not found", nextIndex)
|
||||
}
|
||||
|
||||
if en.nextNode[nextIndex] == nil {
|
||||
return nil
|
||||
}
|
||||
return en.nextNode[nextIndex].Do(gr)
|
||||
}
|
||||
420
util/blueprint/port.go
Normal file
420
util/blueprint/port.go
Normal file
@@ -0,0 +1,420 @@
|
||||
package blueprint
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
const (
|
||||
Config_PortType_Exec = "exec"
|
||||
Config_PortType_Data = "data"
|
||||
Config_DataType_Int = "int"
|
||||
Config_DataType_Integer = "integer"
|
||||
Config_DataType_Float = "float"
|
||||
Config_DataType_Str = "string"
|
||||
Config_DataType_Boolean = "boolean"
|
||||
Config_DataType_Bool = "bool"
|
||||
Config_DataType_Array = "array"
|
||||
)
|
||||
|
||||
type Port[T iPortType] struct {
|
||||
PortVal T
|
||||
}
|
||||
|
||||
func (em *Port[T]) Clone() IPort {
|
||||
arrayData, ok := any(em.PortVal).(Port_Array)
|
||||
if !ok {
|
||||
return &Port[T]{
|
||||
PortVal: em.PortVal,
|
||||
}
|
||||
}
|
||||
|
||||
portArray := Port[Port_Array]{}
|
||||
portArray.PortVal = append(portArray.PortVal, arrayData...)
|
||||
return &portArray
|
||||
}
|
||||
|
||||
func (em *Port[T]) Reset() {
|
||||
var v T
|
||||
em.PortVal = v
|
||||
}
|
||||
|
||||
func (em *Port[T]) GetInt() (Port_Int, bool) {
|
||||
if t, ok := any(em.PortVal).(Port_Int); ok {
|
||||
return t, true
|
||||
}
|
||||
return 0, false
|
||||
}
|
||||
|
||||
func (em *Port[T]) GetFloat() (Port_Float, bool) {
|
||||
if t, ok := any(em.PortVal).(Port_Float); ok {
|
||||
return t, true
|
||||
}
|
||||
return 0, false
|
||||
}
|
||||
|
||||
func (em *Port[T]) GetStr() (Port_Str, bool) {
|
||||
if t, ok := any(em.PortVal).(Port_Str); ok {
|
||||
return t, true
|
||||
}
|
||||
return "", false
|
||||
}
|
||||
|
||||
func (em *Port[T]) GetArrayValInt(idx int) (Port_Int, bool) {
|
||||
if t, ok := any(em.PortVal).(Port_Array); ok {
|
||||
if idx >= 0 && idx < len(t) {
|
||||
return t[idx].IntVal, true
|
||||
}
|
||||
}
|
||||
|
||||
return 0, false
|
||||
}
|
||||
|
||||
func (em *Port[T]) GetArrayValStr(idx int) (string, bool) {
|
||||
if t, ok := any(em.PortVal).(Port_Array); ok {
|
||||
if idx >= 0 && idx < len(t) {
|
||||
return t[idx].StrVal, true
|
||||
}
|
||||
}
|
||||
|
||||
return "", false
|
||||
}
|
||||
|
||||
func (em *Port[T]) GetBool() (Port_Bool, bool) {
|
||||
if t, ok := any(em.PortVal).(Port_Bool); ok {
|
||||
return t, true
|
||||
}
|
||||
return false, false
|
||||
}
|
||||
|
||||
func (em *Port[T]) GetArray() (Port_Array, bool) {
|
||||
if t, ok := any(em.PortVal).(Port_Array); ok {
|
||||
return t, true
|
||||
}
|
||||
return nil, false
|
||||
}
|
||||
|
||||
func (em *Port[T]) SetInt(val Port_Int) bool {
|
||||
if t, ok := any(&em.PortVal).(*Port_Int); ok {
|
||||
*t = val
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (em *Port[T]) SetFloat(val Port_Float) bool {
|
||||
if t, ok := any(&em.PortVal).(*Port_Float); ok {
|
||||
*t = val
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (em *Port[T]) SetStr(val Port_Str) bool {
|
||||
if t, ok := any(&em.PortVal).(*Port_Str); ok {
|
||||
*t = val
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (em *Port[T]) SetBool(val Port_Bool) bool {
|
||||
if t, ok := any(&em.PortVal).(*Port_Bool); ok {
|
||||
*t = val
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (em *Port[T]) SetArrayValInt(idx int, val Port_Int) bool {
|
||||
if t, ok := any(em.PortVal).(Port_Array); ok {
|
||||
if idx >= 0 && idx < len(t) {
|
||||
t[idx].IntVal = val
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (em *Port[T]) SetArrayValStr(idx int, val Port_Str) bool {
|
||||
if t, ok := any(em.PortVal).(Port_Array); ok {
|
||||
if idx >= 0 && idx < len(t) {
|
||||
(t)[idx].StrVal = val
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (em *Port[T]) AppendArrayValInt(val Port_Int) bool {
|
||||
if t, ok := any(&em.PortVal).(*Port_Array); ok {
|
||||
*t = append(*t, ArrayData{IntVal: val})
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (em *Port[T]) AppendArrayValStr(val Port_Str) bool {
|
||||
if t, ok := any(&em.PortVal).(*Port_Array); ok {
|
||||
*t = append(*t, ArrayData{StrVal: val})
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (em *Port[T]) AppendArrayData(val ArrayData) bool {
|
||||
if t, ok := any(&em.PortVal).(*Port_Array); ok {
|
||||
*t = append(*t, val)
|
||||
return true
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
func (em *Port[T]) GetArrayLen() Port_Int {
|
||||
if t, ok := any(&em.PortVal).(*Port_Array); ok {
|
||||
return Port_Int(len(*t))
|
||||
}
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
func (em *Port[T]) IsPortExec() bool {
|
||||
_, ok := any(em.PortVal).(Port_Exec)
|
||||
return ok
|
||||
}
|
||||
|
||||
func (em *Port[T]) convertInt64(v any) (int64, bool) {
|
||||
switch v.(type) {
|
||||
case int:
|
||||
return int64(v.(int)), true
|
||||
case int64:
|
||||
return v.(int64), true
|
||||
case int32:
|
||||
return int64(v.(int32)), true
|
||||
case int16:
|
||||
return int64(v.(int16)), true
|
||||
case int8:
|
||||
return int64(v.(int8)), true
|
||||
case uint64:
|
||||
return int64(v.(uint64)), true
|
||||
case uint32:
|
||||
return int64(v.(uint32)), true
|
||||
case uint16:
|
||||
return int64(v.(uint16)), true
|
||||
case uint8:
|
||||
return int64(v.(uint8)), true
|
||||
case uint:
|
||||
return int64(v.(uint)), true
|
||||
default:
|
||||
return 0, false
|
||||
}
|
||||
}
|
||||
|
||||
func (em *Port[T]) setAnyVale(v any) error {
|
||||
switch v.(type) {
|
||||
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)
|
||||
}
|
||||
|
||||
switch any(em.PortVal).(type) {
|
||||
case Port_Int:
|
||||
em.SetInt(val)
|
||||
case Port_Float:
|
||||
em.SetFloat(Port_Float(val))
|
||||
case Port_Str:
|
||||
em.SetStr(fmt.Sprintf("%d", int64(val)))
|
||||
case Port_Bool:
|
||||
em.SetBool(int64(val) != 0)
|
||||
default:
|
||||
return fmt.Errorf("port type is %T, but value is %v", em.PortVal, v)
|
||||
}
|
||||
case float64:
|
||||
fV := v.(float64)
|
||||
switch any(em.PortVal).(type) {
|
||||
case Port_Int:
|
||||
em.SetInt(Port_Int(fV))
|
||||
case Port_Float:
|
||||
em.SetFloat(fV)
|
||||
case Port_Str:
|
||||
em.SetStr(fmt.Sprintf("%d", int64(fV)))
|
||||
case Port_Bool:
|
||||
em.SetBool(int64(fV) != 0)
|
||||
default:
|
||||
return fmt.Errorf("port type is %T, but value is %v", em.PortVal, v)
|
||||
}
|
||||
case string:
|
||||
strV := v.(string)
|
||||
switch any(em.PortVal).(type) {
|
||||
case Port_Int:
|
||||
val, err := strconv.Atoi(strV)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
em.SetInt(Port_Int(val))
|
||||
case Port_Float:
|
||||
fV, err := strconv.ParseFloat(strV, 64)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
em.SetFloat(fV)
|
||||
case Port_Str:
|
||||
em.SetStr(strV)
|
||||
case Port_Bool:
|
||||
val, err := strconv.ParseBool(strV)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
em.SetBool(val)
|
||||
default:
|
||||
return fmt.Errorf("port type is %T, but value is %v", em.PortVal, v)
|
||||
}
|
||||
case bool:
|
||||
strV := v.(bool)
|
||||
switch any(em.PortVal).(type) {
|
||||
case Port_Int:
|
||||
return fmt.Errorf("port type is int, but value is %v", strV)
|
||||
case Port_Float:
|
||||
return fmt.Errorf("port type is float, but value is %v", strV)
|
||||
case Port_Str:
|
||||
return fmt.Errorf("port type is string, but value is %v", strV)
|
||||
case Port_Bool:
|
||||
em.SetBool(strV)
|
||||
default:
|
||||
return fmt.Errorf("port type is %T, but value is %v", em.PortVal, v)
|
||||
}
|
||||
case []int64:
|
||||
arr := v.([]int64)
|
||||
for _, val := range arr {
|
||||
em.AppendArrayValInt(val)
|
||||
}
|
||||
case []int32:
|
||||
arr := v.([]int32)
|
||||
for _, val := range arr {
|
||||
em.AppendArrayValInt(Port_Int(val))
|
||||
}
|
||||
case []int16:
|
||||
arr := v.([]int16)
|
||||
for _, val := range arr {
|
||||
em.AppendArrayValInt(Port_Int(val))
|
||||
}
|
||||
case []int8:
|
||||
arr := v.([]int8)
|
||||
for _, val := range arr {
|
||||
em.AppendArrayValInt(Port_Int(val))
|
||||
}
|
||||
case []uint64:
|
||||
arr := v.([]uint64)
|
||||
for _, val := range arr {
|
||||
em.AppendArrayValInt(Port_Int(val))
|
||||
}
|
||||
case []uint32:
|
||||
arr := v.([]uint32)
|
||||
for _, val := range arr {
|
||||
em.AppendArrayValInt(Port_Int(val))
|
||||
}
|
||||
case []uint16:
|
||||
arr := v.([]uint16)
|
||||
for _, val := range arr {
|
||||
em.AppendArrayValInt(Port_Int(val))
|
||||
}
|
||||
case []uint8:
|
||||
arr := v.([]uint8)
|
||||
for _, val := range arr {
|
||||
em.AppendArrayValInt(Port_Int(val))
|
||||
}
|
||||
case []string:
|
||||
arr := v.([]string)
|
||||
for _, val := range arr {
|
||||
em.AppendArrayValStr(val)
|
||||
}
|
||||
case Port_Array:
|
||||
arr := v.(Port_Array)
|
||||
for _, val := range arr {
|
||||
em.AppendArrayValInt(val.IntVal)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (em *Port[T]) SetValue(val IPort) bool {
|
||||
valT, ok := val.(*Port[T])
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
em.PortVal = valT.PortVal
|
||||
return true
|
||||
}
|
||||
|
||||
type IPort interface {
|
||||
GetInt() (Port_Int, bool)
|
||||
GetFloat() (Port_Float, bool)
|
||||
GetStr() (Port_Str, bool)
|
||||
GetArrayValInt(idx int) (Port_Int, bool)
|
||||
GetArrayValStr(idx int) (Port_Str, bool)
|
||||
GetBool() (Port_Bool, bool)
|
||||
GetArray() (Port_Array, bool)
|
||||
|
||||
SetInt(val Port_Int) bool
|
||||
SetFloat(val Port_Float) bool
|
||||
SetStr(val Port_Str) bool
|
||||
SetBool(val Port_Bool) bool
|
||||
SetArrayValInt(idx int, val Port_Int) bool
|
||||
SetArrayValStr(idx int, val Port_Str) bool
|
||||
AppendArrayValInt(val Port_Int) bool
|
||||
AppendArrayValStr(val Port_Str) bool
|
||||
GetArrayLen() Port_Int
|
||||
Clone() IPort
|
||||
Reset()
|
||||
|
||||
IsPortExec() bool
|
||||
|
||||
setAnyVale(v any) error
|
||||
SetValue(val IPort) bool
|
||||
}
|
||||
|
||||
func NewPortExec() IPort {
|
||||
return &Port[Port_Exec]{}
|
||||
}
|
||||
|
||||
func NewPortInt() IPort {
|
||||
return &Port[Port_Int]{}
|
||||
}
|
||||
|
||||
func NewPortFloat() IPort {
|
||||
return &Port[Port_Float]{}
|
||||
}
|
||||
|
||||
func NewPortStr() IPort {
|
||||
return &Port[Port_Str]{}
|
||||
}
|
||||
|
||||
func NewPortBool() IPort {
|
||||
return &Port[Port_Bool]{}
|
||||
}
|
||||
|
||||
func NewPortArray() IPort {
|
||||
return &Port[Port_Array]{}
|
||||
}
|
||||
|
||||
func NewPortByType(typ string) IPort {
|
||||
switch typ {
|
||||
case Config_PortType_Exec:
|
||||
return NewPortExec()
|
||||
case Config_DataType_Int, Config_DataType_Integer:
|
||||
return NewPortInt()
|
||||
case Config_DataType_Float:
|
||||
return NewPortFloat()
|
||||
case Config_DataType_Str:
|
||||
return NewPortStr()
|
||||
case Config_DataType_Bool, Config_DataType_Boolean:
|
||||
return NewPortBool()
|
||||
case Config_DataType_Array:
|
||||
return NewPortArray()
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
784
util/blueprint/sysnodes.go
Normal file
784
util/blueprint/sysnodes.go
Normal file
@@ -0,0 +1,784 @@
|
||||
package blueprint
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math/rand/v2"
|
||||
"time"
|
||||
|
||||
"github.com/duanhf2012/origin/v2/log"
|
||||
)
|
||||
|
||||
// 系统入口ID定义,1000以内
|
||||
const (
|
||||
EntranceID_IntParam = 1
|
||||
EntranceID_ArrayParam = 2
|
||||
EntranceID_Timer = 3
|
||||
)
|
||||
|
||||
type Entrance_ArrayParam struct {
|
||||
BaseExecNode
|
||||
}
|
||||
|
||||
func (em *Entrance_ArrayParam) GetName() string {
|
||||
return "Entrance_ArrayParam"
|
||||
}
|
||||
|
||||
func (em *Entrance_ArrayParam) Exec() (int, error) {
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
type Entrance_IntParam struct {
|
||||
BaseExecNode
|
||||
}
|
||||
|
||||
func (em *Entrance_IntParam) GetName() string {
|
||||
return "Entrance_IntParam"
|
||||
}
|
||||
|
||||
func (em *Entrance_IntParam) Exec() (int, error) {
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
type Entrance_Timer struct {
|
||||
BaseExecNode
|
||||
}
|
||||
|
||||
func (em *Entrance_Timer) GetName() string {
|
||||
return "Entrance_Timer"
|
||||
}
|
||||
|
||||
func (em *Entrance_Timer) Exec() (int, error) {
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
type DebugOutput struct {
|
||||
BaseExecNode
|
||||
}
|
||||
|
||||
func (em *DebugOutput) GetName() string {
|
||||
return "DebugOutput"
|
||||
}
|
||||
|
||||
func (em *DebugOutput) Exec() (int, error) {
|
||||
val, ok := em.GetInPortInt(1)
|
||||
if !ok {
|
||||
return 0, fmt.Errorf("output Exec inParam not found")
|
||||
}
|
||||
|
||||
valStr, ok := em.GetInPortStr(2)
|
||||
if !ok {
|
||||
return 0, fmt.Errorf("output Exec inParam not found")
|
||||
}
|
||||
|
||||
valArray, ok := em.GetInPortArray(3)
|
||||
if !ok {
|
||||
return 0, fmt.Errorf("output Exec inParam not found")
|
||||
}
|
||||
|
||||
log.Debug("DebugOutput Exec", log.Any("param1", val), log.Any("param2", valStr), log.Any("param3", valArray))
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
type Sequence struct {
|
||||
BaseExecNode
|
||||
}
|
||||
|
||||
func (em *Sequence) GetName() string {
|
||||
return "Sequence"
|
||||
}
|
||||
|
||||
func (em *Sequence) Exec() (int, error) {
|
||||
for i := range em.outPort {
|
||||
if !em.outPort[i].IsPortExec() {
|
||||
break
|
||||
}
|
||||
|
||||
err := em.DoNext(i)
|
||||
if err != nil {
|
||||
return -1, err
|
||||
}
|
||||
}
|
||||
|
||||
return -1, nil
|
||||
}
|
||||
|
||||
type ForeachIntArray struct {
|
||||
BaseExecNode
|
||||
}
|
||||
|
||||
func (em *ForeachIntArray) GetName() string {
|
||||
return "ForeachIntArray"
|
||||
}
|
||||
|
||||
func (em *ForeachIntArray) Exec() (int, error) {
|
||||
array, ok := em.GetInPortArray(1)
|
||||
if !ok {
|
||||
return 0, fmt.Errorf("ForeachIntArray Exec inParam 1 not found")
|
||||
}
|
||||
|
||||
for i := range array {
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
err := em.DoNext(1)
|
||||
if err != nil {
|
||||
return -1, err
|
||||
}
|
||||
|
||||
return -1, nil
|
||||
}
|
||||
|
||||
type Foreach struct {
|
||||
BaseExecNode
|
||||
}
|
||||
|
||||
func (em *Foreach) GetName() string {
|
||||
return "Foreach"
|
||||
}
|
||||
|
||||
func (em *Foreach) Exec() (int, error) {
|
||||
startIndex, ok := em.ExecContext.InputPorts[1].GetInt()
|
||||
if !ok {
|
||||
return 0, fmt.Errorf("foreach Exec inParam not found")
|
||||
}
|
||||
endIndex, ok := em.ExecContext.InputPorts[2].GetInt()
|
||||
if !ok {
|
||||
return 0, fmt.Errorf("foreach Exec inParam not found")
|
||||
}
|
||||
|
||||
for i := startIndex; i < endIndex; i++ {
|
||||
em.ExecContext.OutputPorts[2].SetInt(i)
|
||||
err := em.DoNext(0)
|
||||
if err != nil {
|
||||
return -1, err
|
||||
}
|
||||
}
|
||||
|
||||
err := em.DoNext(1)
|
||||
if err != nil {
|
||||
return -1, err
|
||||
}
|
||||
|
||||
return -1, nil
|
||||
}
|
||||
|
||||
type GetArrayInt struct {
|
||||
BaseExecNode
|
||||
}
|
||||
|
||||
func (em *GetArrayInt) GetName() string {
|
||||
return "GetArrayInt"
|
||||
}
|
||||
|
||||
func (em *GetArrayInt) Exec() (int, error) {
|
||||
inPort := em.GetInPort(0)
|
||||
if inPort == nil {
|
||||
return -1, fmt.Errorf("GetArrayInt inParam not found")
|
||||
}
|
||||
outPort := em.GetOutPort(0)
|
||||
if outPort == nil {
|
||||
return -1, fmt.Errorf("GetArrayInt outParam not found")
|
||||
}
|
||||
|
||||
arrIndexPort := em.GetInPort(1)
|
||||
if arrIndexPort == nil {
|
||||
return -1, fmt.Errorf("GetArrayInt arrIndexParam not found")
|
||||
}
|
||||
arrIndex, ok := arrIndexPort.GetInt()
|
||||
if !ok {
|
||||
return -1, fmt.Errorf("GetArrayInt arrIndexParam not found")
|
||||
}
|
||||
|
||||
if arrIndex < 0 || arrIndex >= inPort.GetArrayLen() {
|
||||
return -1, fmt.Errorf("GetArrayInt arrIndexParam out of range,index %d", arrIndex)
|
||||
}
|
||||
|
||||
val, ok := inPort.GetArrayValInt(int(arrIndex))
|
||||
if !ok {
|
||||
log.Errorf("GetArrayValInt failed, idx:%d", arrIndex)
|
||||
return -1, fmt.Errorf("GetArrayInt inParam not found")
|
||||
}
|
||||
|
||||
outPort.SetInt(val)
|
||||
return -1, nil
|
||||
}
|
||||
|
||||
type GetArrayString struct {
|
||||
BaseExecNode
|
||||
}
|
||||
|
||||
func (em *GetArrayString) GetName() string {
|
||||
return "GetArrayString"
|
||||
}
|
||||
|
||||
func (em *GetArrayString) Exec() (int, error) {
|
||||
inPort := em.GetInPort(0)
|
||||
if inPort == nil {
|
||||
return -1, fmt.Errorf("GetArrayInt inParam 0 not found")
|
||||
}
|
||||
outPort := em.GetOutPort(0)
|
||||
if outPort == nil {
|
||||
return -1, fmt.Errorf("GetArrayInt outParam 0 not found")
|
||||
}
|
||||
|
||||
arrIndexPort := em.GetInPort(1)
|
||||
if arrIndexPort == nil {
|
||||
return -1, fmt.Errorf("GetArrayInt arrIndexParam 1 not found")
|
||||
}
|
||||
arrIndex, ok := arrIndexPort.GetInt()
|
||||
if !ok {
|
||||
return -1, fmt.Errorf("GetArrayInt arrIndexParam not found")
|
||||
}
|
||||
|
||||
if arrIndex < 0 || arrIndex >= inPort.GetArrayLen() {
|
||||
return -1, fmt.Errorf("GetArrayInt arrIndexParam out of range,index %d", arrIndex)
|
||||
}
|
||||
|
||||
val, ok := inPort.GetArrayValStr(int(arrIndex))
|
||||
if !ok {
|
||||
log.Errorf("GetArrayValStr failed, idx:%d", arrIndex)
|
||||
return -1, fmt.Errorf("GetArrayInt inParam not found")
|
||||
}
|
||||
|
||||
outPort.SetStr(val)
|
||||
return -1, nil
|
||||
}
|
||||
|
||||
type GetArrayLen struct {
|
||||
BaseExecNode
|
||||
}
|
||||
|
||||
func (em *GetArrayLen) GetName() string {
|
||||
return "GetArrayLen"
|
||||
}
|
||||
|
||||
func (em *GetArrayLen) Exec() (int, error) {
|
||||
inPort := em.GetInPort(0)
|
||||
if inPort == nil {
|
||||
return -1, fmt.Errorf("GetArrayInt inParam 0 not found")
|
||||
}
|
||||
outPort := em.GetOutPort(0)
|
||||
if outPort == nil {
|
||||
return -1, fmt.Errorf("GetArrayInt outParam 0 not found")
|
||||
}
|
||||
|
||||
outPort.SetInt(inPort.GetArrayLen())
|
||||
return -1, nil
|
||||
}
|
||||
|
||||
// BoolIf 布尔判断
|
||||
type BoolIf struct {
|
||||
BaseExecNode
|
||||
}
|
||||
|
||||
func (em *BoolIf) GetName() string {
|
||||
return "BoolIf"
|
||||
}
|
||||
|
||||
func (em *BoolIf) Exec() (int, error) {
|
||||
inPort := em.GetInPort(1)
|
||||
if inPort == nil {
|
||||
return -1, fmt.Errorf("GetArrayInt inParam 1 not found")
|
||||
}
|
||||
|
||||
ret, ok := inPort.GetBool()
|
||||
if !ok {
|
||||
return -1, fmt.Errorf("BoolIf inParam error")
|
||||
}
|
||||
|
||||
if ret {
|
||||
return 1, nil
|
||||
}
|
||||
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
// GreaterThanInteger 大于(整型) >
|
||||
type GreaterThanInteger struct {
|
||||
BaseExecNode
|
||||
}
|
||||
|
||||
func (em *GreaterThanInteger) GetName() string {
|
||||
return "GreaterThanInteger"
|
||||
}
|
||||
|
||||
func (em *GreaterThanInteger) Exec() (int, error) {
|
||||
inPortEqual := em.GetInPort(1)
|
||||
if inPortEqual == nil {
|
||||
return -1, fmt.Errorf("GreaterThanInteger inParam 1 not found")
|
||||
}
|
||||
|
||||
inPortA := em.GetInPort(2)
|
||||
if inPortA == nil {
|
||||
return -1, fmt.Errorf("GreaterThanInteger inParam 1 not found")
|
||||
}
|
||||
|
||||
inPorB := em.GetInPort(3)
|
||||
if inPorB == nil {
|
||||
return -1, fmt.Errorf("GreaterThanInteger inParam 1 not found")
|
||||
}
|
||||
|
||||
ret, ok := inPortEqual.GetBool()
|
||||
if !ok {
|
||||
return -1, fmt.Errorf("GreaterThanInteger inParam 1 error")
|
||||
}
|
||||
|
||||
inA, ok := inPortA.GetInt()
|
||||
if !ok {
|
||||
return -1, fmt.Errorf("GreaterThanInteger inParam 2 error")
|
||||
}
|
||||
inB, ok := inPorB.GetInt()
|
||||
if !ok {
|
||||
return -1, fmt.Errorf("GreaterThanInteger inParam 3 error")
|
||||
}
|
||||
if ret {
|
||||
if inA >= inB {
|
||||
return 1, nil
|
||||
}
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
if inA > inB {
|
||||
return 1, nil
|
||||
}
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
// LessThanInteger 小于(整型) <
|
||||
type LessThanInteger struct {
|
||||
BaseExecNode
|
||||
}
|
||||
|
||||
func (em *LessThanInteger) GetName() string {
|
||||
return "LessThanInteger"
|
||||
}
|
||||
|
||||
func (em *LessThanInteger) Exec() (int, error) {
|
||||
inPortEqual := em.GetInPort(1)
|
||||
if inPortEqual == nil {
|
||||
return -1, fmt.Errorf("GreaterThanInteger inParam 1 not found")
|
||||
}
|
||||
|
||||
inPortA := em.GetInPort(2)
|
||||
if inPortA == nil {
|
||||
return -1, fmt.Errorf("GreaterThanInteger inParam 1 not found")
|
||||
}
|
||||
|
||||
inPorB := em.GetInPort(3)
|
||||
if inPorB == nil {
|
||||
return -1, fmt.Errorf("GreaterThanInteger inParam 1 not found")
|
||||
}
|
||||
|
||||
ret, ok := inPortEqual.GetBool()
|
||||
if !ok {
|
||||
return -1, fmt.Errorf("GreaterThanInteger inParam 1 error")
|
||||
}
|
||||
|
||||
inA, ok := inPortA.GetInt()
|
||||
if !ok {
|
||||
return -1, fmt.Errorf("GreaterThanInteger inParam 2 error")
|
||||
}
|
||||
inB, ok := inPorB.GetInt()
|
||||
if !ok {
|
||||
return -1, fmt.Errorf("GreaterThanInteger inParam 3 error")
|
||||
}
|
||||
if ret {
|
||||
if inA <= inB {
|
||||
return 1, nil
|
||||
}
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
if inA < inB {
|
||||
return 1, nil
|
||||
}
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
// EqualInteger 等于(整型)==
|
||||
type EqualInteger struct {
|
||||
BaseExecNode
|
||||
}
|
||||
|
||||
func (em *EqualInteger) GetName() string {
|
||||
return "EqualInteger"
|
||||
}
|
||||
|
||||
func (em *EqualInteger) Exec() (int, error) {
|
||||
|
||||
inPortA := em.GetInPort(1)
|
||||
if inPortA == nil {
|
||||
return -1, fmt.Errorf("GreaterThanInteger inParam 1 not found")
|
||||
}
|
||||
|
||||
inPorB := em.GetInPort(2)
|
||||
if inPorB == nil {
|
||||
return -1, fmt.Errorf("GreaterThanInteger inParam 1 not found")
|
||||
}
|
||||
|
||||
inA, ok := inPortA.GetInt()
|
||||
if !ok {
|
||||
return -1, fmt.Errorf("GreaterThanInteger inParam 2 error")
|
||||
}
|
||||
inB, ok := inPorB.GetInt()
|
||||
if !ok {
|
||||
return -1, fmt.Errorf("GreaterThanInteger inParam 3 error")
|
||||
}
|
||||
|
||||
if inA == inB {
|
||||
return 1, nil
|
||||
}
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
// RangeCompare 范围比较<=
|
||||
type RangeCompare struct {
|
||||
BaseExecNode
|
||||
}
|
||||
|
||||
func (em *RangeCompare) GetName() string {
|
||||
return "RangeCompare"
|
||||
}
|
||||
|
||||
func (em *RangeCompare) 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
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
|
||||
func (em *Probability) GetName() string {
|
||||
return "Probability"
|
||||
}
|
||||
|
||||
func (em *Probability) Exec() (int, error) {
|
||||
inPortProbability := em.GetInPort(1)
|
||||
if inPortProbability == nil {
|
||||
return -1, fmt.Errorf("GreaterThanInteger inParam 1 not found")
|
||||
}
|
||||
|
||||
inProbability, ok := inPortProbability.GetInt()
|
||||
if !ok {
|
||||
return -1, fmt.Errorf("GreaterThanInteger inParam 1 error")
|
||||
}
|
||||
|
||||
if inProbability > rand.Int64N(10000) {
|
||||
return 1, nil
|
||||
}
|
||||
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
// CreateIntArray 创建整型数组
|
||||
type CreateIntArray struct {
|
||||
BaseExecNode
|
||||
}
|
||||
|
||||
func (em *CreateIntArray) GetName() string {
|
||||
return "CreateIntArray"
|
||||
}
|
||||
|
||||
func (em *CreateIntArray) Exec() (int, error) {
|
||||
intArray := em.execNode.GetInPortDefaultIntArrayValue(0)
|
||||
if intArray == nil {
|
||||
return -1, fmt.Errorf("CreateIntArray inParam 0 not found")
|
||||
}
|
||||
|
||||
outPort := em.GetOutPort(0)
|
||||
if outPort == nil {
|
||||
return -1, fmt.Errorf("GetArrayInt outParam 0 not found")
|
||||
}
|
||||
|
||||
for _, v := range intArray {
|
||||
outPort.AppendArrayValInt(v)
|
||||
}
|
||||
|
||||
return -1, nil
|
||||
}
|
||||
|
||||
// CreateStringArray 创建字符串数组
|
||||
type CreateStringArray struct {
|
||||
BaseExecNode
|
||||
}
|
||||
|
||||
func (em *CreateStringArray) GetName() string {
|
||||
return "CreateStringArray"
|
||||
}
|
||||
|
||||
func (em *CreateStringArray) Exec() (int, error) {
|
||||
intArray := em.execNode.GetInPortDefaultStringArrayValue(0)
|
||||
if intArray == nil {
|
||||
return -1, fmt.Errorf("CreateIntArray inParam 0 not found")
|
||||
}
|
||||
|
||||
outPort := em.GetOutPort(0)
|
||||
if outPort == nil {
|
||||
return -1, fmt.Errorf("GetArrayInt outParam 0 not found")
|
||||
}
|
||||
|
||||
for _, v := range intArray {
|
||||
outPort.AppendArrayValStr(v)
|
||||
}
|
||||
|
||||
return -1, nil
|
||||
}
|
||||
|
||||
// AppendIntegerToArray 数组追加整型
|
||||
type AppendIntegerToArray struct {
|
||||
BaseExecNode
|
||||
}
|
||||
|
||||
func (em *AppendIntegerToArray) GetName() string {
|
||||
return "AppendIntegerToArray"
|
||||
}
|
||||
|
||||
func (em *AppendIntegerToArray) Exec() (int, error) {
|
||||
inPortArray := em.GetInPort(0)
|
||||
if inPortArray == nil {
|
||||
return -1, fmt.Errorf("AppendIntegerToArray inParam 0 not found")
|
||||
}
|
||||
|
||||
inPortVal := em.GetInPort(1)
|
||||
if inPortVal == nil {
|
||||
return -1, fmt.Errorf("AppendIntegerToArray inParam 1 not found")
|
||||
}
|
||||
|
||||
outPort := em.GetOutPort(0)
|
||||
if outPort == nil {
|
||||
return -1, fmt.Errorf("AppendIntegerToArray outParam 0 not found")
|
||||
}
|
||||
|
||||
intArray, ok := inPortArray.GetArray()
|
||||
if !ok {
|
||||
return -1, fmt.Errorf("AppendIntegerToArray inParam 0 error")
|
||||
}
|
||||
|
||||
intVal, ok := inPortVal.GetInt()
|
||||
if !ok {
|
||||
return -1, fmt.Errorf("AppendIntegerToArray inParam 1 error")
|
||||
}
|
||||
|
||||
for i := range intArray {
|
||||
outPort.AppendArrayValInt(intArray[i].IntVal)
|
||||
}
|
||||
outPort.AppendArrayValInt(intVal)
|
||||
return -1, nil
|
||||
}
|
||||
|
||||
// AppendStringToArray 数组追加字符串
|
||||
type AppendStringToArray struct {
|
||||
BaseExecNode
|
||||
}
|
||||
|
||||
func (em *AppendStringToArray) GetName() string {
|
||||
return "AppendStringToArray"
|
||||
}
|
||||
|
||||
func (em *AppendStringToArray) Exec() (int, error) {
|
||||
inPortArray := em.GetInPort(0)
|
||||
if inPortArray == nil {
|
||||
return -1, fmt.Errorf("AppendStringToArray inParam 0 not found")
|
||||
}
|
||||
|
||||
inPortVal := em.GetInPort(1)
|
||||
if inPortVal == nil {
|
||||
return -1, fmt.Errorf("AppendStringToArray inParam 1 not found")
|
||||
}
|
||||
|
||||
outPort := em.GetOutPort(0)
|
||||
if outPort == nil {
|
||||
return -1, fmt.Errorf("AppendStringToArray outParam 0 not found")
|
||||
}
|
||||
|
||||
intArray, ok := inPortArray.GetArray()
|
||||
if !ok {
|
||||
return -1, fmt.Errorf("AppendStringToArray inParam 0 error")
|
||||
}
|
||||
|
||||
for i := range intArray {
|
||||
outPort.AppendArrayValStr(intArray[i].StrVal)
|
||||
}
|
||||
|
||||
return -1, nil
|
||||
}
|
||||
|
||||
// CreateTimer 创建定时器
|
||||
type CreateTimer struct {
|
||||
BaseExecNode
|
||||
}
|
||||
|
||||
func (em *CreateTimer) GetName() string {
|
||||
return "CreateTimer"
|
||||
}
|
||||
|
||||
func (em *CreateTimer) Exec() (int, error) {
|
||||
delay, ok := em.GetInPortInt(0)
|
||||
if !ok {
|
||||
return -1, fmt.Errorf("CreateTimer inParam 0 error")
|
||||
}
|
||||
|
||||
array, ok := em.GetInPortArray(1)
|
||||
if !ok {
|
||||
return -1, fmt.Errorf("CreateTimer inParam 1 error")
|
||||
}
|
||||
|
||||
var timerId uint64
|
||||
graphID := em.gr.graphID
|
||||
em.gr.IBlueprintModule.SafeAfterFunc(&timerId, time.Duration(delay)*time.Millisecond, nil, func(timerId uint64, additionData interface{}) {
|
||||
err := em.gr.IBlueprintModule.TriggerEvent(graphID, EntranceID_Timer, array)
|
||||
if err != nil {
|
||||
log.Warnf("CreateTimer SafeAfterFunc error timerId:%d err:%v", timerId, err)
|
||||
}
|
||||
|
||||
em.gr.IBlueprintModule.CancelTimerId(graphID, &timerId)
|
||||
})
|
||||
|
||||
em.gr.mapTimerID[timerId] = struct{}{}
|
||||
|
||||
outPort := em.GetOutPort(1)
|
||||
if outPort == nil {
|
||||
return -1, fmt.Errorf("CreateTimer outParam 1 not found")
|
||||
}
|
||||
|
||||
outPort.SetInt(int64(timerId))
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
// CloseTimer 关闭定时器
|
||||
type CloseTimer struct {
|
||||
BaseExecNode
|
||||
}
|
||||
|
||||
func (em *CloseTimer) GetName() string {
|
||||
return "CloseTimer"
|
||||
}
|
||||
|
||||
func (em *CloseTimer) Exec() (int, error) {
|
||||
timerID, ok := em.GetInPortInt(1)
|
||||
if !ok {
|
||||
return -1, fmt.Errorf("CreateTimer inParam 1 error")
|
||||
}
|
||||
|
||||
id := uint64(timerID)
|
||||
ok = em.gr.IBlueprintModule.CancelTimerId(em.gr.graphID, &id)
|
||||
if !ok {
|
||||
log.Warnf("CloseTimer CancelTimerId:%d", id)
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
25
util/blueprint/typedef.go
Normal file
25
util/blueprint/typedef.go
Normal file
@@ -0,0 +1,25 @@
|
||||
package blueprint
|
||||
|
||||
type ArrayElement struct {
|
||||
IntVal int64
|
||||
StrVal string
|
||||
}
|
||||
|
||||
type PortExec struct {
|
||||
}
|
||||
type ArrayData struct {
|
||||
IntVal int64
|
||||
StrVal string
|
||||
}
|
||||
|
||||
type Port_Exec = PortExec
|
||||
type Port_Int = int64
|
||||
type Port_Float = float64
|
||||
type Port_Str = string
|
||||
type Port_Bool = bool
|
||||
|
||||
type Port_Array []ArrayData
|
||||
|
||||
type iPortType interface {
|
||||
Port_Exec | Port_Int | Port_Float | Port_Str | Port_Bool | Port_Array
|
||||
}
|
||||
70
util/blueprint/variables.go
Normal file
70
util/blueprint/variables.go
Normal file
@@ -0,0 +1,70 @@
|
||||
package blueprint
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const GetVariables = "GetVar"
|
||||
const SetVariables = "SetVar"
|
||||
const globalVariablesPrefix = "g_"
|
||||
|
||||
type GetVariablesNode struct {
|
||||
BaseExecNode
|
||||
nodeName string
|
||||
}
|
||||
|
||||
type SetVariablesNode struct {
|
||||
BaseExecNode
|
||||
nodeName string
|
||||
}
|
||||
|
||||
func (g *GetVariablesNode) GetName() string {
|
||||
return g.nodeName
|
||||
}
|
||||
|
||||
func (g *GetVariablesNode) Exec() (int, error) {
|
||||
var port IPort
|
||||
if strings.HasPrefix(g.GetVariableName(), globalVariablesPrefix) {
|
||||
port = g.gr.globalVariables[g.GetVariableName()]
|
||||
} else {
|
||||
port = g.gr.variables[g.GetVariableName()]
|
||||
}
|
||||
|
||||
if port == nil {
|
||||
return -1, fmt.Errorf("variable %s not found,node name %s", g.GetVariableName(), g.nodeName)
|
||||
}
|
||||
|
||||
if !g.SetOutPort(0, port) {
|
||||
return -1, fmt.Errorf("set out port failed,node name %s", g.nodeName)
|
||||
}
|
||||
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
|
||||
|
||||
func (g *SetVariablesNode) GetName() string {
|
||||
return g.nodeName
|
||||
}
|
||||
|
||||
func (g *SetVariablesNode) Exec() (int, error) {
|
||||
port := g.GetInPort(1)
|
||||
if port == nil {
|
||||
return -1, fmt.Errorf("get in port failed,node name %s", g.nodeName)
|
||||
}
|
||||
|
||||
varPort := port.Clone()
|
||||
if strings.HasPrefix(g.GetVariableName(), globalVariablesPrefix) {
|
||||
g.gr.globalVariables[g.GetVariableName()] = varPort
|
||||
} else {
|
||||
g.gr.variables[g.GetVariableName()] = varPort
|
||||
}
|
||||
|
||||
if !g.SetOutPort(1, varPort) {
|
||||
return -1, fmt.Errorf("set out port failed,node name %s", g.nodeName)
|
||||
}
|
||||
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
@@ -32,10 +32,10 @@ func Abs[NumType typ.Signed | typ.Float](Num NumType) NumType {
|
||||
func AddSafe[NumType typ.Number](number1 NumType, number2 NumType) (NumType, bool) {
|
||||
ret := number1 + number2
|
||||
if number2 > 0 && ret < number1 {
|
||||
log.Stack("Calculation overflow", log.Any("number1", number1), log.Any("number2", number2))
|
||||
log.SStackError("Calculation overflow", log.Any("number1", number1), log.Any("number2", number2))
|
||||
return ret, false
|
||||
} else if number2 < 0 && ret > number1 {
|
||||
log.Stack("Calculation overflow", log.Any("number1", number1), log.Any("number2", number2))
|
||||
log.SStackError("Calculation overflow", log.Any("number1", number1), log.Any("number2", number2))
|
||||
return ret, false
|
||||
}
|
||||
|
||||
@@ -45,10 +45,10 @@ func AddSafe[NumType typ.Number](number1 NumType, number2 NumType) (NumType, boo
|
||||
func SubSafe[NumType typ.Number](number1 NumType, number2 NumType) (NumType, bool) {
|
||||
ret := number1 - number2
|
||||
if number2 > 0 && ret > number1 {
|
||||
log.Stack("Calculation overflow", log.Any("number1", number1), log.Any("number2", number2))
|
||||
log.SStackError("Calculation overflow", log.Any("number1", number1), log.Any("number2", number2))
|
||||
return ret, false
|
||||
} else if number2 < 0 && ret < number1 {
|
||||
log.Stack("Calculation overflow", log.Any("number1", number1), log.Any("number2", number2))
|
||||
log.SStackError("Calculation overflow", log.Any("number1", number1), log.Any("number2", number2))
|
||||
return ret, false
|
||||
}
|
||||
|
||||
@@ -65,7 +65,7 @@ func MulSafe[NumType typ.Number](number1 NumType, number2 NumType) (NumType, boo
|
||||
return ret, true
|
||||
}
|
||||
|
||||
log.Stack("Calculation overflow", log.Any("number1", number1), log.Any("number2", number2))
|
||||
log.SStackError("Calculation overflow", log.Any("number1", number1), log.Any("number2", number2))
|
||||
return ret, true
|
||||
}
|
||||
|
||||
|
||||
41
util/stack/stack.go
Normal file
41
util/stack/stack.go
Normal file
@@ -0,0 +1,41 @@
|
||||
package stack
|
||||
|
||||
// Stack 是一个通用类型的栈结构
|
||||
type Stack[T any] struct {
|
||||
items []*T
|
||||
}
|
||||
|
||||
// Push 将元素压入栈顶
|
||||
func (s *Stack[T]) Push(item *T) {
|
||||
s.items = append(s.items, item)
|
||||
}
|
||||
|
||||
// Pop 弹出并返回栈顶元素,如果栈为空则返回错误
|
||||
func (s *Stack[T]) Pop() *T {
|
||||
if len(s.items) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
index := len(s.items) - 1
|
||||
item := s.items[index]
|
||||
s.items = s.items[:index]
|
||||
return item
|
||||
}
|
||||
|
||||
// Peek 返回栈顶元素但不移除,如果栈为空则返回错误
|
||||
func (s *Stack[T]) Peek() *T {
|
||||
if len(s.items) == 0 {
|
||||
return nil
|
||||
}
|
||||
return s.items[len(s.items)-1]
|
||||
}
|
||||
|
||||
// IsEmpty 检查栈是否为空
|
||||
func (s *Stack[T]) IsEmpty() bool {
|
||||
return len(s.items) == 0
|
||||
}
|
||||
|
||||
// Size 返回栈中元素的数量
|
||||
func (s *Stack[T]) Size() int {
|
||||
return len(s.items)
|
||||
}
|
||||
@@ -6,21 +6,21 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
func SetupTimer(timer ITimer) ITimer{
|
||||
func SetupTimer(timer ITimer) ITimer {
|
||||
if timer.IsOpen() == true {
|
||||
return nil
|
||||
}
|
||||
|
||||
timer.Open(true)
|
||||
timerHeapLock.Lock() // 使用锁规避竞争条件
|
||||
heap.Push(&timerHeap,timer)
|
||||
heap.Push(&timerHeap, timer)
|
||||
timerHeapLock.Unlock()
|
||||
return timer
|
||||
}
|
||||
|
||||
func NewTimer(d time.Duration) *Timer{
|
||||
c := make(chan ITimer,1)
|
||||
timer := newTimer(d,c,nil,"")
|
||||
func NewTimer(d time.Duration) *Timer {
|
||||
c := make(chan ITimer, 1)
|
||||
timer := newTimer(d, c, nil, "")
|
||||
SetupTimer(timer)
|
||||
|
||||
return timer
|
||||
@@ -43,7 +43,7 @@ func (h *_TimerHeap) Less(i, j int) bool {
|
||||
}
|
||||
|
||||
func (h *_TimerHeap) Swap(i, j int) {
|
||||
h.timers[i],h.timers[j] = h.timers[j],h.timers[i]
|
||||
h.timers[i], h.timers[j] = h.timers[j], h.timers[i]
|
||||
}
|
||||
|
||||
func (h *_TimerHeap) Push(x interface{}) {
|
||||
@@ -62,16 +62,16 @@ var (
|
||||
timeOffset time.Duration
|
||||
)
|
||||
|
||||
func StartTimer(minTimerInterval time.Duration,maxTimerNum int){
|
||||
timerHeap.timers = make([]ITimer,0,maxTimerNum)
|
||||
func StartTimer(minTimerInterval time.Duration, maxTimerNum int) {
|
||||
timerHeap.timers = make([]ITimer, 0, maxTimerNum)
|
||||
heap.Init(&timerHeap) // 初始化定时器heap
|
||||
|
||||
go tickRoutine(minTimerInterval)
|
||||
go tickRoutine(minTimerInterval)
|
||||
}
|
||||
|
||||
func tickRoutine(minTimerInterval time.Duration){
|
||||
func tickRoutine(minTimerInterval time.Duration) {
|
||||
var bContinue bool
|
||||
for{
|
||||
for {
|
||||
bContinue = tick()
|
||||
if bContinue == false {
|
||||
time.Sleep(minTimerInterval)
|
||||
@@ -79,7 +79,7 @@ func tickRoutine(minTimerInterval time.Duration){
|
||||
}
|
||||
}
|
||||
|
||||
func tick() bool{
|
||||
func tick() bool {
|
||||
now := Now()
|
||||
timerHeapLock.Lock()
|
||||
if timerHeap.Len() <= 0 { // 没有任何定时器,立刻返回
|
||||
@@ -100,7 +100,7 @@ func tick() bool{
|
||||
return true
|
||||
}
|
||||
|
||||
func Now() time.Time{
|
||||
func Now() time.Time {
|
||||
if timeOffset == 0 {
|
||||
return time.Now()
|
||||
}
|
||||
@@ -108,4 +108,12 @@ func Now() time.Time{
|
||||
return time.Now().Add(timeOffset)
|
||||
}
|
||||
|
||||
// SetTimeOffset 设置时间偏移量
|
||||
func SetTimeOffset(offset time.Duration) {
|
||||
timeOffset = offset
|
||||
}
|
||||
|
||||
// AddTimeOffset 添加时间偏移量
|
||||
func AddTimeOffset(addOffset time.Duration) {
|
||||
timeOffset += addOffset
|
||||
}
|
||||
@@ -124,10 +124,7 @@ func (t *Timer) IsOpen() bool {
|
||||
func (t *Timer) Do() {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
buf := make([]byte, 4096)
|
||||
l := runtime.Stack(buf, false)
|
||||
errString := fmt.Sprint(r)
|
||||
log.Dump(string(buf[:l]), log.String("error", errString))
|
||||
log.StackError(fmt.Sprint(r))
|
||||
}
|
||||
}()
|
||||
|
||||
@@ -210,10 +207,7 @@ func (c *Cron) Reset() {
|
||||
func (c *Cron) Do() {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
buf := make([]byte, 4096)
|
||||
l := runtime.Stack(buf, false)
|
||||
errString := fmt.Sprint(r)
|
||||
log.Dump(string(buf[:l]), log.String("error", errString))
|
||||
log.StackError(fmt.Sprint(r))
|
||||
}
|
||||
}()
|
||||
|
||||
@@ -266,10 +260,7 @@ func (c *Cron) UnRef() {
|
||||
func (c *Ticker) Do() {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
buf := make([]byte, 4096)
|
||||
l := runtime.Stack(buf, false)
|
||||
errString := fmt.Sprint(r)
|
||||
log.Dump(string(buf[:l]), log.String("error", errString))
|
||||
log.StackError(fmt.Sprint(r))
|
||||
}
|
||||
}()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user