From 39116c44024b0827cbbffd3194ce116571a4f061 Mon Sep 17 00:00:00 2001 From: duanhf2012 <6549168@qq.com> Date: Fri, 20 Sep 2024 17:25:08 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81=E8=A7=84?= =?UTF-8?q?=E8=8C=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cluster/cluster.go | 151 +++++---- cluster/etcddiscovery.go | 299 +++++++++--------- cluster/nodettl.go | 36 +-- cluster/origindiscovery.go | 150 +++++---- cluster/parsecfg.go | 169 +++++----- concurrent/concurrent.go | 10 +- concurrent/dispatch.go | 26 +- concurrent/worker.go | 10 +- console/command.go | 66 ++-- event/event.go | 93 +++--- event/eventtype.go | 33 +- log/buffer.go | 71 ++--- log/handler.go | 64 ++-- log/log.go | 269 ++++++++-------- network/http_server.go | 17 +- network/processor/jsonprocessor.go | 82 ++--- network/processor/pbprocessor.go | 29 +- network/processor/pbrawprocessor.go | 68 ++-- network/processor/processor.go | 22 +- network/tcp_conn.go | 35 +- network/tcp_msg.go | 11 +- network/tcp_server.go | 69 ++-- network/ws_conn.go | 6 +- node/node.go | 74 ++--- profiler/profiler.go | 114 +++---- rpc/callset.go | 100 +++--- rpc/client.go | 120 ++++--- rpc/compressor.go | 31 +- rpc/lclient.go | 50 ++- rpc/lserver.go | 92 +++--- rpc/natsclient.go | 65 ++-- rpc/natsserver.go | 84 ++--- rpc/pbprocessor.go | 53 ++-- rpc/rclient.go | 42 ++- rpc/rpchandler.go | 181 +++++------ rpc/server.go | 69 ++-- service/module.go | 10 +- service/service.go | 242 +++++++------- sysmodule/frametimer/FrameTimerModule.go | 2 +- sysmodule/ginmodule/GinModule.go | 78 ++--- .../httpclientmodule/httpclientpoolmodule.go | 14 +- sysmodule/mongodbmodule/mongodbmodule.go | 8 +- sysmodule/mysqlmodule/mysqlmodule.go | 103 +++--- sysmodule/redismodule/redismodule.go | 54 ++-- sysservice/httpservice/httpservice.go | 25 +- .../messagequeueservice/CustomerSubscriber.go | 18 +- sysservice/messagequeueservice/MemoryQueue.go | 10 +- .../messagequeueservice/MongoPersist.go | 55 ++-- sysservice/messagequeueservice/Subscriber.go | 2 +- sysservice/messagequeueservice/TopicRoom.go | 10 +- sysservice/rankservice/MongodbPersist.go | 176 +++++------ sysservice/rankservice/RankInterface.go | 11 +- sysservice/rankservice/RankService.go | 2 +- sysservice/rankservice/RankSkip.go | 6 +- sysservice/tcpservice/tcpservice.go | 146 +++++---- sysservice/wsservice/wsservice.go | 81 ++--- util/aesencrypt/aes_encrypt.go | 32 +- util/algorithms/BitwiseOperation.go | 2 +- util/bytespool/bytespool.go | 2 +- util/coroutine/coroutine.go | 16 +- util/srand/slice.go | 5 +- util/sync/MemPool.go | 24 +- util/timer/timer.go | 19 +- 63 files changed, 1971 insertions(+), 2043 deletions(-) diff --git a/cluster/cluster.go b/cluster/cluster.go index 2f5bf9f..f2ecd8c 100644 --- a/cluster/cluster.go +++ b/cluster/cluster.go @@ -1,15 +1,15 @@ package cluster import ( + "errors" "fmt" + "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" - "github.com/duanhf2012/origin/v2/event" - "errors" - "reflect" ) var configDir = "./config/" @@ -24,22 +24,22 @@ const ( ) type DiscoveryService struct { - MasterNodeId string //要筛选的主结点Id,如果不配置或者配置成0,表示针对所有的主结点 - NetworkName string //如果是etcd,指定要筛选的网络名中的服务,不配置,表示所有的网络 - ServiceList []string //只发现的服务列表 + MasterNodeId string //要筛选的主结点Id,如果不配置或者配置成0,表示针对所有的主结点 + NetworkName string //如果是etcd,指定要筛选的网络名中的服务,不配置,表示所有的网络 + ServiceList []string //只发现的服务列表 } type NodeInfo struct { NodeId string Private bool ListenAddr string - MaxRpcParamLen uint32 //最大Rpc参数长度 - CompressBytesLen int //超过字节进行压缩的长度 - ServiceList []string //所有的有序服务列表 - PublicServiceList []string //对外公开的服务列表 + MaxRpcParamLen uint32 //最大Rpc参数长度 + CompressBytesLen int //超过字节进行压缩的长度 + ServiceList []string //所有的有序服务列表 + PublicServiceList []string //对外公开的服务列表 DiscoveryService []DiscoveryService //筛选发现的服务,如果不配置,不进行筛选 status NodeStatus - Retire bool + Retire bool NetworkName string } @@ -52,22 +52,22 @@ type NodeRpcInfo struct { var cluster Cluster type Cluster struct { - localNodeInfo NodeInfo //本结点配置信息 + localNodeInfo NodeInfo //本结点配置信息 discoveryInfo DiscoveryInfo //服务发现配置 - rpcMode RpcMode - globalCfg interface{} //全局配置 + rpcMode RpcMode + globalCfg interface{} //全局配置 localServiceCfg map[string]interface{} //map[serviceName]配置数据* serviceDiscovery IServiceDiscovery //服务发现接口 - locker sync.RWMutex //结点与服务关系保护锁 - mapRpc map[string]*NodeRpcInfo //nodeId - mapServiceNode map[string]map[string]struct{} //map[serviceName]map[NodeId] + locker sync.RWMutex //结点与服务关系保护锁 + mapRpc map[string]*NodeRpcInfo //nodeId + mapServiceNode map[string]map[string]struct{} //map[serviceName]map[NodeId] mapTemplateServiceNode map[string]map[string]struct{} //map[templateServiceName]map[serviceName]nodeId - callSet rpc.CallSet - rpcNats rpc.RpcNats + callSet rpc.CallSet + rpcNats rpc.RpcNats rpcServer rpc.IServer rpcEventLocker sync.RWMutex //Rpc事件监听保护锁 @@ -86,7 +86,7 @@ func SetServiceDiscovery(serviceDiscovery IServiceDiscovery) { cluster.serviceDiscovery = serviceDiscovery } -func (cls *Cluster) Start() error{ +func (cls *Cluster) Start() error { return cls.rpcServer.Start() } @@ -97,7 +97,7 @@ func (cls *Cluster) Stop() { func (cls *Cluster) DiscardNode(nodeId string) { cls.locker.Lock() nodeInfo, ok := cls.mapRpc[nodeId] - bDel := (ok == true) && nodeInfo.nodeInfo.status == Discard + bDel := (ok == true) && nodeInfo.nodeInfo.status == Discard cls.locker.Unlock() if bDel { @@ -113,22 +113,22 @@ func (cls *Cluster) DelNode(nodeId string) { cls.locker.Lock() defer cls.locker.Unlock() - rpc, ok := cls.mapRpc[nodeId] + nodeRpc, ok := cls.mapRpc[nodeId] if ok == false { return } - cls.TriggerDiscoveryEvent(false,nodeId,rpc.nodeInfo.ServiceList) - for _, serviceName := range rpc.nodeInfo.ServiceList { + cls.TriggerDiscoveryEvent(false, nodeId, nodeRpc.nodeInfo.ServiceList) + for _, serviceName := range nodeRpc.nodeInfo.ServiceList { cls.delServiceNode(serviceName, nodeId) } delete(cls.mapRpc, nodeId) if ok == true { - rpc.client.Close(false) + nodeRpc.client.Close(false) } - log.Info("remove node ",log.String("NodeId", rpc.nodeInfo.NodeId),log.String("ListenAddr", rpc.nodeInfo.ListenAddr)) + log.Info("remove node ", log.String("NodeId", nodeRpc.nodeInfo.NodeId), log.String("ListenAddr", nodeRpc.nodeInfo.ListenAddr)) } func (cls *Cluster) serviceDiscoveryDelNode(nodeId string) { @@ -141,16 +141,16 @@ func (cls *Cluster) delServiceNode(serviceName string, nodeId string) { } //处理模板服务 - splitServiceName := strings.Split(serviceName,":") + splitServiceName := strings.Split(serviceName, ":") if len(splitServiceName) == 2 { serviceName = splitServiceName[0] templateServiceName := splitServiceName[1] mapService := cls.mapTemplateServiceNode[templateServiceName] - delete(mapService,serviceName) + delete(mapService, serviceName) if len(cls.mapTemplateServiceNode[templateServiceName]) == 0 { - delete(cls.mapTemplateServiceNode,templateServiceName) + delete(cls.mapTemplateServiceNode, templateServiceName) } } @@ -178,7 +178,7 @@ func (cls *Cluster) serviceDiscoverySetNodeInfo(nodeInfo *NodeInfo) { } } - cluster.TriggerDiscoveryEvent(true,nodeInfo.NodeId,nodeInfo.PublicServiceList) + cluster.TriggerDiscoveryEvent(true, nodeInfo.NodeId, nodeInfo.PublicServiceList) //再重新组装 mapDuplicate := map[string]interface{}{} //预防重复数据 for _, serviceName := range nodeInfo.PublicServiceList { @@ -190,13 +190,13 @@ func (cls *Cluster) serviceDiscoverySetNodeInfo(nodeInfo *NodeInfo) { mapDuplicate[serviceName] = nil //如果是模板服务,则记录模板关系 - splitServiceName := strings.Split(serviceName,":") + splitServiceName := strings.Split(serviceName, ":") if len(splitServiceName) == 2 { serviceName = splitServiceName[0] templateServiceName := splitServiceName[1] //记录模板 if _, ok = cls.mapTemplateServiceNode[templateServiceName]; ok == false { - cls.mapTemplateServiceNode[templateServiceName]=map[string]struct{}{} + cls.mapTemplateServiceNode[templateServiceName] = map[string]struct{}{} } cls.mapTemplateServiceNode[templateServiceName][serviceName] = struct{}{} } @@ -208,7 +208,7 @@ func (cls *Cluster) serviceDiscoverySetNodeInfo(nodeInfo *NodeInfo) { } if lastNodeInfo != nil { - log.Info("Discovery nodeId",log.String("NodeId", nodeInfo.NodeId),log.Any("services:", nodeInfo.PublicServiceList),log.Bool("Retire",nodeInfo.Retire)) + log.Info("Discovery nodeId", log.String("NodeId", nodeInfo.NodeId), log.Any("services:", nodeInfo.PublicServiceList), log.Bool("Retire", nodeInfo.Retire)) lastNodeInfo.nodeInfo = *nodeInfo return } @@ -218,19 +218,18 @@ func (cls *Cluster) serviceDiscoverySetNodeInfo(nodeInfo *NodeInfo) { rpcInfo.nodeInfo = *nodeInfo if cls.IsNatsMode() { - rpcInfo.client = cls.rpcNats.NewNatsClient(nodeInfo.NodeId, cls.GetLocalNodeInfo().NodeId,&cls.callSet,cls.NotifyAllService) - }else{ - rpcInfo.client =rpc.NewRClient(nodeInfo.NodeId, nodeInfo.ListenAddr, nodeInfo.MaxRpcParamLen,cls.localNodeInfo.CompressBytesLen,&cls.callSet,cls.NotifyAllService) + rpcInfo.client = cls.rpcNats.NewNatsClient(nodeInfo.NodeId, cls.GetLocalNodeInfo().NodeId, &cls.callSet, cls.NotifyAllService) + } else { + rpcInfo.client = rpc.NewRClient(nodeInfo.NodeId, nodeInfo.ListenAddr, nodeInfo.MaxRpcParamLen, cls.localNodeInfo.CompressBytesLen, &cls.callSet, cls.NotifyAllService) } 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)) - }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)) + 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)) + } 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)) } } - func (cls *Cluster) Init(localNodeId string, setupServiceFun SetupServiceFun) error { //1.初始化配置 err := cls.InitCfg(localNodeId) @@ -240,18 +239,18 @@ func (cls *Cluster) Init(localNodeId string, setupServiceFun SetupServiceFun) er cls.callSet.Init() if cls.IsNatsMode() { - cls.rpcNats.Init(cls.rpcMode.Nats.NatsUrl,cls.rpcMode.Nats.NoRandomize,cls.GetLocalNodeInfo().NodeId,cls.localNodeInfo.CompressBytesLen,cls,cluster.NotifyAllService) + cls.rpcNats.Init(cls.rpcMode.Nats.NatsUrl, cls.rpcMode.Nats.NoRandomize, cls.GetLocalNodeInfo().NodeId, cls.localNodeInfo.CompressBytesLen, cls, cluster.NotifyAllService) cls.rpcServer = &cls.rpcNats - }else{ + } else { s := &rpc.Server{} - s.Init(cls.localNodeInfo.ListenAddr,cls.localNodeInfo.MaxRpcParamLen,cls.localNodeInfo.CompressBytesLen,cls) + s.Init(cls.localNodeInfo.ListenAddr, cls.localNodeInfo.MaxRpcParamLen, cls.localNodeInfo.CompressBytesLen, cls) cls.rpcServer = s } //2.安装服务发现结点 err = cls.setupDiscovery(localNodeId, setupServiceFun) if err != nil { - log.Error("setupDiscovery fail",log.ErrorAttr("err",err)) + log.Error("setupDiscovery fail", log.ErrorAttr("err", err)) return err } service.RegRpcEventFun = cls.RegRpcEvent @@ -274,16 +273,16 @@ func (cls *Cluster) FindRpcHandler(serviceName string) rpc.IRpcHandler { return pService.GetRpcHandler() } -func (cls *Cluster) getRpcClient(nodeId string) (*rpc.Client,bool) { +func (cls *Cluster) getRpcClient(nodeId string) (*rpc.Client, bool) { c, ok := cls.mapRpc[nodeId] if ok == false { - return nil,false + return nil, false } - return c.client,c.nodeInfo.Retire + return c.client, c.nodeInfo.Retire } -func (cls *Cluster) GetRpcClient(nodeId string) (*rpc.Client,bool) { +func (cls *Cluster) GetRpcClient(nodeId string) (*rpc.Client, bool) { cls.locker.RLock() defer cls.locker.RUnlock() return cls.getRpcClient(nodeId) @@ -293,9 +292,9 @@ func GetNodeIdByTemplateService(templateServiceName string, rpcClientList []*rpc return GetCluster().GetNodeIdByTemplateService(templateServiceName, rpcClientList, filterRetire) } -func GetRpcClient(nodeId string, serviceMethod string,filterRetire bool, clientList []*rpc.Client) (error, []*rpc.Client) { +func GetRpcClient(nodeId string, serviceMethod string, filterRetire bool, clientList []*rpc.Client) (error, []*rpc.Client) { if nodeId != rpc.NodeIdNull { - pClient,retire := GetCluster().GetRpcClient(nodeId) + pClient, retire := GetCluster().GetRpcClient(nodeId) if pClient == nil { return fmt.Errorf("cannot find nodeid %s", nodeId), nil } @@ -305,7 +304,7 @@ func GetRpcClient(nodeId string, serviceMethod string,filterRetire bool, clientL return fmt.Errorf("cannot find nodeid %s", nodeId), nil } - clientList = append(clientList,pClient) + clientList = append(clientList, pClient) return nil, clientList } @@ -315,7 +314,6 @@ func GetRpcClient(nodeId string, serviceMethod string,filterRetire bool, clientL } serviceName := serviceMethod[:findIndex] - //1.找到对应的rpcNodeid return GetCluster().GetNodeIdByService(serviceName, clientList, filterRetire) } @@ -324,7 +322,7 @@ func GetRpcServer() rpc.IServer { } func (cls *Cluster) IsNodeConnected(nodeId string) bool { - pClient,_ := cls.GetRpcClient(nodeId) + pClient, _ := cls.GetRpcClient(nodeId) return pClient != nil && pClient.IsConnected() } @@ -332,18 +330,18 @@ func (cls *Cluster) IsNodeRetire(nodeId string) bool { cls.locker.RLock() defer cls.locker.RUnlock() - _,retire :=cls.getRpcClient(nodeId) + _, retire := cls.getRpcClient(nodeId) return retire } -func (cls *Cluster) NotifyAllService(event event.IEvent){ +func (cls *Cluster) NotifyAllService(event event.IEvent) { cls.rpcEventLocker.Lock() defer cls.rpcEventLocker.Unlock() - for serviceName, _ := range cls.mapServiceListenRpcEvent { + for serviceName := range cls.mapServiceListenRpcEvent { ser := service.GetService(serviceName) if ser == nil { - log.Error("cannot find service name "+serviceName) + log.Error("cannot find service name " + serviceName) continue } @@ -403,7 +401,7 @@ func GetNodeByServiceName(serviceName string) map[string]struct{} { } mapNodeId := map[string]struct{}{} - for nodeId,_ := range mapNode { + for nodeId := range mapNode { mapNodeId[nodeId] = struct{}{} } @@ -416,14 +414,14 @@ func GetNodeByTemplateServiceName(templateServiceName string) map[string]string 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 { return nil } - for nodeId,_ := range mapNode { + for nodeId := range mapNode { mapNodeId[serviceName] = nodeId } } @@ -435,55 +433,54 @@ func (cls *Cluster) GetGlobalCfg() interface{} { return cls.globalCfg } - -func (cls *Cluster) ParseGlobalCfg(cfg interface{}) error{ - if cls.globalCfg == nil { +func (cls *Cluster) ParseGlobalCfg(cfg interface{}) error { + if cls.globalCfg == nil { return errors.New("no service configuration found") } rv := reflect.ValueOf(cls.globalCfg) - if rv.Kind() == reflect.Ptr && rv.IsNil() { + if rv.Kind() == reflect.Ptr && rv.IsNil() { return errors.New("no service configuration found") } - bytes,err := json.Marshal(cls.globalCfg) + bytes, err := json.Marshal(cls.globalCfg) if err != nil { return err } - return json.Unmarshal(bytes,cfg) + return json.Unmarshal(bytes, cfg) } -func (cls *Cluster) GetNodeInfo(nodeId string) (NodeInfo,bool) { +func (cls *Cluster) GetNodeInfo(nodeId string) (NodeInfo, bool) { cls.locker.RLock() defer cls.locker.RUnlock() - nodeInfo,ok:= cls.mapRpc[nodeId] + nodeInfo, ok := cls.mapRpc[nodeId] if ok == false || nodeInfo == nil { - return NodeInfo{},false + return NodeInfo{}, false } - return nodeInfo.nodeInfo,true + return nodeInfo.nodeInfo, true } -func (cls *Cluster) CanDiscoveryService(fromMasterNodeId string,serviceName string) bool{ +func (cls *Cluster) CanDiscoveryService(fromMasterNodeId string, serviceName string) bool { canDiscovery := true - splitServiceName := strings.Split(serviceName,":") + splitServiceName := strings.Split(serviceName, ":") if len(splitServiceName) == 2 { serviceName = splitServiceName[0] } - for i:=0;i 0 { - 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)) - return errors.New("cannot grant lease") - } - } - - if lg != nil { - 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)) - } - return errors.New("cannot put record") - } - - _,err = client.Put(context.Background(), path.Join(originDir,etcdServiceRecord.RecordKey),etcdServiceRecord.RecordInfo) + if etcdServiceRecord.TTLSecond > 0 { + ctx, _ := context.WithTimeout(context.Background(), time.Second*3) + lg, err = client.Grant(ctx, etcdServiceRecord.TTLSecond) if err != nil { - log.Error("etcd record fail,cannot put record",log.ErrorAttr("err",err)) - return errors.New("cannot put record") + log.Error("etcd record fail,cannot grant lease", log.ErrorAttr("err", err)) + return errors.New("cannot grant lease") } + } - return nil + if lg != nil { + 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)) + } + 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)) + return errors.New("cannot put record") + } + + return nil } diff --git a/cluster/nodettl.go b/cluster/nodettl.go index e22cb22..dbffc07 100644 --- a/cluster/nodettl.go +++ b/cluster/nodettl.go @@ -1,53 +1,53 @@ package cluster import ( - "time" "container/list" + "time" ) type nodeTTL struct { - nodeId string + nodeId string refreshTime time.Time } type nodeSetTTL struct { - l *list.List + l *list.List mapElement map[string]*list.Element - ttl time.Duration + ttl time.Duration } func (ns *nodeSetTTL) init(ttl time.Duration) { ns.ttl = ttl - ns.mapElement = make(map[string]*list.Element,32) + ns.mapElement = make(map[string]*list.Element, 32) ns.l = list.New() } func (ns *nodeSetTTL) removeNode(nodeId string) { - ele,ok:=ns.mapElement[nodeId] + ele, ok := ns.mapElement[nodeId] if ok == false { return } ns.l.Remove(ele) - delete(ns.mapElement,nodeId) + delete(ns.mapElement, nodeId) } -func (ns *nodeSetTTL) addAndRefreshNode(nodeId string){ - ele,ok:=ns.mapElement[nodeId] +func (ns *nodeSetTTL) addAndRefreshNode(nodeId string) { + ele, ok := ns.mapElement[nodeId] if ok == false { ele = ns.l.PushBack(nodeId) - ele.Value = &nodeTTL{nodeId,time.Now()} + ele.Value = &nodeTTL{nodeId, time.Now()} ns.mapElement[nodeId] = ele return } - ele.Value.(*nodeTTL).refreshTime = time.Now() + ele.Value.(*nodeTTL).refreshTime = time.Now() ns.l.MoveToBack(ele) } -func (ns *nodeSetTTL) checkTTL(cb func(nodeIdList []string)){ - nodeIdList := []string{} - for{ +func (ns *nodeSetTTL) checkTTL(cb func(nodeIdList []string)) { + var nodeIdList []string + for { f := ns.l.Front() if f == nil { break @@ -55,17 +55,17 @@ func (ns *nodeSetTTL) checkTTL(cb func(nodeIdList []string)){ nt := f.Value.(*nodeTTL) if time.Now().Sub(nt.refreshTime) > ns.ttl { - nodeIdList = append(nodeIdList,nt.nodeId) - }else{ + nodeIdList = append(nodeIdList, nt.nodeId) + } else { break } //删除结点 ns.l.Remove(f) - delete(ns.mapElement,nt.nodeId) + delete(ns.mapElement, nt.nodeId) } - if len(nodeIdList) >0 { + if len(nodeIdList) > 0 { cb(nodeIdList) } } diff --git a/cluster/origindiscovery.go b/cluster/origindiscovery.go index 5f50471..1e626d6 100644 --- a/cluster/origindiscovery.go +++ b/cluster/origindiscovery.go @@ -14,10 +14,9 @@ const OriginDiscoveryMasterName = "DiscoveryMaster" const OriginDiscoveryClientName = "DiscoveryClient" const RegServiceDiscover = OriginDiscoveryMasterName + ".RPC_RegServiceDiscover" const SubServiceDiscover = OriginDiscoveryClientName + ".RPC_SubServiceDiscover" -const AddSubServiceDiscover = OriginDiscoveryMasterName + ".RPC_AddSubServiceDiscover" -const NodeRetireRpcMethod = OriginDiscoveryMasterName+".RPC_NodeRetire" -const RpcPingMethod = OriginDiscoveryMasterName+".RPC_Ping" -const UnRegServiceDiscover = OriginDiscoveryMasterName+".RPC_UnRegServiceDiscover" +const NodeRetireRpcMethod = OriginDiscoveryMasterName + ".RPC_NodeRetire" +const RpcPingMethod = OriginDiscoveryMasterName + ".RPC_Ping" +const UnRegServiceDiscover = OriginDiscoveryMasterName + ".RPC_UnRegServiceDiscover" type OriginDiscoveryMaster struct { service.Service @@ -31,12 +30,12 @@ type OriginDiscoveryMaster struct { type OriginDiscoveryClient struct { service.Service - funDelNode FunDelNode - funSetNode FunSetNode - localNodeId string + funDelNode FunDelNode + funSetNode FunSetNode + localNodeId string mapDiscovery map[string]map[string][]string //map[masterNodeId]map[nodeId]struct{} - bRetire bool + bRetire bool isRegisterOk bool } @@ -58,14 +57,14 @@ func (ds *OriginDiscoveryMaster) isRegNode(nodeId string) bool { } func (ds *OriginDiscoveryMaster) updateNodeInfo(nInfo *rpc.NodeInfo) { - if _,ok:= ds.mapNodeInfo[nInfo.NodeId];ok == false { + if _, ok := ds.mapNodeInfo[nInfo.NodeId]; ok == false { return } nodeInfo := proto.Clone(nInfo).(*rpc.NodeInfo) - for i:=0;i 1 { + if atomic.AddInt32(&c.open, 1) > 1 { panic("repeated calls to OpenConcurrent are not allowed!") } - + c.tasks = make(chan task, maxTaskChannelNum) c.cbChannel = make(chan func(error), maxTaskChannelNum) @@ -66,7 +68,7 @@ func (c *Concurrent) AsyncDoByQueue(queueId int64, fn func() bool, cb func(err e } if queueId != 0 { - queueId = queueId % maxTaskQueueSessionId+1 + queueId = queueId%maxTaskQueueSessionId + 1 } select { diff --git a/concurrent/dispatch.go b/concurrent/dispatch.go index 787713b..3153a0f 100644 --- a/concurrent/dispatch.go +++ b/concurrent/dispatch.go @@ -8,12 +8,13 @@ import ( "fmt" "runtime" + "context" "github.com/duanhf2012/origin/v2/log" "github.com/duanhf2012/origin/v2/util/queue" - "context" ) var idleTimeout = int64(2 * time.Second) + const maxTaskQueueSessionId = 10000 type dispatch struct { @@ -33,7 +34,7 @@ type dispatch struct { waitDispatch sync.WaitGroup cancelContext context.Context - cancel context.CancelFunc + cancel context.CancelFunc } func (d *dispatch) open(minGoroutineNum int32, maxGoroutineNum int32, tasks chan task, cbChannel chan func(error)) { @@ -44,7 +45,7 @@ func (d *dispatch) open(minGoroutineNum int32, maxGoroutineNum int32, tasks chan d.workerQueue = make(chan task) d.cbChannel = cbChannel d.queueIdChannel = make(chan int64, cap(tasks)) - d.cancelContext,d.cancel = context.WithCancel(context.Background()) + d.cancelContext, d.cancel = context.WithCancel(context.Background()) d.waitDispatch.Add(1) go d.run() } @@ -56,7 +57,7 @@ func (d *dispatch) run() { for { select { case queueId := <-d.queueIdChannel: - d.processqueueEvent(queueId) + d.processQueueEvent(queueId) default: select { case t, ok := <-d.tasks: @@ -65,13 +66,13 @@ func (d *dispatch) run() { } d.processTask(&t) case queueId := <-d.queueIdChannel: - d.processqueueEvent(queueId) + d.processQueueEvent(queueId) case <-timeout.C: d.processTimer() - case <- d.cancelContext.Done(): - atomic.StoreInt64(&idleTimeout,int64(time.Millisecond * 5)) + case <-d.cancelContext.Done(): + atomic.StoreInt64(&idleTimeout, int64(time.Millisecond*5)) timeout.Reset(time.Duration(atomic.LoadInt64(&idleTimeout))) - for i:=int32(0);i= 0 { - if b.bs[i] == '\n' { - b.bs = b.bs[:i] +func (buff *Buffer) TrimNewline() { + if i := len(buff.bs) - 1; i >= 0 { + if buff.bs[i] == '\n' { + buff.bs = buff.bs[:i] } } } - diff --git a/log/handler.go b/log/handler.go index b510003..b9ceec5 100644 --- a/log/handler.go +++ b/log/handler.go @@ -11,6 +11,7 @@ import ( ) const defaultSkip = 7 + type IOriginHandler interface { slog.Handler Lock() @@ -21,9 +22,9 @@ type IOriginHandler interface { type BaseHandler struct { addSource bool - w io.Writer - locker sync.Mutex - skip int + w io.Writer + locker sync.Mutex + skip int } type OriginTextHandler struct { @@ -36,15 +37,15 @@ type OriginJsonHandler struct { *slog.JSONHandler } -func (bh *BaseHandler) SetSkip(skip int){ +func (bh *BaseHandler) SetSkip(skip int) { bh.skip = skip } -func (bh *BaseHandler) GetSkip() int{ +func (bh *BaseHandler) GetSkip() int { return bh.skip } -func getStrLevel(level slog.Level) string{ +func getStrLevel(level slog.Level) string { switch level { case LevelTrace: return "Trace" @@ -71,39 +72,39 @@ 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 { + } 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 { + } 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{ +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{ + textHandler.TextHandler = slog.NewTextHandler(w, &slog.HandlerOptions{ AddSource: addSource, Level: level, ReplaceAttr: replaceAttr, }) - textHandler.skip = defaultSkip + textHandler.skip = defaultSkip return &textHandler } -func (oh *OriginTextHandler) Handle(context context.Context, record slog.Record) error{ - oh.Fill(context,&record) +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{ + + if record.Level == LevelStack || record.Level == LevelFatal { err := oh.TextHandler.Handle(context, record) oh.logStack(&record) return err - }else if record.Level == LevelDump { + } else if record.Level == LevelDump { strDump := record.Message record.Message = "dump info" err := oh.TextHandler.Handle(context, record) @@ -111,27 +112,26 @@ func (oh *OriginTextHandler) Handle(context context.Context, record slog.Record) return err } - return oh.TextHandler.Handle(context, record) } -func (b *BaseHandler) logStack(record *slog.Record){ - b.w.Write(debug.Stack()) +func (bh *BaseHandler) logStack(record *slog.Record) { + bh.w.Write(debug.Stack()) } -func (b *BaseHandler) Lock(){ - b.locker.Lock() +func (bh *BaseHandler) Lock() { + bh.locker.Lock() } -func (b *BaseHandler) UnLock(){ - b.locker.Unlock() +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{ +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{ + jsonHandler.JSONHandler = slog.NewJSONHandler(w, &slog.HandlerOptions{ AddSource: addSource, Level: level, ReplaceAttr: replaceAttr, @@ -141,10 +141,10 @@ func NewOriginJsonHandler(level slog.Level,w io.Writer,addSource bool,replaceAtt 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()) +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() @@ -152,10 +152,10 @@ func (oh *OriginJsonHandler) Handle(context context.Context, record slog.Record) return oh.JSONHandler.Handle(context, record) } -func (b *BaseHandler) Fill(context context.Context, record *slog.Record) { - if b.addSource { +func (bh *BaseHandler) Fill(_ context.Context, record *slog.Record) { + if bh.addSource { var pcs [1]uintptr - runtime.Callers(b.skip, pcs[:]) + runtime.Callers(bh.skip, pcs[:]) record.PC = pcs[0] } } diff --git a/log/log.go b/log/log.go index 6fae961..c158f69 100644 --- a/log/log.go +++ b/log/log.go @@ -4,7 +4,6 @@ import ( "context" "fmt" "github.com/duanhf2012/origin/v2/util/bytespool" - jsoniter "github.com/json-iterator/go" "io" "log/slog" "os" @@ -16,28 +15,26 @@ import ( "time" ) -var json = jsoniter.ConfigCompatibleWithStandardLibrary var OpenConsole bool var LogSize int64 var LogChannelCap int var LogPath string -var LogLevel slog.Level = LevelTrace +var LogLevel = LevelTrace - -var gLogger, _ = NewTextLogger(LevelDebug, "", "",true,LogChannelCap) +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) + 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 { @@ -50,13 +47,13 @@ type ILogger interface { Dump(msg string, args ...any) Fatal(msg string, args ...any) - DoSPrintf(level slog.Level,a []interface{}) - FormatHeader(buf *Buffer,level slog.Level,calldepth int) + DoSPrintf(level slog.Level, a []interface{}) + FormatHeader(buf *Buffer, level slog.Level, callDepth int) Close() } type Logger struct { - Slogger *slog.Logger + SLogger *slog.Logger ioWriter IoWriter @@ -64,17 +61,17 @@ type Logger struct { } type IoWriter struct { - outFile io.Writer // destination for output - writeBytes int64 + outFile io.Writer // destination for output + writeBytes int64 logChannel chan []byte - wg sync.WaitGroup - closeSig chan struct{} + wg sync.WaitGroup + closeSig chan struct{} lockWrite sync.Mutex - filePath string - fileprefix string - fileDay int + filePath string + filePrefix string + fileDay int fileCreateTime int64 //second } @@ -94,7 +91,7 @@ func (iw *IoWriter) close() error { } iw.wg.Wait() - if iw.outFile!= nil { + if iw.outFile != nil { err := iw.outFile.(io.Closer).Close() iw.outFile = nil return err @@ -103,21 +100,21 @@ func (iw *IoWriter) close() error { return nil } -func (iw *IoWriter) writeFile(p []byte) (n int, err error){ - //swich log file - iw.swichFile() +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) + n, err = iw.outFile.Write(p) if n > 0 { - atomic.AddInt64(&iw.writeBytes,int64(n)) + atomic.AddInt64(&iw.writeBytes, int64(n)) } } - return 0,nil + return 0, nil } -func (iw *IoWriter) Write(p []byte) (n int, err error){ +func (iw *IoWriter) Write(p []byte) (n int, err error) { iw.lockWrite.Lock() defer iw.lockWrite.Unlock() @@ -127,26 +124,26 @@ func (iw *IoWriter) Write(p []byte) (n int, err error){ copyBuff := memPool.MakeBytes(len(p)) if copyBuff == nil { - return 0,fmt.Errorf("MakeByteSlice failed") + return 0, fmt.Errorf("MakeByteSlice failed") } - copy(copyBuff,p) + copy(copyBuff, p) iw.logChannel <- copyBuff return } -func (iw *IoWriter) writeIo(p []byte) (n int, err error){ - n,err = iw.writeFile(p) +func (iw *IoWriter) writeIo(p []byte) (n int, err error) { + n, err = iw.writeFile(p) if OpenConsole { - n,err = os.Stdout.Write(p) + n, err = os.Stdout.Write(p) } return } -func (iw *IoWriter) setLogChannel(logChannelNum int) (err error){ +func (iw *IoWriter) setLogChannel(logChannelNum int) (err error) { iw.lockWrite.Lock() defer iw.lockWrite.Unlock() iw.close() @@ -157,9 +154,9 @@ func (iw *IoWriter) setLogChannel(logChannelNum int) (err error){ //copy iw.logChannel var logInfo []byte - logChannel := make(chan []byte,logChannelNum) - for i := 0; i < logChannelNum&&i 0 { - logs := <-iw.logChannel - iw.writeIo(logs) - memPool.ReleaseBytes(logs) + logs := <-iw.logChannel + iw.writeIo(logs) + memPool.ReleaseBytes(logs) } } @@ -200,11 +197,11 @@ func (iw *IoWriter) isFull() bool { return atomic.LoadInt64(&iw.writeBytes) >= LogSize } -func (logger *Logger) setLogChannel(logChannel int) (err error){ +func (logger *Logger) setLogChannel(logChannel int) (err error) { return logger.ioWriter.setLogChannel(logChannel) } -func (iw *IoWriter) swichFile() error{ +func (iw *IoWriter) switchFile() error { now := time.Now() if iw.fileCreateTime == now.Unix() { return nil @@ -217,7 +214,7 @@ func (iw *IoWriter) swichFile() error{ if iw.filePath != "" { var err error fileName := fmt.Sprintf("%s%d%02d%02d_%02d_%02d_%02d.log", - iw.fileprefix, + iw.filePrefix, now.Year(), now.Month(), now.Day(), @@ -227,92 +224,92 @@ func (iw *IoWriter) swichFile() error{ filePath := path.Join(iw.filePath, fileName) - iw.outFile,err = os.Create(filePath) + iw.outFile, err = os.Create(filePath) if err != nil { return err } iw.fileDay = now.Day() iw.fileCreateTime = now.Unix() - atomic.StoreInt64(&iw.writeBytes,0) + atomic.StoreInt64(&iw.writeBytes, 0) } return nil } -func GetDefaultHandler() IOriginHandler{ - return gLogger.(*Logger).Slogger.Handler().(IOriginHandler) +func GetDefaultHandler() IOriginHandler { + return gLogger.(*Logger).SLogger.Handler().(IOriginHandler) } -func NewTextLogger(level slog.Level,pathName string,filePrefix string,addSource bool,logChannelCap int) (ILogger,error){ +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.ioWriter.filePrefix = filePrefix - logger.Slogger = slog.New(NewOriginTextHandler(level,&logger.ioWriter,addSource,defaultReplaceAttr)) + logger.SLogger = slog.New(NewOriginTextHandler(level, &logger.ioWriter, addSource, defaultReplaceAttr)) logger.setLogChannel(logChannelCap) - err := logger.ioWriter.swichFile() + err := logger.ioWriter.switchFile() if err != nil { - return nil,err + return nil, err } - return &logger,nil + return &logger, nil } -func NewJsonLogger(level slog.Level,pathName string,filePrefix string,addSource bool,logChannelCap int) (ILogger,error){ +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.ioWriter.filePrefix = filePrefix - logger.Slogger = slog.New(NewOriginJsonHandler(level,&logger.ioWriter,true,defaultReplaceAttr)) + logger.SLogger = slog.New(NewOriginJsonHandler(level, &logger.ioWriter, true, defaultReplaceAttr)) logger.setLogChannel(logChannelCap) - err := logger.ioWriter.swichFile() + err := logger.ioWriter.switchFile() if err != nil { - return nil,err + return nil, err } - return &logger,nil + return &logger, nil } -// It's dangerous to call the method on logging +// 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...) + logger.SLogger.Log(context.Background(), LevelTrace, msg, args...) } func (logger *Logger) Debug(msg string, args ...any) { - logger.Slogger.Log(context.Background(),LevelDebug,msg,args...) + logger.SLogger.Log(context.Background(), LevelDebug, msg, args...) } func (logger *Logger) Info(msg string, args ...any) { - logger.Slogger.Log(context.Background(),LevelInfo,msg,args...) + logger.SLogger.Log(context.Background(), LevelInfo, msg, args...) } func (logger *Logger) Warning(msg string, args ...any) { - logger.Slogger.Log(context.Background(),LevelWarning,msg,args...) + logger.SLogger.Log(context.Background(), LevelWarning, msg, args...) } func (logger *Logger) Error(msg string, args ...any) { - logger.Slogger.Log(context.Background(),LevelError,msg,args...) + logger.SLogger.Log(context.Background(), LevelError, msg, args...) } func (logger *Logger) Stack(msg string, args ...any) { - logger.Slogger.Log(context.Background(),LevelStack,msg,args...) + logger.SLogger.Log(context.Background(), LevelStack, msg, args...) } func (logger *Logger) Dump(msg string, args ...any) { - logger.Slogger.Log(context.Background(),LevelDump,msg,args...) + logger.SLogger.Log(context.Background(), LevelDump, msg, args...) } func (logger *Logger) Fatal(msg string, args ...any) { - logger.Slogger.Log(context.Background(),LevelFatal,msg,args...) + logger.SLogger.Log(context.Background(), LevelFatal, msg, args...) os.Exit(1) } -// It's non-thread-safe +// SetLogger It's non-thread-safe func SetLogger(logger ILogger) { if logger != nil && isSetLogger == false { gLogger = logger @@ -320,189 +317,189 @@ func SetLogger(logger ILogger) { } } -func GetLogger() ILogger{ +func GetLogger() ILogger { return gLogger } -func Trace(msg string, args ...any){ +func Trace(msg string, args ...any) { gLogger.Trace(msg, args...) } -func Debug(msg string, args ...any){ - gLogger.Debug(msg,args...) +func Debug(msg string, args ...any) { + gLogger.Debug(msg, args...) } -func Info(msg string, args ...any){ - gLogger.Info(msg,args...) +func Info(msg string, args ...any) { + gLogger.Info(msg, args...) } -func Warning(msg string, args ...any){ - gLogger.Warning(msg,args...) +func Warning(msg string, args ...any) { + gLogger.Warning(msg, args...) } -func Error(msg string, args ...any){ - gLogger.Error(msg,args...) +func Error(msg string, args ...any) { + gLogger.Error(msg, args...) } -func Stack(msg string, args ...any){ - gLogger.Stack(msg,args...) +func Stack(msg string, args ...any) { + gLogger.Stack(msg, args...) } -func Dump(dump string, args ...any){ - gLogger.Dump(dump,args...) +func Dump(dump string, args ...any) { + gLogger.Dump(dump, args...) } -func Fatal(msg string, args ...any){ - gLogger.Fatal(msg,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, slog.StringValue("nil")} +func ErrorAttr(key string, value error) slog.Attr { + if value == nil { + return slog.Attr{Key: key, Value: slog.StringValue("nil")} } - return slog.Attr{key, slog.StringValue(value.Error())} + return slog.Attr{Key: key, Value: slog.StringValue(value.Error())} } func String(key, value string) slog.Attr { - return slog.Attr{key, slog.StringValue(value)} + return slog.Attr{Key: key, Value: slog.StringValue(value)} } func Int(key string, value int) slog.Attr { - return slog.Attr{key, slog.Int64Value(int64(value))} + return slog.Attr{Key: key, Value: slog.Int64Value(int64(value))} } func Int64(key string, value int64) slog.Attr { - return slog.Attr{key, slog.Int64Value(value)} + return slog.Attr{Key: key, Value: slog.Int64Value(value)} } func Int32(key string, value int32) slog.Attr { - return slog.Attr{key, slog.Int64Value(int64(value))} + return slog.Attr{Key: key, Value: slog.Int64Value(int64(value))} } func Int16(key string, value int16) slog.Attr { - return slog.Attr{key, slog.Int64Value(int64(value))} + return slog.Attr{Key: key, Value: slog.Int64Value(int64(value))} } func Int8(key string, value int8) slog.Attr { - return slog.Attr{key, slog.Int64Value(int64(value))} + return slog.Attr{Key: key, Value: slog.Int64Value(int64(value))} } func Uint(key string, value uint) slog.Attr { - return slog.Attr{key, slog.Uint64Value(uint64(value))} + return slog.Attr{Key: key, Value: slog.Uint64Value(uint64(value))} } func Uint64(key string, v uint64) slog.Attr { - return slog.Attr{key, slog.Uint64Value(v)} + return slog.Attr{Key: key, Value: slog.Uint64Value(v)} } func Uint32(key string, value uint32) slog.Attr { - return slog.Attr{key, slog.Uint64Value(uint64(value))} + return slog.Attr{Key: key, Value: slog.Uint64Value(uint64(value))} } func Uint16(key string, value uint16) slog.Attr { - return slog.Attr{key, slog.Uint64Value(uint64(value))} + return slog.Attr{Key: key, Value: slog.Uint64Value(uint64(value))} } func Uint8(key string, value uint8) slog.Attr { - return slog.Attr{key, slog.Uint64Value(uint64(value))} + return slog.Attr{Key: key, Value: slog.Uint64Value(uint64(value))} } func Float64(key string, v float64) slog.Attr { - return slog.Attr{key, slog.Float64Value(v)} + return slog.Attr{Key: key, Value: slog.Float64Value(v)} } func Bool(key string, v bool) slog.Attr { - return slog.Attr{key, slog.BoolValue(v)} + return slog.Attr{Key: key, Value: slog.BoolValue(v)} } func Time(key string, v time.Time) slog.Attr { - return slog.Attr{key, slog.TimeValue(v)} + return slog.Attr{Key: key, Value: slog.TimeValue(v)} } func Duration(key string, v time.Duration) slog.Attr { - return slog.Attr{key, slog.DurationValue(v)} + return slog.Attr{Key: key, Value: slog.DurationValue(v)} } func Any(key string, value any) slog.Attr { - return slog.Attr{key, slog.AnyValue(value)} + 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) DoSPrintf(level slog.Level, a []interface{}) { + if logger.SLogger.Enabled(context.Background(), level) == false { return } - logger.Slogger.Handler().(IOriginHandler).Lock() - defer logger.Slogger.Handler().(IOriginHandler).UnLock() + logger.SLogger.Handler().(IOriginHandler).Lock() + defer logger.SLogger.Handler().(IOriginHandler).UnLock() logger.sBuff.Reset() - logger.FormatHeader(&logger.sBuff,level,3) + logger.FormatHeader(&logger.sBuff, level, 3) - for _,s := range a { + for _, s := range a { logger.sBuff.AppendString(slog.AnyValue(s).String()) } logger.sBuff.AppendString("\"\n") - logger.ioWriter.Write([]byte(logger.sBuff.Bytes())) + logger.ioWriter.Write(logger.sBuff.Bytes()) } - func (logger *Logger) STrace(a ...interface{}) { - logger.DoSPrintf(LevelTrace,a) +func (logger *Logger) STrace(a ...interface{}) { + logger.DoSPrintf(LevelTrace, a) } -func (logger *Logger) SDebug(a ...interface{}) { - logger.DoSPrintf(LevelDebug,a) +func (logger *Logger) SDebug(a ...interface{}) { + logger.DoSPrintf(LevelDebug, a) } -func (logger *Logger) SInfo(a ...interface{}) { - logger.DoSPrintf(LevelInfo,a) +func (logger *Logger) SInfo(a ...interface{}) { + logger.DoSPrintf(LevelInfo, a) } -func (logger *Logger) SWarning(a ...interface{}) { - logger.DoSPrintf(LevelWarning,a) +func (logger *Logger) SWarning(a ...interface{}) { + logger.DoSPrintf(LevelWarning, a) } -func (logger *Logger) SError(a ...interface{}) { - logger.DoSPrintf(LevelError,a) +func (logger *Logger) SError(a ...interface{}) { + logger.DoSPrintf(LevelError, a) } func STrace(a ...interface{}) { - gLogger.DoSPrintf(LevelTrace,a) + gLogger.DoSPrintf(LevelTrace, a) } func SDebug(a ...interface{}) { - gLogger.DoSPrintf(LevelDebug,a) + gLogger.DoSPrintf(LevelDebug, a) } func SInfo(a ...interface{}) { - gLogger.DoSPrintf(LevelInfo,a) + gLogger.DoSPrintf(LevelInfo, a) } func SWarning(a ...interface{}) { - gLogger.DoSPrintf(LevelWarning,a) + gLogger.DoSPrintf(LevelWarning, a) } func SError(a ...interface{}) { - gLogger.DoSPrintf(LevelError,a) + gLogger.DoSPrintf(LevelError, a) } -func (logger *Logger) FormatHeader(buf *Buffer,level slog.Level,calldepth int) { +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) + _, file, line, ok = runtime.Caller(callDepth) if !ok { file = "???" line = 0 @@ -520,4 +517,4 @@ func (logger *Logger) FormatHeader(buf *Buffer,level slog.Level,calldepth int) { buf.AppendByte(':') buf.AppendInt(int64(line)) buf.AppendString(" msg=\"") -} \ No newline at end of file +} diff --git a/network/http_server.go b/network/http_server.go index 407542c..7e3d57b 100644 --- a/network/http_server.go +++ b/network/http_server.go @@ -8,20 +8,20 @@ import ( "time" ) -var DefaultMaxHeaderBytes int = 1<<20 +var DefaultMaxHeaderBytes = 1 << 20 type CAFile struct { CertFile string - Keyfile string + Keyfile string } type HttpServer struct { - listenAddr string + listenAddr string readTimeout time.Duration writeTimeout time.Duration - handler http.Handler - caFileList []CAFile + handler http.Handler + caFileList []CAFile httpServer *http.Server } @@ -39,7 +39,7 @@ func (slf *HttpServer) Start() { func (slf *HttpServer) startListen() error { if slf.httpServer != nil { - return errors.New("Duplicate start not allowed") + return errors.New("duplicate start not allowed") } var tlsCaList []tls.Certificate @@ -47,7 +47,7 @@ func (slf *HttpServer) startListen() error { for _, caFile := range slf.caFileList { cer, err := tls.LoadX509KeyPair(caFile.CertFile, caFile.Keyfile) if err != nil { - log.Fatal("Load CA file is fail",log.String("error",err.Error()),log.String("certFile",caFile.CertFile),log.String("keyFile",caFile.Keyfile)) + log.Fatal("Load CA file is fail", log.String("error", err.Error()), log.String("certFile", caFile.CertFile), log.String("keyFile", caFile.Keyfile)) return err } tlsCaList = append(tlsCaList, cer) @@ -74,14 +74,13 @@ func (slf *HttpServer) startListen() error { } if err != nil { - log.Fatal("Listen failure",log.String("error",err.Error()),log.String("addr:",slf.listenAddr)) + log.Fatal("Listen failure", log.String("error", err.Error()), log.String("addr:", slf.listenAddr)) return err } return nil } - func (slf *HttpServer) SetCAFile(caFile []CAFile) { slf.caFileList = caFile } diff --git a/network/processor/jsonprocessor.go b/network/processor/jsonprocessor.go index fd12ef4..b9fafec 100644 --- a/network/processor/jsonprocessor.go +++ b/network/processor/jsonprocessor.go @@ -13,29 +13,29 @@ type MessageJsonInfo struct { msgHandler MessageJsonHandler } -type MessageJsonHandler func(clientId string,msg interface{}) +type MessageJsonHandler func(clientId string, msg interface{}) type ConnectJsonHandler func(clientId string) -type UnknownMessageJsonHandler func(clientId string,msg []byte) +type UnknownMessageJsonHandler func(clientId string, msg []byte) type JsonProcessor struct { - mapMsg map[uint16]MessageJsonInfo + mapMsg map[uint16]MessageJsonInfo LittleEndian bool unknownMessageHandler UnknownMessageJsonHandler - connectHandler ConnectJsonHandler - disconnectHandler ConnectJsonHandler - bytespool.IBytesMempool + connectHandler ConnectJsonHandler + disconnectHandler ConnectJsonHandler + bytespool.IBytesMemPool } type JsonPackInfo struct { - typ uint16 - msg interface{} + typ uint16 + msg interface{} rawMsg []byte } func NewJsonProcessor() *JsonProcessor { - processor := &JsonProcessor{mapMsg:map[uint16]MessageJsonInfo{}} - processor.IBytesMempool = bytespool.NewMemAreaPool() + processor := &JsonProcessor{mapMsg: map[uint16]MessageJsonInfo{}} + processor.IBytesMemPool = bytespool.NewMemAreaPool() return processor } @@ -44,99 +44,101 @@ func (jsonProcessor *JsonProcessor) SetByteOrder(littleEndian bool) { jsonProcessor.LittleEndian = littleEndian } -// must goroutine safe -func (jsonProcessor *JsonProcessor ) MsgRoute(clientId string,msg interface{},recyclerReaderBytes func(data []byte)) error{ +// MsgRoute must goroutine safe +func (jsonProcessor *JsonProcessor) MsgRoute(clientId string, msg interface{}, recyclerReaderBytes func(data []byte)) error { pPackInfo := msg.(*JsonPackInfo) defer recyclerReaderBytes(pPackInfo.rawMsg) - v,ok := jsonProcessor.mapMsg[pPackInfo.typ] + v, ok := jsonProcessor.mapMsg[pPackInfo.typ] if ok == false { - return fmt.Errorf("cannot find msgtype %d is register!",pPackInfo.typ) + return fmt.Errorf("cannot find msgtype %d is register", pPackInfo.typ) } - v.msgHandler(clientId,pPackInfo.msg) + v.msgHandler(clientId, pPackInfo.msg) return nil } -func (jsonProcessor *JsonProcessor) Unmarshal(clientId string,data []byte) (interface{}, error) { - typeStruct := struct {Type int `json:"typ"`}{} +func (jsonProcessor *JsonProcessor) Unmarshal(clientId string, data []byte) (interface{}, error) { + typeStruct := struct { + Type int `json:"typ"` + }{} err := json.Unmarshal(data, &typeStruct) if err != nil { return nil, err } msgType := uint16(typeStruct.Type) - info,ok := jsonProcessor.mapMsg[msgType] + info, ok := jsonProcessor.mapMsg[msgType] if ok == false { - return nil,fmt.Errorf("Cannot find register %d msgType!",msgType) + return nil, fmt.Errorf("cannot find register %d msgType", msgType) } msgData := reflect.New(info.msgType.Elem()).Interface() err = json.Unmarshal(data, msgData) if err != nil { - return nil,err + return nil, err } - return &JsonPackInfo{typ:msgType,msg:msgData,rawMsg: data},nil + return &JsonPackInfo{typ: msgType, msg: msgData, rawMsg: data}, nil } -func (jsonProcessor *JsonProcessor) Marshal(clientId string,msg interface{}) ([]byte, error) { - rawMsg,err := json.Marshal(msg) +func (jsonProcessor *JsonProcessor) Marshal(clientId string, msg interface{}) ([]byte, error) { + rawMsg, err := json.Marshal(msg) if err != nil { - return nil,err + return nil, err } - return rawMsg,nil + return rawMsg, nil } -func (jsonProcessor *JsonProcessor) Register(msgtype uint16,msg interface{},handle MessageJsonHandler) { +func (jsonProcessor *JsonProcessor) Register(msgType uint16, msg interface{}, handle MessageJsonHandler) { var info MessageJsonInfo info.msgType = reflect.TypeOf(msg) info.msgHandler = handle - jsonProcessor.mapMsg[msgtype] = info + jsonProcessor.mapMsg[msgType] = info } -func (jsonProcessor *JsonProcessor) MakeMsg(msgType uint16,msg interface{}) *JsonPackInfo { - return &JsonPackInfo{typ:msgType,msg:msg} +func (jsonProcessor *JsonProcessor) MakeMsg(msgType uint16, msg interface{}) *JsonPackInfo { + return &JsonPackInfo{typ: msgType, msg: msg} } -func (jsonProcessor *JsonProcessor) MakeRawMsg(msgType uint16,msg []byte) *JsonPackInfo { - return &JsonPackInfo{typ:msgType,rawMsg:msg} +func (jsonProcessor *JsonProcessor) MakeRawMsg(msgType uint16, msg []byte) *JsonPackInfo { + return &JsonPackInfo{typ: msgType, rawMsg: msg} } -func (jsonProcessor *JsonProcessor) UnknownMsgRoute(clientId string,msg interface{},recyclerReaderBytes func(data []byte)){ +func (jsonProcessor *JsonProcessor) UnknownMsgRoute(clientId string, msg interface{}, recyclerReaderBytes func(data []byte)) { defer recyclerReaderBytes(msg.([]byte)) - if jsonProcessor.unknownMessageHandler==nil { - log.Debug("Unknown message",log.String("clientId",clientId)) + if jsonProcessor.unknownMessageHandler == nil { + log.Debug("Unknown message", log.String("clientId", clientId)) return } - jsonProcessor.unknownMessageHandler(clientId,msg.([]byte)) + jsonProcessor.unknownMessageHandler(clientId, msg.([]byte)) } -func (jsonProcessor *JsonProcessor) ConnectedRoute(clientId string){ +func (jsonProcessor *JsonProcessor) ConnectedRoute(clientId string) { if jsonProcessor.connectHandler != nil { jsonProcessor.connectHandler(clientId) } } -func (jsonProcessor *JsonProcessor) DisConnectedRoute(clientId string){ +func (jsonProcessor *JsonProcessor) DisConnectedRoute(clientId string) { if jsonProcessor.disconnectHandler != nil { jsonProcessor.disconnectHandler(clientId) } } -func (jsonProcessor *JsonProcessor) RegisterUnknownMsg(unknownMessageHandler UnknownMessageJsonHandler){ +func (jsonProcessor *JsonProcessor) RegisterUnknownMsg(unknownMessageHandler UnknownMessageJsonHandler) { jsonProcessor.unknownMessageHandler = unknownMessageHandler } -func (jsonProcessor *JsonProcessor) RegisterConnected(connectHandler ConnectJsonHandler){ +func (jsonProcessor *JsonProcessor) RegisterConnected(connectHandler ConnectJsonHandler) { jsonProcessor.connectHandler = connectHandler } -func (jsonProcessor *JsonProcessor) RegisterDisConnected(disconnectHandler ConnectJsonHandler){ +func (jsonProcessor *JsonProcessor) RegisterDisConnected(disconnectHandler ConnectJsonHandler) { jsonProcessor.disconnectHandler = disconnectHandler } diff --git a/network/processor/pbprocessor.go b/network/processor/pbprocessor.go index 8fbfcff..150d4ae 100644 --- a/network/processor/pbprocessor.go +++ b/network/processor/pbprocessor.go @@ -26,7 +26,7 @@ type PBProcessor struct { unknownMessageHandler UnknownMessageHandler connectHandler ConnectHandler disconnectHandler ConnectHandler - bytespool.IBytesMempool + bytespool.IBytesMemPool } type PBPackInfo struct { @@ -37,7 +37,7 @@ type PBPackInfo struct { func NewPBProcessor() *PBProcessor { processor := &PBProcessor{mapMsg: map[uint16]MessageInfo{}} - processor.IBytesMempool = bytespool.NewMemAreaPool() + processor.IBytesMemPool = bytespool.NewMemAreaPool() return processor } @@ -53,26 +53,26 @@ func (slf *PBPackInfo) GetMsg() proto.Message { return slf.msg } -// must goroutine safe -func (pbProcessor *PBProcessor) MsgRoute(clientId string, msg interface{},recyclerReaderBytes func(data []byte)) error { +// MsgRoute must goroutine safe +func (pbProcessor *PBProcessor) MsgRoute(clientId string, msg interface{}, recyclerReaderBytes func(data []byte)) 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) + return fmt.Errorf("cannot find msgtype %d is register", pPackInfo.typ) } v.msgHandler(clientId, pPackInfo.msg) return nil } -// must goroutine safe +// Unmarshal must goroutine safe func (pbProcessor *PBProcessor) Unmarshal(clientId string, data []byte) (interface{}, error) { return pbProcessor.UnmarshalWithOutRelease(clientId, data) } -// unmarshal but not release data +// UnmarshalWithOutRelease not release data func (pbProcessor *PBProcessor) UnmarshalWithOutRelease(clientId string, data []byte) (interface{}, error) { var msgType uint16 if pbProcessor.LittleEndian == true { @@ -83,7 +83,7 @@ func (pbProcessor *PBProcessor) UnmarshalWithOutRelease(clientId string, data [] info, ok := pbProcessor.mapMsg[msgType] if ok == false { - return nil, fmt.Errorf("cannot find register %d msgtype!", msgType) + return nil, fmt.Errorf("cannot find register %d msgtype", msgType) } msg := reflect.New(info.msgType.Elem()).Interface() protoMsg := msg.(proto.Message) @@ -92,10 +92,10 @@ func (pbProcessor *PBProcessor) UnmarshalWithOutRelease(clientId string, data [] return nil, err } - return &PBPackInfo{typ: msgType, msg: protoMsg,rawMsg:data}, nil + return &PBPackInfo{typ: msgType, msg: protoMsg, rawMsg: data}, nil } -// must goroutine safe +// Marshal must goroutine safe func (pbProcessor *PBProcessor) Marshal(clientId string, msg interface{}) ([]byte, error) { pMsg := msg.(*PBPackInfo) @@ -118,12 +118,12 @@ func (pbProcessor *PBProcessor) Marshal(clientId string, msg interface{}) ([]byt return buff, nil } -func (pbProcessor *PBProcessor) Register(msgtype uint16, msg proto.Message, handle MessageHandler) { +func (pbProcessor *PBProcessor) Register(msgType uint16, msg proto.Message, handle MessageHandler) { var info MessageInfo info.msgType = reflect.TypeOf(msg.(proto.Message)) info.msgHandler = handle - pbProcessor.mapMsg[msgtype] = info + pbProcessor.mapMsg[msgType] = info } func (pbProcessor *PBProcessor) MakeMsg(msgType uint16, protoMsg proto.Message) *PBPackInfo { @@ -134,12 +134,11 @@ 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{}, recyclerReaderBytes func(data []byte)) { pbProcessor.unknownMessageHandler(clientId, msg.([]byte)) recyclerReaderBytes(msg.([]byte)) } -// connect event func (pbProcessor *PBProcessor) ConnectedRoute(clientId string) { pbProcessor.connectHandler(clientId) } diff --git a/network/processor/pbrawprocessor.go b/network/processor/pbrawprocessor.go index de74ef6..c75f8c0 100644 --- a/network/processor/pbrawprocessor.go +++ b/network/processor/pbrawprocessor.go @@ -10,21 +10,22 @@ type RawMessageInfo struct { msgHandler RawMessageHandler } -type RawMessageHandler func(clientId string,packType uint16,msg []byte) +type RawMessageHandler func(clientId string, packType uint16, msg []byte) type RawConnectHandler func(clientId string) -type UnknownRawMessageHandler func(clientId string,msg []byte) +type UnknownRawMessageHandler func(clientId string, msg []byte) const RawMsgTypeSize = 2 + type PBRawProcessor struct { - msgHandler RawMessageHandler - LittleEndian bool + msgHandler RawMessageHandler + LittleEndian bool unknownMessageHandler UnknownRawMessageHandler - connectHandler RawConnectHandler - disconnectHandler RawConnectHandler + connectHandler RawConnectHandler + disconnectHandler RawConnectHandler } type PBRawPackInfo struct { - typ uint16 + typ uint16 rawMsg []byte } @@ -37,77 +38,76 @@ func (pbRawProcessor *PBRawProcessor) SetByteOrder(littleEndian bool) { pbRawProcessor.LittleEndian = littleEndian } -// must goroutine safe -func (pbRawProcessor *PBRawProcessor ) MsgRoute(clientId string, msg interface{},recyclerReaderBytes func(data []byte)) error{ +// MsgRoute must goroutine safe +func (pbRawProcessor *PBRawProcessor) MsgRoute(clientId string, msg interface{}, recyclerReaderBytes func(data []byte)) error { pPackInfo := msg.(*PBRawPackInfo) - pbRawProcessor.msgHandler(clientId,pPackInfo.typ,pPackInfo.rawMsg) + pbRawProcessor.msgHandler(clientId, pPackInfo.typ, pPackInfo.rawMsg) recyclerReaderBytes(pPackInfo.rawMsg) - + return nil } -// must goroutine safe -func (pbRawProcessor *PBRawProcessor ) Unmarshal(clientId string,data []byte) (interface{}, error) { +// Unmarshal must goroutine safe +func (pbRawProcessor *PBRawProcessor) Unmarshal(clientId string, data []byte) (interface{}, error) { var msgType uint16 if pbRawProcessor.LittleEndian == true { msgType = binary.LittleEndian.Uint16(data[:2]) - }else{ + } else { msgType = binary.BigEndian.Uint16(data[:2]) } - return &PBRawPackInfo{typ:msgType,rawMsg:data},nil + return &PBRawPackInfo{typ: msgType, rawMsg: data}, nil } -// must goroutine safe -func (pbRawProcessor *PBRawProcessor ) Marshal(clientId string,msg interface{}) ([]byte, error){ +// Marshal must goroutine safe +func (pbRawProcessor *PBRawProcessor) Marshal(clientId string, msg interface{}) ([]byte, error) { pMsg := msg.(*PBRawPackInfo) buff := make([]byte, 2, len(pMsg.rawMsg)+RawMsgTypeSize) if pbRawProcessor.LittleEndian == true { - binary.LittleEndian.PutUint16(buff[:2],pMsg.typ) - }else{ - binary.BigEndian.PutUint16(buff[:2],pMsg.typ) + binary.LittleEndian.PutUint16(buff[:2], pMsg.typ) + } else { + binary.BigEndian.PutUint16(buff[:2], pMsg.typ) } - buff = append(buff,pMsg.rawMsg...) - return buff,nil + buff = append(buff, pMsg.rawMsg...) + return buff, nil } -func (pbRawProcessor *PBRawProcessor) SetRawMsgHandler(handle RawMessageHandler) { +func (pbRawProcessor *PBRawProcessor) SetRawMsgHandler(handle RawMessageHandler) { pbRawProcessor.msgHandler = handle } -func (pbRawProcessor *PBRawProcessor) MakeRawMsg(msgType uint16,msg []byte,pbRawPackInfo *PBRawPackInfo) { +func (pbRawProcessor *PBRawProcessor) MakeRawMsg(msgType uint16, msg []byte, pbRawPackInfo *PBRawPackInfo) { pbRawPackInfo.typ = msgType pbRawPackInfo.rawMsg = msg } -func (pbRawProcessor *PBRawProcessor) UnknownMsgRoute(clientId string,msg interface{},recyclerReaderBytes func(data []byte)){ +func (pbRawProcessor *PBRawProcessor) UnknownMsgRoute(clientId string, msg interface{}, recyclerReaderBytes func(data []byte)) { defer recyclerReaderBytes(msg.([]byte)) if pbRawProcessor.unknownMessageHandler == nil { return } - pbRawProcessor.unknownMessageHandler(clientId,msg.([]byte)) + pbRawProcessor.unknownMessageHandler(clientId, msg.([]byte)) } -// connect event -func (pbRawProcessor *PBRawProcessor) ConnectedRoute(clientId string){ +func (pbRawProcessor *PBRawProcessor) ConnectedRoute(clientId string) { pbRawProcessor.connectHandler(clientId) } -func (pbRawProcessor *PBRawProcessor) DisConnectedRoute(clientId string){ +func (pbRawProcessor *PBRawProcessor) DisConnectedRoute(clientId string) { pbRawProcessor.disconnectHandler(clientId) } -func (pbRawProcessor *PBRawProcessor) SetUnknownMsgHandler(unknownMessageHandler UnknownRawMessageHandler){ +func (pbRawProcessor *PBRawProcessor) SetUnknownMsgHandler(unknownMessageHandler UnknownRawMessageHandler) { pbRawProcessor.unknownMessageHandler = unknownMessageHandler } -func (pbRawProcessor *PBRawProcessor) SetConnectedHandler(connectHandler RawConnectHandler){ +func (pbRawProcessor *PBRawProcessor) SetConnectedHandler(connectHandler RawConnectHandler) { pbRawProcessor.connectHandler = connectHandler } -func (pbRawProcessor *PBRawProcessor) SetDisConnectedHandler(disconnectHandler RawConnectHandler){ +func (pbRawProcessor *PBRawProcessor) SetDisConnectedHandler(disconnectHandler RawConnectHandler) { pbRawProcessor.disconnectHandler = disconnectHandler } @@ -119,7 +119,7 @@ func (slf *PBRawPackInfo) GetMsg() []byte { return slf.rawMsg } -func (slf *PBRawPackInfo) SetPackInfo(typ uint16,rawMsg []byte){ +func (slf *PBRawPackInfo) SetPackInfo(typ uint16, rawMsg []byte) { slf.typ = typ slf.rawMsg = rawMsg -} \ No newline at end of file +} diff --git a/network/processor/processor.go b/network/processor/processor.go index b24530f..9b4e42d 100644 --- a/network/processor/processor.go +++ b/network/processor/processor.go @@ -1,19 +1,18 @@ package processor - type IProcessor interface { - // must goroutine safe - MsgRoute(clientId string,msg interface{},recyclerReaderBytes func(data []byte)) error - //must goroutine safe - UnknownMsgRoute(clientId string,msg interface{},recyclerReaderBytes func(data []byte)) - // connect event + // MsgRoute must goroutine safe + MsgRoute(clientId string, msg interface{}, recyclerReaderBytes func(data []byte)) error + // UnknownMsgRoute must goroutine safe + UnknownMsgRoute(clientId string, msg interface{}, recyclerReaderBytes func(data []byte)) + // ConnectedRoute connect event ConnectedRoute(clientId string) DisConnectedRoute(clientId string) - // must goroutine safe - Unmarshal(clientId string,data []byte) (interface{}, error) - // must goroutine safe - Marshal(clientId string,msg interface{}) ([]byte, error) + // Unmarshal must goroutine safe + Unmarshal(clientId string, data []byte) (interface{}, error) + // Marshal must goroutine safe + Marshal(clientId string, msg interface{}) ([]byte, error) } type IRawProcessor interface { @@ -21,9 +20,8 @@ type IRawProcessor interface { SetByteOrder(littleEndian bool) SetRawMsgHandler(handle RawMessageHandler) - MakeRawMsg(msgType uint16,msg []byte,pbRawPackInfo *PBRawPackInfo) + MakeRawMsg(msgType uint16, msg []byte, pbRawPackInfo *PBRawPackInfo) SetUnknownMsgHandler(unknownMessageHandler UnknownRawMessageHandler) SetConnectedHandler(connectHandler RawConnectHandler) SetDisConnectedHandler(disconnectHandler RawConnectHandler) } - diff --git a/network/tcp_conn.go b/network/tcp_conn.go index f02ce2a..f97fa24 100644 --- a/network/tcp_conn.go +++ b/network/tcp_conn.go @@ -19,16 +19,16 @@ type TCPConn struct { msgParser *MsgParser } -func freeChannel(conn *TCPConn){ - for;len(conn.writeChan)>0;{ - byteBuff := <- conn.writeChan +func freeChannel(conn *TCPConn) { + for len(conn.writeChan) > 0 { + byteBuff := <-conn.writeChan if byteBuff != nil { conn.ReleaseReadMsg(byteBuff) } } } -func newTCPConn(conn net.Conn, pendingWriteNum int, msgParser *MsgParser,writeDeadline time.Duration) *TCPConn { +func newTCPConn(conn net.Conn, pendingWriteNum int, msgParser *MsgParser, writeDeadline time.Duration) *TCPConn { tcpConn := new(TCPConn) tcpConn.conn = conn tcpConn.writeChan = make(chan []byte, pendingWriteNum) @@ -50,7 +50,7 @@ func newTCPConn(conn net.Conn, pendingWriteNum int, msgParser *MsgParser,writeDe conn.Close() tcpConn.Lock() freeChannel(tcpConn) - atomic.StoreInt32(&tcpConn.closeFlag,1) + atomic.StoreInt32(&tcpConn.closeFlag, 1) tcpConn.Unlock() }() @@ -61,9 +61,9 @@ func (tcpConn *TCPConn) doDestroy() { tcpConn.conn.(*net.TCPConn).SetLinger(0) tcpConn.conn.Close() - if atomic.LoadInt32(&tcpConn.closeFlag)==0 { + if atomic.LoadInt32(&tcpConn.closeFlag) == 0 { close(tcpConn.writeChan) - atomic.StoreInt32(&tcpConn.closeFlag,1) + atomic.StoreInt32(&tcpConn.closeFlag, 1) } } @@ -77,19 +77,19 @@ func (tcpConn *TCPConn) Destroy() { func (tcpConn *TCPConn) Close() { tcpConn.Lock() defer tcpConn.Unlock() - if atomic.LoadInt32(&tcpConn.closeFlag)==1 { + if atomic.LoadInt32(&tcpConn.closeFlag) == 1 { return } tcpConn.doWrite(nil) - atomic.StoreInt32(&tcpConn.closeFlag,1) + atomic.StoreInt32(&tcpConn.closeFlag, 1) } func (tcpConn *TCPConn) GetRemoteIp() string { return tcpConn.conn.RemoteAddr().String() } -func (tcpConn *TCPConn) doWrite(b []byte) error{ +func (tcpConn *TCPConn) doWrite(b []byte) error { if len(tcpConn.writeChan) == cap(tcpConn.writeChan) { tcpConn.ReleaseReadMsg(b) log.Error("close conn: channel full") @@ -102,10 +102,10 @@ func (tcpConn *TCPConn) doWrite(b []byte) error{ } // b must not be modified by the others goroutines -func (tcpConn *TCPConn) Write(b []byte) error{ +func (tcpConn *TCPConn) Write(b []byte) error { tcpConn.Lock() defer tcpConn.Unlock() - if atomic.LoadInt32(&tcpConn.closeFlag)==1 || b == nil { + if atomic.LoadInt32(&tcpConn.closeFlag) == 1 || b == nil { tcpConn.ReleaseReadMsg(b) return errors.New("conn is close") } @@ -129,14 +129,14 @@ func (tcpConn *TCPConn) ReadMsg() ([]byte, error) { return tcpConn.msgParser.Read(tcpConn) } -func (tcpConn *TCPConn) GetRecyclerReaderBytes() func (data []byte) { - bytePool := tcpConn.msgParser.IBytesMempool +func (tcpConn *TCPConn) GetRecyclerReaderBytes() func(data []byte) { + bytePool := tcpConn.msgParser.IBytesMemPool return func(data []byte) { bytePool.ReleaseBytes(data) } } -func (tcpConn *TCPConn) ReleaseReadMsg(byteBuff []byte){ +func (tcpConn *TCPConn) ReleaseReadMsg(byteBuff []byte) { tcpConn.msgParser.ReleaseBytes(byteBuff) } @@ -155,15 +155,14 @@ func (tcpConn *TCPConn) WriteRawMsg(args []byte) error { return tcpConn.Write(args) } - func (tcpConn *TCPConn) IsConnected() bool { return atomic.LoadInt32(&tcpConn.closeFlag) == 0 } -func (tcpConn *TCPConn) SetReadDeadline(d time.Duration) { +func (tcpConn *TCPConn) SetReadDeadline(d time.Duration) { tcpConn.conn.SetReadDeadline(time.Now().Add(d)) } -func (tcpConn *TCPConn) SetWriteDeadline(d time.Duration) { +func (tcpConn *TCPConn) SetWriteDeadline(d time.Duration) { tcpConn.conn.SetWriteDeadline(time.Now().Add(d)) } diff --git a/network/tcp_msg.go b/network/tcp_msg.go index 4af4265..12ab697 100644 --- a/network/tcp_msg.go +++ b/network/tcp_msg.go @@ -17,10 +17,9 @@ type MsgParser struct { MaxMsgLen uint32 LittleEndian bool - bytespool.IBytesMempool + bytespool.IBytesMemPool } - func (p *MsgParser) getMaxMsgLen(lenMsgLen int) uint32 { switch p.LenMsgLen { case 1: @@ -34,8 +33,8 @@ func (p *MsgParser) getMaxMsgLen(lenMsgLen int) uint32 { } } -func (p *MsgParser) init(){ - p.IBytesMempool = bytespool.NewMemAreaPool() +func (p *MsgParser) init() { + p.IBytesMemPool = bytespool.NewMemAreaPool() } // goroutine safe @@ -73,7 +72,7 @@ func (p *MsgParser) Read(conn *TCPConn) ([]byte, error) { } else if msgLen < p.MinMsgLen { return nil, errors.New("message too short") } - + // data msgData := p.MakeBytes(int(msgLen)) if _, err := io.ReadFull(conn, msgData[:msgLen]); err != nil { @@ -100,7 +99,7 @@ func (p *MsgParser) Write(conn *TCPConn, args ...[]byte) error { } //msg := make([]byte, uint32(p.lenMsgLen)+msgLen) - msg := p.MakeBytes(p.LenMsgLen+int(msgLen)) + msg := p.MakeBytes(p.LenMsgLen + int(msgLen)) // write len switch p.LenMsgLen { case 1: diff --git a/network/tcp_server.go b/network/tcp_server.go index 227f30e..d068ec5 100644 --- a/network/tcp_server.go +++ b/network/tcp_server.go @@ -1,24 +1,23 @@ package network import ( + "errors" + "fmt" "github.com/duanhf2012/origin/v2/log" "github.com/duanhf2012/origin/v2/util/bytespool" "net" "sync" "time" - "fmt" - "errors" ) -const( - Default_ReadDeadline = time.Second*30 //默认读超时30s - Default_WriteDeadline = time.Second*30 //默认写超时30s - Default_MaxConnNum = 1000000 //默认最大连接数 - Default_PendingWriteNum = 100000 //单连接写消息Channel容量 - Default_LittleEndian = false //默认大小端 - Default_MinMsgLen = 2 //最小消息长度2byte - Default_LenMsgLen = 2 //包头字段长度占用2byte - Default_MaxMsgLen = 65535 //最大消息长度 +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 //最大消息长度 ) type TCPServer struct { @@ -26,19 +25,19 @@ type TCPServer struct { MaxConnNum int PendingWriteNum int ReadDeadline time.Duration - WriteDeadline time.Duration + WriteDeadline time.Duration - NewAgent func(*TCPConn) Agent - ln net.Listener - conns ConnSet - mutexConns sync.Mutex - wgLn sync.WaitGroup - wgConns sync.WaitGroup + NewAgent func(*TCPConn) Agent + ln net.Listener + conns ConnSet + mutexConns sync.Mutex + wgLn sync.WaitGroup + wgConns sync.WaitGroup MsgParser } -func (server *TCPServer) Start() error{ +func (server *TCPServer) Start() error { err := server.init() if err != nil { return err @@ -48,20 +47,20 @@ func (server *TCPServer) Start() error{ return nil } -func (server *TCPServer) init() error{ +func (server *TCPServer) init() error { ln, err := net.Listen("tcp", server.Addr) if err != nil { - return fmt.Errorf("Listen tcp fail,error:%s",err.Error()) + return fmt.Errorf("listen tcp fail,error:%s", err.Error()) } if server.MaxConnNum <= 0 { server.MaxConnNum = Default_MaxConnNum - log.Info("invalid MaxConnNum",log.Int("reset", server.MaxConnNum)) + log.Info("invalid MaxConnNum", log.Int("reset", server.MaxConnNum)) } if server.PendingWriteNum <= 0 { server.PendingWriteNum = Default_PendingWriteNum - log.Info("invalid PendingWriteNum",log.Int("reset", server.PendingWriteNum)) + log.Info("invalid PendingWriteNum", log.Int("reset", server.PendingWriteNum)) } if server.LenMsgLen <= 0 { @@ -77,22 +76,22 @@ func (server *TCPServer) init() error{ maxMsgLen := server.MsgParser.getMaxMsgLen(server.LenMsgLen) if server.MaxMsgLen > maxMsgLen { server.MaxMsgLen = maxMsgLen - log.Info("invalid MaxMsgLen",log.Uint32("reset", maxMsgLen)) + log.Info("invalid MaxMsgLen", log.Uint32("reset", maxMsgLen)) } - + if server.MinMsgLen <= 0 { server.MinMsgLen = Default_MinMsgLen - log.Info("invalid MinMsgLen",log.Uint32("reset", server.MinMsgLen)) + log.Info("invalid MinMsgLen", log.Uint32("reset", server.MinMsgLen)) } if server.WriteDeadline == 0 { server.WriteDeadline = Default_WriteDeadline - log.Info("invalid WriteDeadline",log.Int64("reset",int64(server.WriteDeadline.Seconds()))) + log.Info("invalid WriteDeadline", log.Int64("reset", int64(server.WriteDeadline.Seconds()))) } if server.ReadDeadline == 0 { server.ReadDeadline = Default_ReadDeadline - log.Info("invalid ReadDeadline",log.Int64("reset", int64(server.ReadDeadline.Seconds()))) + log.Info("invalid ReadDeadline", log.Int64("reset", int64(server.ReadDeadline.Seconds()))) } if server.NewAgent == nil { @@ -102,16 +101,16 @@ func (server *TCPServer) init() error{ server.ln = ln server.conns = make(ConnSet) server.MsgParser.init() - + return nil } -func (server *TCPServer) SetNetMempool(mempool bytespool.IBytesMempool){ - server.IBytesMempool = mempool +func (server *TCPServer) SetNetMemPool(memPool bytespool.IBytesMemPool) { + server.IBytesMemPool = memPool } -func (server *TCPServer) GetNetMempool() bytespool.IBytesMempool { - return server.IBytesMempool +func (server *TCPServer) GetNetMemPool() bytespool.IBytesMemPool { + return server.IBytesMemPool } func (server *TCPServer) run() { @@ -131,7 +130,7 @@ func (server *TCPServer) run() { if max := 1 * time.Second; tempDelay > max { tempDelay = max } - log.Info("accept fail",log.String("error",err.Error()),log.Duration("sleep time", tempDelay)) + log.Info("accept fail", log.String("error", err.Error()), log.Duration("sleep time", tempDelay)) time.Sleep(tempDelay) continue } @@ -153,7 +152,7 @@ func (server *TCPServer) run() { server.mutexConns.Unlock() server.wgConns.Add(1) - tcpConn := newTCPConn(conn, server.PendingWriteNum, &server.MsgParser,server.WriteDeadline) + tcpConn := newTCPConn(conn, server.PendingWriteNum, &server.MsgParser, server.WriteDeadline) agent := server.NewAgent(tcpConn) go func() { diff --git a/network/ws_conn.go b/network/ws_conn.go index a01ccc1..9f290ba 100644 --- a/network/ws_conn.go +++ b/network/ws_conn.go @@ -18,7 +18,7 @@ type WSConn struct { closeFlag bool } -func newWSConn(conn *websocket.Conn, pendingWriteNum int, maxMsgLen uint32,messageType int) *WSConn { +func newWSConn(conn *websocket.Conn, pendingWriteNum int, maxMsgLen uint32, messageType int) *WSConn { wsConn := new(WSConn) wsConn.conn = conn wsConn.writeChan = make(chan []byte, pendingWriteNum) @@ -91,13 +91,13 @@ func (wsConn *WSConn) RemoteAddr() net.Addr { return wsConn.conn.RemoteAddr() } -// goroutine not safe +// ReadMsg goroutine not safe func (wsConn *WSConn) ReadMsg() ([]byte, error) { _, b, err := wsConn.conn.ReadMessage() return b, err } -// args must not be modified by the others goroutines +// WriteMsg args must not be modified by the others goroutines func (wsConn *WSConn) WriteMsg(args ...[]byte) error { wsConn.Lock() defer wsConn.Unlock() diff --git a/node/node.go b/node/node.go index 0d51949..b23a8ef 100644 --- a/node/node.go +++ b/node/node.go @@ -9,6 +9,7 @@ import ( "github.com/duanhf2012/origin/v2/profiler" "github.com/duanhf2012/origin/v2/service" "github.com/duanhf2012/origin/v2/util/buildtime" + "github.com/duanhf2012/origin/v2/util/sysprocess" "github.com/duanhf2012/origin/v2/util/timer" "io" "net/http" @@ -19,34 +20,32 @@ import ( "strings" "syscall" "time" - "github.com/duanhf2012/origin/v2/util/sysprocess" ) var sig chan os.Signal var nodeId string var preSetupService []service.IService //预安装 -var preSetupTemplateService []func()service.IService +var preSetupTemplateService []func() service.IService var profilerInterval time.Duration -var bValid bool var configDir = "./config/" var NodeIsRun = false -const( +const ( SingleStop syscall.Signal = 10 SignalRetire syscall.Signal = 12 ) type BuildOSType = int8 -const( +const ( Windows BuildOSType = 0 - Linux BuildOSType = 1 - Mac BuildOSType = 2 + Linux BuildOSType = 1 + Mac BuildOSType = 2 ) func init() { sig = make(chan os.Signal, 4) - signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM, SingleStop,SignalRetire) + 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) @@ -62,8 +61,7 @@ func init() { console.RegisterCommandString("pprof", "", "<-pprof ip:port> Open performance analysis.", setPprof) } - -func notifyAllServiceRetire(){ +func notifyAllServiceRetire() { service.NotifyAllServiceRetire() } @@ -83,7 +81,7 @@ func usage(val interface{}) error { return nil } -func setName(val interface{}) error { +func setName(_ interface{}) error { return nil } @@ -110,7 +108,7 @@ func setConfigPath(val interface{}) error { } _, err := os.Stat(configPath) if err != nil { - return fmt.Errorf("Cannot find file path %s", configPath) + return fmt.Errorf("cannot find file path %s", configPath) } cluster.SetConfigDir(configPath) @@ -158,7 +156,7 @@ 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.ErrorAttr("error", err)) os.Exit(1) } @@ -169,20 +167,20 @@ func initNode(id string) { //2.顺序安装服务 serviceOrder := cluster.GetCluster().GetLocalNodeInfo().ServiceList - for _,serviceName:= range serviceOrder{ + for _, serviceName := range serviceOrder { bSetup := false //判断是否有配置模板服务 - splitServiceName := strings.Split(serviceName,":") + splitServiceName := strings.Split(serviceName, ":") if len(splitServiceName) == 2 { serviceName = splitServiceName[0] - templateServiceName := splitServiceName[1] - for _,newSer := range preSetupTemplateService { + templateServiceName := splitServiceName[1] + for _, newSer := range preSetupTemplateService { ser := newSer() ser.OnSetup(ser) if ser.GetName() == templateServiceName { ser.SetName(serviceName) - ser.Init(ser,cluster.GetRpcClient,cluster.GetRpcServer,cluster.GetCluster().GetServiceCfg(ser.GetName())) + ser.Init(ser, cluster.GetRpcClient, cluster.GetRpcServer, cluster.GetCluster().GetServiceCfg(ser.GetName())) service.Setup(ser) bSetup = true @@ -190,8 +188,8 @@ func initNode(id string) { } } - if bSetup == false{ - log.Error("Template service not found",log.String("service name",serviceName),log.String("template service name",templateServiceName)) + if bSetup == false { + log.Error("Template service not found", log.String("service name", serviceName), log.String("template service name", templateServiceName)) os.Exit(1) } } @@ -208,7 +206,7 @@ func initNode(id string) { } if bSetup == false { - log.Fatal("Service name "+serviceName+" configuration error") + log.Fatal("Service name " + serviceName + " configuration error") } } @@ -221,9 +219,9 @@ func initLog() error { setLogPath("./log") } - localnodeinfo := cluster.GetCluster().GetLocalNodeInfo() - filepre := fmt.Sprintf("%s_", localnodeinfo.NodeId) - logger, err := log.NewTextLogger(log.LogLevel,log.LogPath,filepre,true,log.LogChannelCap) + 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 @@ -240,7 +238,6 @@ func Start() { } } - func retireNode(args interface{}) error { //1.解析参数 param := args.(string) @@ -265,12 +262,10 @@ func retireNode(args interface{}) error { return err } - RetireProcess(processId) return nil } - func stopNode(args interface{}) error { //1.解析参数 param := args.(string) @@ -318,7 +313,7 @@ func startNode(args interface{}) error { return fmt.Errorf("invalid option %s", param) } - for{ + for { processId, pErr := getRunProcessPid(strNodeId) if pErr != nil { break @@ -328,13 +323,13 @@ 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.Info("get my process's name is error", log.ErrorAttr("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.Info("repeat runs are not allowed", log.String("nodeId", strNodeId), log.Int("processId", processId)) os.Exit(-1) } break @@ -353,7 +348,7 @@ func startNode(args interface{}) error { //5.运行集群 cluster.GetCluster().Start() - + //6.监听程序退出信号&性能报告 var pProfilerTicker *time.Ticker = &time.Ticker{} @@ -369,7 +364,7 @@ func startNode(args interface{}) error { if signal == SignalRetire { log.Info("receipt retire signal.") notifyAllServiceRetire() - }else { + } else { NodeIsRun = false log.Info("receipt stop signal.") } @@ -378,7 +373,6 @@ func startNode(args interface{}) error { } } - //7.退出 service.StopAllService() cluster.GetCluster().Stop() @@ -395,7 +389,7 @@ func Setup(s ...service.IService) { } } -func SetupTemplate(fs ...func()service.IService){ +func SetupTemplate(fs ...func() service.IService) { for _, f := range fs { preSetupTemplateService = append(preSetupTemplateService, f) } @@ -428,7 +422,7 @@ func openConsole(args interface{}) error { } else if strOpen == "true" { log.OpenConsole = true } else { - return errors.New("Parameter console error!") + return errors.New("parameter console error") } return nil } @@ -486,12 +480,12 @@ func setLogSize(args interface{}) error { return nil } - logSize,ok := args.(int) - if ok == false{ + logSize, ok := args.(int) + if ok == false { return errors.New("param logsize is error") } - log.LogSize = int64(logSize)*1024*1024 + log.LogSize = int64(logSize) * 1024 * 1024 return nil } @@ -501,8 +495,8 @@ func setLogChannelCapNum(args interface{}) error { return nil } - logChannelCap,ok := args.(int) - if ok == false{ + logChannelCap, ok := args.(int) + if ok == false { return errors.New("param logsize is error") } diff --git a/profiler/profiler.go b/profiler/profiler.go index 38a3c98..49cdc3c 100644 --- a/profiler/profiler.go +++ b/profiler/profiler.go @@ -2,100 +2,103 @@ package profiler import ( "container/list" - "fmt" "github.com/duanhf2012/origin/v2/log" "sync" "time" ) -//最大超长时间,一般可以认为是死锁或者死循环,或者极差的性能问题 -var DefaultMaxOvertime time.Duration = 5*time.Second -//超过该时间将会监控报告 -var DefaultOvertime time.Duration = 10*time.Millisecond -var DefaultMaxRecordNum int = 100 //最大记录条数 +// DefaultMaxOvertime 最大超长时间,一般可以认为是死锁或者死循环,或者极差的性能问题 +var DefaultMaxOvertime = 5 * time.Second + +// DefaultOvertime 超过该时间将会监控报告 +var DefaultOvertime = 10 * time.Millisecond +var DefaultMaxRecordNum = 100 //最大记录条数 var mapProfiler map[string]*Profiler -type ReportFunType func(name string,callNum int,costTime time.Duration,record *list.List) -var reportFunc ReportFunType =DefaultReportFunction + +type ReportFunType func(name string, callNum int, costTime time.Duration, record *list.List) + +var reportFunc ReportFunType = DefaultReportFunction type Element struct { - tagName string + tagName string pushTime time.Time } type RecordType int -const ( + +const ( MaxOvertimeType = 1 - OvertimeType =2 - ) + OvertimeType = 2 +) type Record struct { - RType RecordType - CostTime time.Duration + RType RecordType + CostTime time.Duration RecordName string } type Analyzer struct { - elem *list.Element + elem *list.Element profiler *Profiler } type Profiler struct { - stack *list.List //Element + stack *list.List //Element stackLocker sync.RWMutex mapAnalyzer map[*list.Element]Analyzer - record *list.List //Record + record *list.List //Record - callNum int //调用次数 + callNum int //调用次数 totalCostTime time.Duration //总消费时间长 - maxOverTime time.Duration - overTime time.Duration + maxOverTime time.Duration + overTime time.Duration maxRecordNum int } -func init(){ +func init() { mapProfiler = map[string]*Profiler{} } func RegProfiler(profilerName string) *Profiler { - if _,ok :=mapProfiler[profilerName];ok==true { + if _, ok := mapProfiler[profilerName]; ok == true { return nil } - pProfiler := &Profiler{stack:list.New(),record:list.New(),maxOverTime: DefaultMaxOvertime,overTime: DefaultOvertime} - mapProfiler[profilerName] =pProfiler + pProfiler := &Profiler{stack: list.New(), record: list.New(), maxOverTime: DefaultMaxOvertime, overTime: DefaultOvertime} + mapProfiler[profilerName] = pProfiler return pProfiler } -func (slf *Profiler) SetMaxOverTime(tm time.Duration){ +func (slf *Profiler) SetMaxOverTime(tm time.Duration) { slf.maxOverTime = tm } -func (slf *Profiler) SetOverTime(tm time.Duration){ +func (slf *Profiler) SetOverTime(tm time.Duration) { slf.overTime = tm } -func (slf *Profiler) SetMaxRecordNum(num int){ +func (slf *Profiler) SetMaxRecordNum(num int) { slf.maxRecordNum = num } -func (slf *Profiler) Push(tag string) *Analyzer{ +func (slf *Profiler) Push(tag string) *Analyzer { slf.stackLocker.Lock() defer slf.stackLocker.Unlock() - pElem := slf.stack.PushBack(&Element{tagName:tag,pushTime:time.Now()}) + pElem := slf.stack.PushBack(&Element{tagName: tag, pushTime: time.Now()}) - return &Analyzer{elem:pElem,profiler:slf} + return &Analyzer{elem: pElem, profiler: slf} } -func (slf *Profiler) check(pElem *Element) (*Record,time.Duration) { +func (slf *Profiler) check(pElem *Element) (*Record, time.Duration) { if pElem == nil { - return nil,0 + return nil, 0 } subTm := time.Now().Sub(pElem.pushTime) if subTm < slf.overTime { - return nil,subTm + return nil, subTm } record := Record{ @@ -104,20 +107,20 @@ func (slf *Profiler) check(pElem *Element) (*Record,time.Duration) { RecordName: pElem.tagName, } - if subTm>slf.maxOverTime { + if subTm > slf.maxOverTime { record.RType = MaxOvertimeType } - return &record,subTm + return &record, subTm } -func (slf *Analyzer) Pop(){ +func (slf *Analyzer) Pop() { slf.profiler.stackLocker.Lock() defer slf.profiler.stackLocker.Unlock() pElement := slf.elem.Value.(*Element) - pElem,subTm := slf.profiler.check(pElement) - slf.profiler.callNum+=1 + pElem, subTm := slf.profiler.check(pElement) + slf.profiler.callNum += 1 slf.profiler.totalCostTime += subTm if pElem != nil { slf.profiler.pushRecordLog(pElem) @@ -125,10 +128,10 @@ func (slf *Analyzer) Pop(){ slf.profiler.stack.Remove(slf.elem) } -func (slf *Profiler) pushRecordLog(record *Record){ - if slf.record.Len()>= DefaultMaxRecordNum { +func (slf *Profiler) pushRecordLog(record *Record) { + if slf.record.Len() >= DefaultMaxRecordNum { front := slf.stack.Front() - if front!=nil { + if front != nil { slf.stack.Remove(front) } } @@ -140,43 +143,43 @@ func SetReportFunction(reportFun ReportFunType) { reportFunc = reportFun } -func DefaultReportFunction(name string,callNum int,costTime time.Duration,record *list.List){ - if record.Len()<=0 { +func DefaultReportFunction(name string, callNum int, costTime time.Duration, record *list.List) { + if record.Len() <= 0 { return } var average int64 - if callNum>0 { - average = costTime.Milliseconds()/int64(callNum) + if callNum > 0 { + average = costTime.Milliseconds() / int64(callNum) } - log.Info("Profiler report tag "+name,log.Int("process count",callNum),log.Int64("take time",costTime.Milliseconds()),log.Int64("average",average)) + log.Info("Profiler report tag "+name, log.Int("process count", callNum), log.Int64("take time", costTime.Milliseconds()), log.Int64("average", average)) elem := record.Front() var strTypes string - for elem!=nil { + for elem != nil { pRecord := elem.Value.(*Record) - if pRecord.RType == MaxOvertimeType { + if pRecord.RType == MaxOvertimeType { strTypes = "too slow process" - }else{ + } else { strTypes = "slow process" } - log.Info("Profiler report type",log.String("Types",strTypes),log.String("RecordName",pRecord.RecordName),log.Int64("take time",pRecord.CostTime.Milliseconds())) + log.Info("Profiler report type", log.String("Types", strTypes), log.String("RecordName", pRecord.RecordName), log.Int64("take time", pRecord.CostTime.Milliseconds())) elem = elem.Next() } } func Report() { var record *list.List - for name,prof := range mapProfiler{ + for name, prof := range mapProfiler { prof.stackLocker.RLock() //取栈顶,是否存在异常MaxOverTime数据 pElem := prof.stack.Back() - for pElem!=nil { + for pElem != nil { pElement := pElem.Value.(*Element) - pExceptionElem,_ := prof.check(pElement) - if pExceptionElem!=nil { + pExceptionElem, _ := prof.check(pElement) + if pExceptionElem != nil { prof.pushRecordLog(pExceptionElem) } pElem = pElem.Prev() @@ -193,7 +196,6 @@ func Report() { totalCostTime := prof.totalCostTime prof.stackLocker.RUnlock() - DefaultReportFunction(name,callNum,totalCostTime,record) + DefaultReportFunction(name, callNum, totalCostTime, record) } } - diff --git a/rpc/callset.go b/rpc/callset.go index 7e25b03..8d80d3d 100644 --- a/rpc/callset.go +++ b/rpc/callset.go @@ -3,11 +3,11 @@ package rpc import ( "errors" + "github.com/duanhf2012/origin/v2/log" "strconv" "sync" "sync/atomic" "time" - "github.com/duanhf2012/origin/v2/log" ) type CallSet struct { @@ -20,12 +20,10 @@ type CallSet struct { callTimerHeap CallTimerHeap } - - -func (cs *CallSet) Init(){ +func (cs *CallSet) Init() { cs.pendingLock.Lock() cs.callTimerHeap.Init() - cs.pending = make(map[uint64]*Call,4096) + cs.pending = make(map[uint64]*Call, 4096) cs.maxCheckCallRpcCount = DefaultMaxCheckCallRpcCount cs.callRpcTimeout = DefaultRpcTimeout @@ -34,7 +32,7 @@ func (cs *CallSet) Init(){ cs.pendingLock.Unlock() } -func (bc *CallSet) makeCallFail(call *Call) { +func (cs *CallSet) makeCallFail(call *Call) { if call.callback != nil && call.callback.IsValid() { call.rpcHandler.PushRpcResponse(call) } else { @@ -42,105 +40,105 @@ func (bc *CallSet) makeCallFail(call *Call) { } } -func (bc *CallSet) checkRpcCallTimeout() { - for{ +func (cs *CallSet) checkRpcCallTimeout() { + for { time.Sleep(DefaultCheckRpcCallTimeoutInterval) - for i := 0; i < bc.maxCheckCallRpcCount; i++ { - bc.pendingLock.Lock() + for i := 0; i < cs.maxCheckCallRpcCount; i++ { + cs.pendingLock.Lock() - callSeq := bc.callTimerHeap.PopTimeout() + callSeq := cs.callTimerHeap.PopTimeout() if callSeq == 0 { - bc.pendingLock.Unlock() + cs.pendingLock.Unlock() break } - pCall := bc.pending[callSeq] + pCall := cs.pending[callSeq] if pCall == nil { - bc.pendingLock.Unlock() - log.Error("call seq is not find",log.Uint64("seq", callSeq)) + cs.pendingLock.Unlock() + log.Error("call seq is not find", log.Uint64("seq", callSeq)) continue } - delete(bc.pending,callSeq) + delete(cs.pending, callSeq) strTimeout := strconv.FormatInt(int64(pCall.TimeOut.Seconds()), 10) - pCall.Err = errors.New("RPC call takes more than " + strTimeout + " seconds,method is "+pCall.ServiceMethod) - log.Error("call timeout",log.String("error",pCall.Err.Error())) - bc.makeCallFail(pCall) - bc.pendingLock.Unlock() + pCall.Err = errors.New("RPC call takes more than " + strTimeout + " seconds,method is " + pCall.ServiceMethod) + log.Error("call timeout", log.String("error", pCall.Err.Error())) + cs.makeCallFail(pCall) + cs.pendingLock.Unlock() continue } } } -func (bc *CallSet) AddPending(call *Call) { - bc.pendingLock.Lock() +func (cs *CallSet) AddPending(call *Call) { + cs.pendingLock.Lock() if call.Seq == 0 { - bc.pendingLock.Unlock() + cs.pendingLock.Unlock() log.Stack("call is error.") return } - bc.pending[call.Seq] = call - bc.callTimerHeap.AddTimer(call.Seq,call.TimeOut) + cs.pending[call.Seq] = call + cs.callTimerHeap.AddTimer(call.Seq, call.TimeOut) - bc.pendingLock.Unlock() + cs.pendingLock.Unlock() } -func (bc *CallSet) RemovePending(seq uint64) *Call { - if seq == 0 { +func (cs *CallSet) RemovePending(seq uint64) *Call { + if seq == 0 { return nil } - bc.pendingLock.Lock() - call := bc.removePending(seq) - bc.pendingLock.Unlock() + cs.pendingLock.Lock() + call := cs.removePending(seq) + cs.pendingLock.Unlock() return call } -func (bc *CallSet) removePending(seq uint64) *Call { - v, ok := bc.pending[seq] +func (cs *CallSet) removePending(seq uint64) *Call { + v, ok := cs.pending[seq] if ok == false { return nil } - bc.callTimerHeap.Cancel(seq) - delete(bc.pending, seq) + cs.callTimerHeap.Cancel(seq) + delete(cs.pending, seq) return v } -func (bc *CallSet) FindPending(seq uint64) (pCall *Call) { +func (cs *CallSet) FindPending(seq uint64) (pCall *Call) { if seq == 0 { return nil } - bc.pendingLock.Lock() - pCall = bc.pending[seq] - bc.pendingLock.Unlock() + cs.pendingLock.Lock() + pCall = cs.pending[seq] + cs.pendingLock.Unlock() return pCall } -func (bc *CallSet) cleanPending(){ - bc.pendingLock.Lock() - for { - callSeq := bc.callTimerHeap.PopFirst() +func (cs *CallSet) cleanPending() { + cs.pendingLock.Lock() + for { + callSeq := cs.callTimerHeap.PopFirst() if callSeq == 0 { break } - pCall := bc.pending[callSeq] + pCall := cs.pending[callSeq] if pCall == nil { - log.Error("call Seq is not find",log.Uint64("seq",callSeq)) + log.Error("call Seq is not find", log.Uint64("seq", callSeq)) continue } - delete(bc.pending,callSeq) - pCall.Err = errors.New("nodeid is disconnect ") - bc.makeCallFail(pCall) + delete(cs.pending, callSeq) + pCall.Err = errors.New("node is disconnect ") + cs.makeCallFail(pCall) } - bc.pendingLock.Unlock() + cs.pendingLock.Unlock() } -func (bc *CallSet) generateSeq() uint64 { - return atomic.AddUint64(&bc.startSeq, 1) +func (cs *CallSet) generateSeq() uint64 { + return atomic.AddUint64(&cs.startSeq, 1) } diff --git a/rpc/client.go b/rpc/client.go index 3118f19..891d009 100644 --- a/rpc/client.go +++ b/rpc/client.go @@ -2,29 +2,29 @@ package rpc import ( "errors" + "fmt" + "github.com/duanhf2012/origin/v2/log" "github.com/duanhf2012/origin/v2/network" "reflect" "time" - "github.com/duanhf2012/origin/v2/log" - "fmt" ) -const( +const ( DefaultRpcConnNum = 1 DefaultRpcLenMsgLen = 4 DefaultRpcMinMsgLen = 2 DefaultMaxCheckCallRpcCount = 1000 - DefaultMaxPendingWriteNum = 1000000 + DefaultMaxPendingWriteNum = 1000000 - DefaultConnectInterval = 2*time.Second - DefaultCheckRpcCallTimeoutInterval = 1*time.Second - DefaultRpcTimeout = 15*time.Second + DefaultConnectInterval = 2 * time.Second + DefaultCheckRpcCallTimeoutInterval = 1 * time.Second + DefaultRpcTimeout = 15 * time.Second ) var clientSeq uint32 type IWriter interface { - WriteMsg (nodeId string,args ...[]byte) error + WriteMsg(nodeId string, args ...[]byte) error IsConnected() bool } @@ -32,9 +32,9 @@ type IRealClient interface { SetConn(conn *network.TCPConn) Close(waitDone bool) - AsyncCall(NodeId string,timeout time.Duration,rpcHandler IRpcHandler, serviceMethod string, callback reflect.Value, args interface{}, replyParam interface{},cancelable bool) (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 + AsyncCall(NodeId string, timeout time.Duration, rpcHandler IRpcHandler, serviceMethod string, callback reflect.Value, args interface{}, replyParam interface{}, cancelable bool) (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 Run() @@ -43,11 +43,9 @@ type IRealClient interface { Bind(server IServer) } - - type Client struct { - clientId uint32 - targetNodeId string + clientId uint32 + targetNodeId string compressBytesLen int *CallSet @@ -68,13 +66,12 @@ func (client *Client) GetClientId() uint32 { return client.clientId } - -func (client *Client) processRpcResponse(responseData []byte) error{ - bCompress := (responseData[0]>>7) > 0 - processor := GetProcessor(responseData[0]&0x7f) +func (client *Client) processRpcResponse(responseData []byte) error { + bCompress := (responseData[0] >> 7) > 0 + processor := GetProcessor(responseData[0] & 0x7f) if processor == nil { //rc.conn.ReleaseReadMsg(responseData) - err:= errors.New(fmt.Sprintf("cannot find process %d",responseData[0]&0x7f)) + err := errors.New(fmt.Sprintf("cannot find process %d", responseData[0]&0x7f)) log.Error(err.Error()) return err } @@ -89,10 +86,10 @@ func (client *Client) processRpcResponse(responseData []byte) error{ if bCompress == true { var unCompressErr error - compressBuff,unCompressErr = compressor.UncompressBlock(byteData) - if unCompressErr!= nil { + compressBuff, unCompressErr = compressor.UncompressBlock(byteData) + if unCompressErr != nil { //rc.conn.ReleaseReadMsg(responseData) - err := fmt.Errorf("uncompressBlock failed,err :%s",unCompressErr.Error()) + err := fmt.Errorf("uncompressBlock failed,err :%s", unCompressErr.Error()) return err } @@ -107,19 +104,19 @@ 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.ErrorAttr("error", err)) return nil } v := client.RemovePending(response.RpcResponseData.GetSeq()) if v == nil { - log.Error("rpcClient cannot find seq",log.Uint64("seq",response.RpcResponseData.GetSeq())) + log.Error("rpcClient cannot find seq", log.Uint64("seq", response.RpcResponseData.GetSeq())) } else { v.Err = nil 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.ErrorAttr("error", err)) v.Err = err } } @@ -140,7 +137,6 @@ func (client *Client) processRpcResponse(responseData []byte) error{ return nil } - //func (rc *Client) Go(timeout time.Duration,rpcHandler IRpcHandler,noReply bool, serviceMethod string, args interface{}, reply interface{}) *Call { // _, processor := GetProcessorType(args) // InParam, err := processor.Marshal(args) @@ -154,11 +150,11 @@ func (client *Client) processRpcResponse(responseData []byte) error{ // return rc.RawGo(timeout,rpcHandler,processor, noReply, 0, serviceMethod, InParam, reply) //} -func (rc *Client) rawGo(nodeId string,w IWriter,timeout time.Duration,rpcHandler IRpcHandler,processor IRpcProcessor, noReply bool, rpcMethodId uint32, serviceMethod string, rawArgs []byte, reply interface{}) *Call { +func (client *Client) rawGo(nodeId string, w IWriter, timeout time.Duration, rpcHandler IRpcHandler, processor IRpcProcessor, noReply bool, rpcMethodId uint32, serviceMethod string, rawArgs []byte, reply interface{}) *Call { call := MakeCall() call.ServiceMethod = serviceMethod call.Reply = reply - call.Seq = rc.generateSeq() + call.Seq = client.generateSeq() call.TimeOut = timeout request := MakeRpcRequest(processor, call.Seq, rpcMethodId, serviceMethod, noReply, rawArgs) @@ -167,47 +163,47 @@ func (rc *Client) rawGo(nodeId string,w IWriter,timeout time.Duration,rpcHandler if err != nil { call.Seq = 0 - log.Error("marshal is fail",log.String("error",err.Error())) + log.Error("marshal is fail", log.String("error", err.Error())) call.DoError(err) return call } - if w == nil || w.IsConnected()==false { + if w == nil || w.IsConnected() == false { call.Seq = 0 sErr := errors.New(serviceMethod + " was called failed,rpc client is disconnect") - log.Error("conn is disconnect",log.String("error",sErr.Error())) + log.Error("conn is disconnect", log.String("error", sErr.Error())) call.DoError(sErr) return call } - var compressBuff[]byte + var compressBuff []byte bCompress := uint8(0) - if rc.compressBytesLen > 0 && len(bytes) >= rc.compressBytesLen { + if client.compressBytesLen > 0 && len(bytes) >= client.compressBytesLen { var cErr error - compressBuff,cErr = compressor.CompressBlock(bytes) + compressBuff, cErr = compressor.CompressBlock(bytes) if cErr != nil { call.Seq = 0 - log.Error("compress fail",log.String("error",cErr.Error())) + log.Error("compress fail", log.String("error", cErr.Error())) call.DoError(cErr) return call } if len(compressBuff) < len(bytes) { bytes = compressBuff - bCompress = 1<<7 + bCompress = 1 << 7 } } if noReply == false { - rc.AddPending(call) + client.AddPending(call) } - err = w.WriteMsg(nodeId,[]byte{uint8(processor.GetProcessorType())|bCompress}, bytes) - if cap(compressBuff) >0 { + err = w.WriteMsg(nodeId, []byte{uint8(processor.GetProcessorType()) | bCompress}, bytes) + if cap(compressBuff) > 0 { compressor.CompressBufferCollection(compressBuff) } if err != nil { - rc.RemovePending(call.Seq) - log.Error("WiteMsg is fail",log.ErrorAttr("error",err)) + client.RemovePending(call.Seq) + log.Error("WriteMsg is fail", log.ErrorAttr("error", err)) call.Seq = 0 call.DoError(err) } @@ -215,37 +211,37 @@ func (rc *Client) rawGo(nodeId string,w IWriter,timeout time.Duration,rpcHandler return call } -func (rc *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{}, cancelable bool) (CancelRpc, error) { processorType, processor := GetProcessorType(args) InParam, herr := processor.Marshal(args) if herr != nil { - return emptyCancelRpc,herr + return emptyCancelRpc, herr } - seq := rc.generateSeq() + seq := client.generateSeq() request := MakeRpcRequest(processor, seq, 0, serviceMethod, false, InParam) bytes, err := processor.Marshal(request.RpcRequestData) ReleaseRpcRequest(request) if err != nil { - return emptyCancelRpc,err + return emptyCancelRpc, err } - if w == nil || w.IsConnected()==false { - return emptyCancelRpc,errors.New("Rpc server is disconnect,call " + serviceMethod) + if w == nil || w.IsConnected() == false { + return emptyCancelRpc, errors.New("Rpc server is disconnect,call " + serviceMethod) } - var compressBuff[]byte + var compressBuff []byte bCompress := uint8(0) - if rc.compressBytesLen>0 &&len(bytes) >= rc.compressBytesLen { + if client.compressBytesLen > 0 && len(bytes) >= client.compressBytesLen { var cErr error - compressBuff,cErr = compressor.CompressBlock(bytes) + compressBuff, cErr = compressor.CompressBlock(bytes) if cErr != nil { - return emptyCancelRpc,cErr + return emptyCancelRpc, cErr } if len(compressBuff) < len(bytes) { bytes = compressBuff - bCompress = 1<<7 + bCompress = 1 << 7 } } @@ -256,22 +252,22 @@ func (rc *Client) asyncCall(nodeId string,w IWriter,timeout time.Duration,rpcHan call.ServiceMethod = serviceMethod call.Seq = seq call.TimeOut = timeout - rc.AddPending(call) + client.AddPending(call) - err = w.WriteMsg(nodeId,[]byte{uint8(processorType)|bCompress}, bytes) - if cap(compressBuff) >0 { + err = w.WriteMsg(nodeId, []byte{uint8(processorType) | bCompress}, bytes) + if cap(compressBuff) > 0 { compressor.CompressBufferCollection(compressBuff) } if err != nil { - rc.RemovePending(call.Seq) + client.RemovePending(call.Seq) ReleaseCall(call) - return emptyCancelRpc,err + return emptyCancelRpc, err } if cancelable { - rpcCancel := RpcCancel{CallSeq:seq,Cli: rc} - return rpcCancel.CancelRpc,nil + rpcCancel := RpcCancel{CallSeq: seq, Cli: client} + return rpcCancel.CancelRpc, nil } - return emptyCancelRpc,nil -} \ No newline at end of file + return emptyCancelRpc, nil +} diff --git a/rpc/compressor.go b/rpc/compressor.go index 51f449e..1652fdf 100644 --- a/rpc/compressor.go +++ b/rpc/compressor.go @@ -8,22 +8,23 @@ import ( "runtime" ) -var memPool bytespool.IBytesMempool = bytespool.NewMemAreaPool() +var memPool bytespool.IBytesMemPool = bytespool.NewMemAreaPool() type ICompressor interface { CompressBlock(src []byte) ([]byte, error) //dst如果有预申请使用dst内存,传入nil时内部申请 UncompressBlock(src []byte) ([]byte, error) //dst如果有预申请使用dst内存,传入nil时内部申请 - CompressBufferCollection(buffer []byte) //压缩的Buffer内存回收 - UnCompressBufferCollection(buffer []byte) //解压缩的Buffer内存回收 + CompressBufferCollection(buffer []byte) //压缩的Buffer内存回收 + UnCompressBufferCollection(buffer []byte) //解压缩的Buffer内存回收 } var compressor ICompressor -func init(){ + +func init() { SetCompressor(&Lz4Compressor{}) } -func SetCompressor(cp ICompressor){ +func SetCompressor(cp ICompressor) { compressor = cp } @@ -42,21 +43,21 @@ func (lc *Lz4Compressor) CompressBlock(src []byte) (dest []byte, err error) { var c lz4.Compressor var cnt int - dest = memPool.MakeBytes(lz4.CompressBlockBound(len(src))+1) + dest = memPool.MakeBytes(lz4.CompressBlockBound(len(src)) + 1) cnt, err = c.CompressBlock(src, dest[1:]) if err != nil { memPool.ReleaseBytes(dest) - return nil,err + return nil, err } ratio := len(src) / cnt - if len(src) % cnt > 0 { + if len(src)%cnt > 0 { ratio += 1 } if ratio > 255 { memPool.ReleaseBytes(dest) - return nil,fmt.Errorf("Impermissible errors") + return nil, fmt.Errorf("impermissible errors") } dest[0] = uint8(ratio) @@ -76,24 +77,24 @@ func (lc *Lz4Compressor) UncompressBlock(src []byte) (dest []byte, err error) { radio := uint8(src[0]) if radio == 0 { - return nil,fmt.Errorf("Impermissible errors") + return nil, fmt.Errorf("impermissible errors") } - dest = memPool.MakeBytes(len(src)*int(radio)) + dest = memPool.MakeBytes(len(src) * int(radio)) cnt, err := lz4.UncompressBlock(src[1:], dest) if err != nil { memPool.ReleaseBytes(dest) - return nil,err + return nil, err } - return dest[:cnt],nil + return dest[:cnt], nil } -func (lc *Lz4Compressor) compressBlockBound(n int) int{ +func (lc *Lz4Compressor) compressBlockBound(n int) int { return lz4.CompressBlockBound(n) } -func (lc *Lz4Compressor) CompressBufferCollection(buffer []byte){ +func (lc *Lz4Compressor) CompressBufferCollection(buffer []byte) { memPool.ReleaseBytes(buffer) } diff --git a/rpc/lclient.go b/rpc/lclient.go index 8285d4d..5c0dbd1 100644 --- a/rpc/lclient.go +++ b/rpc/lclient.go @@ -10,40 +10,40 @@ import ( "time" ) -//本结点的Client +// LClient 本结点的Client type LClient struct { selfClient *Client } -func (rc *LClient) Lock(){ +func (lc *LClient) Lock() { } -func (rc *LClient) Unlock(){ +func (lc *LClient) Unlock() { } -func (lc *LClient) Run(){ +func (lc *LClient) Run() { } -func (lc *LClient) OnClose(){ +func (lc *LClient) OnClose() { } func (lc *LClient) IsConnected() bool { return true } -func (lc *LClient) SetConn(conn *network.TCPConn){ +func (lc *LClient) SetConn(conn *network.TCPConn) { } -func (lc *LClient) Close(waitDone bool){ +func (lc *LClient) Close(waitDone bool) { } -func (lc *LClient) Go(nodeId string,timeout time.Duration,rpcHandler IRpcHandler,noReply bool, serviceMethod string, args interface{}, reply interface{}) *Call { +func (lc *LClient) Go(nodeId string, timeout time.Duration, rpcHandler IRpcHandler, noReply bool, serviceMethod string, args interface{}, reply interface{}) *Call { pLocalRpcServer := rpcHandler.GetRpcServer()() //判断是否是同一服务 findIndex := strings.Index(serviceMethod, ".") if findIndex == -1 { sErr := errors.New("Call serviceMethod " + serviceMethod + " is error!") - log.Error("call rpc fail",log.String("error",sErr.Error())) + log.Error("call rpc fail", log.String("error", sErr.Error())) call := MakeCall() call.DoError(sErr) @@ -53,7 +53,7 @@ func (lc *LClient) Go(nodeId string,timeout time.Duration,rpcHandler IRpcHandler serviceName := serviceMethod[:findIndex] if serviceName == rpcHandler.GetName() { //自己服务调用 //调用自己rpcHandler处理器 - err := pLocalRpcServer.myselfRpcHandlerGo(lc.selfClient,serviceName, serviceMethod, args, requestHandlerNull,reply) + err := pLocalRpcServer.myselfRpcHandlerGo(lc.selfClient, serviceName, serviceMethod, args, requestHandlerNull, reply) call := MakeCall() if err != nil { @@ -66,13 +66,12 @@ func (lc *LClient) Go(nodeId string,timeout time.Duration,rpcHandler IRpcHandler } //其他的rpcHandler的处理器 - return pLocalRpcServer.selfNodeRpcHandlerGo(timeout,nil, lc.selfClient, noReply, serviceName, 0, serviceMethod, args, reply, nil) + return pLocalRpcServer.selfNodeRpcHandlerGo(timeout, nil, lc.selfClient, noReply, serviceName, 0, serviceMethod, args, reply, nil) } - -func (rc *LClient) RawGo(nodeId string,timeout time.Duration,rpcHandler IRpcHandler,processor IRpcProcessor, noReply bool, rpcMethodId uint32, serviceName string, rawArgs []byte, reply interface{}) *Call { +func (lc *LClient) RawGo(nodeId string, timeout time.Duration, rpcHandler IRpcHandler, processor IRpcProcessor, noReply bool, rpcMethodId uint32, serviceName string, rawArgs []byte, reply interface{}) *Call { pLocalRpcServer := rpcHandler.GetRpcServer()() - + //服务自我调用 if serviceName == rpcHandler.GetName() { call := MakeCall() @@ -80,7 +79,7 @@ func (rc *LClient) RawGo(nodeId string,timeout time.Duration,rpcHandler IRpcHand call.Reply = reply call.TimeOut = timeout - err := pLocalRpcServer.myselfRpcHandlerGo(rc.selfClient,serviceName, serviceName, rawArgs, requestHandlerNull,nil) + err := pLocalRpcServer.myselfRpcHandlerGo(lc.selfClient, serviceName, serviceName, rawArgs, requestHandlerNull, nil) call.Err = err call.done <- call @@ -88,11 +87,10 @@ func (rc *LClient) RawGo(nodeId string,timeout time.Duration,rpcHandler IRpcHand } //其他的rpcHandler的处理器 - return pLocalRpcServer.selfNodeRpcHandlerGo(timeout,processor,rc.selfClient, true, serviceName, rpcMethodId, serviceName, nil, nil, rawArgs) + 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{}, cancelable bool) (CancelRpc, error) { pLocalRpcServer := rpcHandler.GetRpcServer()() //判断是否是同一服务 @@ -100,26 +98,26 @@ func (lc *LClient) AsyncCall(nodeId string,timeout time.Duration,rpcHandler IRpc if findIndex == -1 { err := errors.New("Call serviceMethod " + serviceMethod + " is error!") callback.Call([]reflect.Value{reflect.ValueOf(reply), reflect.ValueOf(err)}) - log.Error("serviceMethod format is error",log.String("error",err.Error())) - return emptyCancelRpc,nil + log.Error("serviceMethod format is error", log.String("error", err.Error())) + return emptyCancelRpc, nil } serviceName := serviceMethod[:findIndex] //调用自己rpcHandler处理器 if serviceName == rpcHandler.GetName() { //自己服务调用 - return emptyCancelRpc,pLocalRpcServer.myselfRpcHandlerGo(lc.selfClient,serviceName, serviceMethod, args,callback ,reply) + return emptyCancelRpc, pLocalRpcServer.myselfRpcHandlerGo(lc.selfClient, serviceName, serviceMethod, args, callback, reply) } //其他的rpcHandler的处理器 - calcelRpc,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, cancelable) if err != nil { callback.Call([]reflect.Value{reflect.ValueOf(reply), reflect.ValueOf(err)}) } - return calcelRpc,nil + return cancelRpc, nil } -func NewLClient(localNodeId string,callSet *CallSet) *Client{ +func NewLClient(localNodeId string, callSet *CallSet) *Client { client := &Client{} client.clientId = atomic.AddUint32(&clientSeq, 1) client.targetNodeId = localNodeId @@ -133,5 +131,5 @@ func NewLClient(localNodeId string,callSet *CallSet) *Client{ return client } -func (rc *LClient) Bind(server IServer){ -} \ No newline at end of file +func (lc *LClient) Bind(_ IServer) { +} diff --git a/rpc/lserver.go b/rpc/lserver.go index e0e29be..6f08d28 100644 --- a/rpc/lserver.go +++ b/rpc/lserver.go @@ -1,48 +1,48 @@ package rpc + import ( "errors" + "fmt" "github.com/duanhf2012/origin/v2/log" "reflect" - "time" "strings" - "fmt" + "time" ) - type BaseServer struct { - localNodeId string + localNodeId string compressBytesLen int rpcHandleFinder RpcHandleFinder - iServer IServer + iServer IServer } -func (ls *BaseServer) initBaseServer(compressBytesLen int,rpcHandleFinder RpcHandleFinder){ - ls.compressBytesLen = compressBytesLen - ls.rpcHandleFinder = rpcHandleFinder +func (server *BaseServer) initBaseServer(compressBytesLen int, rpcHandleFinder RpcHandleFinder) { + server.compressBytesLen = compressBytesLen + server.rpcHandleFinder = rpcHandleFinder } -func (ls *BaseServer) myselfRpcHandlerGo(client *Client,handlerName string, serviceMethod string, args interface{},callBack reflect.Value, reply interface{}) error { - rpcHandler := ls.rpcHandleFinder.FindRpcHandler(handlerName) +func (server *BaseServer) myselfRpcHandlerGo(client *Client, handlerName string, serviceMethod string, args interface{}, callBack reflect.Value, reply interface{}) error { + 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.Error("service method not config", log.String("serviceMethod", serviceMethod)) return err } - return rpcHandler.CallMethod(client,serviceMethod, args,callBack, reply) + return rpcHandler.CallMethod(client, serviceMethod, args, callBack, reply) } -func (ls *BaseServer) selfNodeRpcHandlerGo(timeout time.Duration,processor IRpcProcessor, client *Client, noReply bool, handlerName string, rpcMethodId uint32, serviceMethod string, args interface{}, reply interface{}, rawArgs []byte) *Call { +func (server *BaseServer) selfNodeRpcHandlerGo(timeout time.Duration, processor IRpcProcessor, client *Client, noReply bool, handlerName string, rpcMethodId uint32, serviceMethod string, args interface{}, reply interface{}, rawArgs []byte) *Call { pCall := MakeCall() pCall.Seq = client.generateSeq() pCall.TimeOut = timeout pCall.ServiceMethod = serviceMethod - rpcHandler := ls.rpcHandleFinder.FindRpcHandler(handlerName) + 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.ErrorAttr("error", err)) pCall.Seq = 0 pCall.DoError(err) @@ -56,10 +56,10 @@ func (ls *BaseServer) selfNodeRpcHandlerGo(timeout time.Duration,processor IRpcP if args != nil { var err error - iParam,err = processor.Clone(args) + iParam, err = processor.Clone(args) if err != nil { - sErr := errors.New("RpcHandler " + handlerName + "."+serviceMethod+" deep copy inParam is error:" + err.Error()) - log.Error("deep copy inParam is failed",log.String("handlerName",handlerName),log.String("serviceMethod",serviceMethod)) + sErr := errors.New("RpcHandler " + handlerName + "." + serviceMethod + " deep copy inParam is error:" + err.Error()) + log.Error("deep copy inParam is failed", log.String("handlerName", handlerName), log.String("serviceMethod", serviceMethod)) pCall.Seq = 0 pCall.DoError(sErr) @@ -74,7 +74,7 @@ func (ls *BaseServer) selfNodeRpcHandlerGo(timeout time.Duration,processor IRpcP 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.ErrorAttr("error", err)) pCall.Seq = 0 pCall.DoError(err) ReleaseRpcRequest(req) @@ -90,12 +90,12 @@ func (ls *BaseServer) selfNodeRpcHandlerGo(timeout time.Duration,processor IRpcP 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)) - }else{ + log.Error("returns data cannot be marshal", log.Uint64("seq", callSeq), log.ErrorAttr("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.ErrorAttr("error", err)) } } } @@ -103,7 +103,7 @@ func (ls *BaseServer) selfNodeRpcHandlerGo(timeout time.Duration,processor IRpcP ReleaseRpcRequest(req) v := client.RemovePending(callSeq) if v == nil { - log.Error("rpcClient cannot find seq",log.Uint64("seq",callSeq)) + log.Error("rpcClient cannot find seq", log.Uint64("seq", callSeq)) return } @@ -127,20 +127,20 @@ func (ls *BaseServer) selfNodeRpcHandlerGo(timeout time.Duration,processor IRpcP 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, cancelable bool) (CancelRpc, error) { rpcHandler := server.rpcHandleFinder.FindRpcHandler(handlerName) if rpcHandler == nil { err := errors.New("service method " + serviceMethod + " not config!") log.Error(err.Error()) - return emptyCancelRpc,err + return emptyCancelRpc, err } _, processor := GetProcessorType(args) - iParam,err := processor.Clone(args) + iParam, err := processor.Clone(args) if err != nil { - errM := errors.New("RpcHandler " + handlerName + "."+serviceMethod+" deep copy inParam is error:" + err.Error()) + errM := errors.New("RpcHandler " + handlerName + "." + serviceMethod + " deep copy inParam is error:" + err.Error()) log.Error(errM.Error()) - return emptyCancelRpc,errM + return emptyCancelRpc, errM } req := MakeRpcRequest(processor, 0, 0, serviceMethod, noReply, nil) @@ -159,7 +159,7 @@ func (server *BaseServer) selfNodeRpcHandlerAsyncGo(timeout time.Duration,client pCall.ServiceMethod = serviceMethod pCall.TimeOut = timeout client.AddPending(pCall) - rpcCancel := RpcCancel{CallSeq: callSeq,Cli: client} + rpcCancel := RpcCancel{CallSeq: callSeq, Cli: client} cancelRpc = rpcCancel.CancelRpc req.requestHandle = func(Returns interface{}, Err RpcError) { @@ -188,15 +188,15 @@ func (server *BaseServer) selfNodeRpcHandlerAsyncGo(timeout time.Duration,client if callSeq > 0 { client.RemovePending(callSeq) } - return emptyCancelRpc,err + return emptyCancelRpc, err } - return cancelRpc,nil + return cancelRpc, nil } -func (bs *BaseServer) processRpcRequest(data []byte,connTag string,wrResponse writeResponse) error{ - bCompress := (data[0]>>7) > 0 - processor := GetProcessor(data[0]&0x7f) +func (server *BaseServer) processRpcRequest(data []byte, connTag string, wrResponse writeResponse) error { + bCompress := (data[0] >> 7) > 0 + processor := GetProcessor(data[0] & 0x7f) if processor == nil { return errors.New("cannot find processor") } @@ -207,9 +207,9 @@ func (bs *BaseServer) processRpcRequest(data []byte,connTag string,wrResponse wr if bCompress == true { var unCompressErr error - compressBuff,unCompressErr = compressor.UncompressBlock(byteData) - if unCompressErr!= nil { - return errors.New("UncompressBlock failed") + compressBuff, unCompressErr = compressor.UncompressBlock(byteData) + if unCompressErr != nil { + return errors.New("uncompressBlock failed") } byteData = compressBuff @@ -225,7 +225,7 @@ func (bs *BaseServer) processRpcRequest(data []byte,connTag string,wrResponse wr if req.RpcRequestData.GetSeq() > 0 { rpcError := RpcError(err.Error()) if req.RpcRequestData.IsNoReply() == false { - wrResponse(processor,connTag, req.RpcRequestData.GetServiceMethod(), req.RpcRequestData.GetSeq(), nil, rpcError) + wrResponse(processor, connTag, req.RpcRequestData.GetServiceMethod(), req.RpcRequestData.GetSeq(), nil, rpcError) } } @@ -238,27 +238,27 @@ func (bs *BaseServer) processRpcRequest(data []byte,connTag string,wrResponse wr if len(serviceMethod) < 1 { rpcError := RpcError("rpc request req.ServiceMethod is error") if req.RpcRequestData.IsNoReply() == false { - wrResponse(processor,connTag, req.RpcRequestData.GetServiceMethod(), req.RpcRequestData.GetSeq(), nil, rpcError) + wrResponse(processor, connTag, req.RpcRequestData.GetServiceMethod(), req.RpcRequestData.GetSeq(), nil, rpcError) } ReleaseRpcRequest(req) log.Error("rpc request req.ServiceMethod is error") return nil } - rpcHandler := bs.rpcHandleFinder.FindRpcHandler(serviceMethod[0]) + rpcHandler := server.rpcHandleFinder.FindRpcHandler(serviceMethod[0]) if rpcHandler == nil { rpcError := RpcError(fmt.Sprintf("service method %s not config!", req.RpcRequestData.GetServiceMethod())) if req.RpcRequestData.IsNoReply() == false { - wrResponse(processor,connTag, req.RpcRequestData.GetServiceMethod(), req.RpcRequestData.GetSeq(), nil, rpcError) + wrResponse(processor, connTag, req.RpcRequestData.GetServiceMethod(), req.RpcRequestData.GetSeq(), nil, rpcError) } - log.Error("serviceMethod not config",log.String("serviceMethod",req.RpcRequestData.GetServiceMethod())) + log.Error("serviceMethod not config", log.String("serviceMethod", req.RpcRequestData.GetServiceMethod())) ReleaseRpcRequest(req) return nil } if req.RpcRequestData.IsNoReply() == false { req.requestHandle = func(Returns interface{}, Err RpcError) { - wrResponse(processor,connTag, req.RpcRequestData.GetServiceMethod(), req.RpcRequestData.GetSeq(), Returns, Err) + wrResponse(processor, connTag, req.RpcRequestData.GetServiceMethod(), req.RpcRequestData.GetSeq(), Returns, Err) ReleaseRpcRequest(req) } } @@ -266,7 +266,7 @@ func (bs *BaseServer) processRpcRequest(data []byte,connTag string,wrResponse wr 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.ErrorAttr("error", err)) if req.requestHandle != nil { req.requestHandle(nil, RpcError(rErr)) } else { @@ -281,11 +281,11 @@ func (bs *BaseServer) processRpcRequest(data []byte,connTag string,wrResponse wr rpcError := RpcError(err.Error()) if req.RpcRequestData.IsNoReply() { - wrResponse(processor, connTag,req.RpcRequestData.GetServiceMethod(), req.RpcRequestData.GetSeq(), nil, rpcError) + wrResponse(processor, connTag, req.RpcRequestData.GetServiceMethod(), req.RpcRequestData.GetSeq(), nil, rpcError) } ReleaseRpcRequest(req) } return nil -} \ No newline at end of file +} diff --git a/rpc/natsclient.go b/rpc/natsclient.go index a7ee005..e7030d6 100644 --- a/rpc/natsclient.go +++ b/rpc/natsclient.go @@ -1,90 +1,91 @@ package rpc + import ( + "github.com/duanhf2012/origin/v2/log" "github.com/duanhf2012/origin/v2/network" + "github.com/nats-io/nats.go" "reflect" "time" - "github.com/nats-io/nats.go" - "github.com/duanhf2012/origin/v2/log" ) -//跨结点连接的Client +// NatsClient 跨结点连接的Client type NatsClient struct { - localNodeId string + localNodeId string notifyEventFun NotifyEventFun - + natsConn *nats.Conn - client *Client + client *Client } -func (nc *NatsClient) Start(natsConn *nats.Conn) error{ +func (nc *NatsClient) Start(natsConn *nats.Conn) error { nc.natsConn = natsConn - _,err := nc.natsConn.QueueSubscribe("oc."+nc.localNodeId, "oc",nc.onSubscribe) + _, err := nc.natsConn.QueueSubscribe("oc."+nc.localNodeId, "oc", nc.onSubscribe) return err } -func (nc *NatsClient) onSubscribe(msg *nats.Msg){ +func (nc *NatsClient) onSubscribe(msg *nats.Msg) { //处理消息 nc.client.processRpcResponse(msg.Data) } -func (nc *NatsClient) SetConn(conn *network.TCPConn){ +func (nc *NatsClient) SetConn(conn *network.TCPConn) { } -func (nc *NatsClient) Close(waitDone bool){ +func (nc *NatsClient) Close(waitDone bool) { } -func (nc *NatsClient) Run(){ +func (nc *NatsClient) Run() { } -func (nc *NatsClient) OnClose(){ +func (nc *NatsClient) OnClose() { } -func (rc *NatsClient) Bind(server IServer){ +func (nc *NatsClient) Bind(server IServer) { s := server.(*NatsServer) - rc.natsConn = s.natsConn + nc.natsConn = s.natsConn } -func (rc *NatsClient) Go(nodeId string,timeout time.Duration,rpcHandler IRpcHandler,noReply bool, serviceMethod string, args interface{}, reply interface{}) *Call { +func (nc *NatsClient) Go(nodeId string, timeout time.Duration, rpcHandler IRpcHandler, noReply bool, serviceMethod string, args interface{}, reply interface{}) *Call { _, 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.ErrorAttr("error", err)) call := MakeCall() call.DoError(err) return call } - return rc.client.rawGo(nodeId,rc,timeout,rpcHandler,processor, noReply, 0, serviceMethod, InParam, reply) + return nc.client.rawGo(nodeId, nc, timeout, rpcHandler, processor, noReply, 0, serviceMethod, InParam, reply) } -func (rc *NatsClient) RawGo(nodeId string,timeout time.Duration,rpcHandler IRpcHandler,processor IRpcProcessor, noReply bool, rpcMethodId uint32, serviceMethod string, rawArgs []byte, reply interface{}) *Call { - return rc.client.rawGo(nodeId,rc,timeout,rpcHandler,processor, noReply, rpcMethodId, serviceMethod, rawArgs, reply) +func (nc *NatsClient) RawGo(nodeId string, timeout time.Duration, rpcHandler IRpcHandler, processor IRpcProcessor, noReply bool, rpcMethodId uint32, serviceMethod string, rawArgs []byte, reply interface{}) *Call { + return nc.client.rawGo(nodeId, nc, timeout, rpcHandler, processor, noReply, rpcMethodId, serviceMethod, rawArgs, reply) } -func (rc *NatsClient) AsyncCall(nodeId string,timeout time.Duration,rpcHandler IRpcHandler, serviceMethod string, callback reflect.Value, args interface{}, replyParam interface{},cancelable bool) (CancelRpc,error) { - cancelRpc,err := rc.client.asyncCall(nodeId,rc,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{}, cancelable bool) (CancelRpc, error) { + cancelRpc, err := nc.client.asyncCall(nodeId, nc, timeout, rpcHandler, serviceMethod, callback, args, replyParam, cancelable) if err != nil { callback.Call([]reflect.Value{reflect.ValueOf(replyParam), reflect.ValueOf(err)}) } - return cancelRpc,nil + return cancelRpc, nil } -func (rc *NatsClient) WriteMsg (nodeId string,args ...[]byte) error{ - buff := make([]byte,0,4096) - for _,ar := range args { - buff = append(buff,ar...) +func (nc *NatsClient) WriteMsg(nodeId string, args ...[]byte) error { + buff := make([]byte, 0, 4096) + for _, ar := range args { + buff = append(buff, ar...) } var msg nats.Msg - msg.Subject = "os."+nodeId + msg.Subject = "os." + nodeId msg.Data = buff msg.Header = nats.Header{} - msg.Header.Set("fnode",rc.localNodeId) - return rc.natsConn.PublishMsg(&msg) + msg.Header.Set("fnode", nc.localNodeId) + return nc.natsConn.PublishMsg(&msg) } -func (rc *NatsClient) IsConnected() bool{ - return rc.natsConn!=nil && rc.natsConn.Status() == nats.CONNECTED +func (nc *NatsClient) IsConnected() bool { + return nc.natsConn != nil && nc.natsConn.Status() == nats.CONNECTED } diff --git a/rpc/natsserver.go b/rpc/natsserver.go index 7597f51..79e9abd 100644 --- a/rpc/natsserver.go +++ b/rpc/natsserver.go @@ -1,66 +1,66 @@ package rpc import ( - "github.com/nats-io/nats.go" "github.com/duanhf2012/origin/v2/log" + "github.com/nats-io/nats.go" "time" ) type NatsServer struct { BaseServer natsUrl string - - natsConn *nats.Conn + + natsConn *nats.Conn NoRandomize bool - nodeSubTopic string + nodeSubTopic string compressBytesLen int - notifyEventFun NotifyEventFun + notifyEventFun NotifyEventFun } -const reconnectWait = 3*time.Second -func (ns *NatsServer) Start() error{ +const reconnectWait = 3 * time.Second + +func (ns *NatsServer) Start() error { var err error var options []nats.Option - options = append(options,nats.DisconnectErrHandler(func(nc *nats.Conn, err error) { - log.Error("nats is disconnected",log.String("connUrl",nc.ConnectedUrl())) - ns.notifyEventFun(&NatsConnEvent{IsConnect:false}) + options = append(options, nats.DisconnectErrHandler(func(nc *nats.Conn, err error) { + log.Error("nats is disconnected", log.String("connUrl", nc.ConnectedUrl())) + ns.notifyEventFun(&NatsConnEvent{IsConnect: false}) })) - options = append(options,nats.ConnectHandler(func(nc *nats.Conn) { - log.Info("nats is connected",log.String("connUrl",nc.ConnectedUrl())) - ns.notifyEventFun(&NatsConnEvent{IsConnect:true}) - //log.Error("nats is connect",log.String("connUrl",nc.ConnectedUrl())) + options = append(options, nats.ConnectHandler(func(nc *nats.Conn) { + log.Info("nats is connected", log.String("connUrl", nc.ConnectedUrl())) + ns.notifyEventFun(&NatsConnEvent{IsConnect: true}) })) - options = append(options,nats.ReconnectHandler(func(nc *nats.Conn) { - ns.notifyEventFun(&NatsConnEvent{IsConnect:true}) - log.Info("nats is reconnected",log.String("connUrl",nc.ConnectedUrl())) + options = append(options, nats.ReconnectHandler(func(nc *nats.Conn) { + ns.notifyEventFun(&NatsConnEvent{IsConnect: true}) + log.Info("nats is reconnected", log.String("connUrl", nc.ConnectedUrl())) })) - options = append(options,nats.MaxReconnects(-1)) - options = append(options,nats.ReconnectWait(reconnectWait)) + options = append(options, nats.MaxReconnects(-1)) + options = append(options, nats.ReconnectWait(reconnectWait)) if ns.NoRandomize { - options = append(options,nats.DontRandomize()) + options = append(options, nats.DontRandomize()) } - ns.natsConn,err = nats.Connect(ns.natsUrl,options...) + 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.ErrorAttr("err", err)) return err } //开始订阅 - _,err = ns.natsConn.QueueSubscribe(ns.nodeSubTopic,"os", func(msg *nats.Msg) { - ns.processRpcRequest(msg.Data,msg.Header.Get("fnode"),ns.WriteResponse) + _, err = ns.natsConn.QueueSubscribe(ns.nodeSubTopic, "os", func(msg *nats.Msg) { + ns.processRpcRequest(msg.Data, msg.Header.Get("fnode"), ns.WriteResponse) }) return err } -func (ns *NatsServer) WriteResponse(processor IRpcProcessor, nodeId string,serviceMethod string, seq uint64, reply interface{}, rpcError RpcError){ +func (ns *NatsServer) WriteResponse(processor IRpcProcessor, nodeId string, serviceMethod string, seq uint64, reply interface{}, rpcError RpcError) { var mReply []byte var err error @@ -77,51 +77,51 @@ func (ns *NatsServer) WriteResponse(processor IRpcProcessor, nodeId string,servi defer processor.ReleaseRpcResponse(rpcResponse.RpcResponseData) if err != nil { - log.Error("mashal RpcResponseData failed",log.String("serviceMethod",serviceMethod),log.ErrorAttr("error",err)) + log.Error("marshal RpcResponseData failed", log.String("serviceMethod", serviceMethod), log.ErrorAttr("error", err)) return } - var compressBuff[]byte + var compressBuff []byte bCompress := uint8(0) - if ns.compressBytesLen >0 && len(bytes) >= ns.compressBytesLen { - compressBuff,err = compressor.CompressBlock(bytes) + 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.ErrorAttr("error", err)) return } if len(compressBuff) < len(bytes) { bytes = compressBuff - bCompress = 1<<7 + bCompress = 1 << 7 } } - sendData := make([]byte,0,4096) - byteTypeAndCompress := []byte{uint8(processor.GetProcessorType())|bCompress} - sendData = append(sendData,byteTypeAndCompress...) - sendData = append(sendData,bytes...) - err = ns.natsConn.PublishMsg(&nats.Msg{Subject: "oc."+nodeId, Data: sendData}) + sendData := make([]byte, 0, 4096) + byteTypeAndCompress := []byte{uint8(processor.GetProcessorType()) | bCompress} + sendData = append(sendData, byteTypeAndCompress...) + sendData = append(sendData, bytes...) + err = ns.natsConn.PublishMsg(&nats.Msg{Subject: "oc." + nodeId, Data: sendData}) - if cap(compressBuff) >0 { + if cap(compressBuff) > 0 { compressor.CompressBufferCollection(compressBuff) } 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.ErrorAttr("error", err)) } } -func (ns *NatsServer) Stop(){ +func (ns *NatsServer) Stop() { if ns.natsConn != nil { ns.natsConn.Close() } } -func (ns *NatsServer) initServer(natsUrl string, noRandomize bool,localNodeId string,compressBytesLen int,rpcHandleFinder RpcHandleFinder,notifyEventFun NotifyEventFun){ +func (ns *NatsServer) initServer(natsUrl string, noRandomize bool, localNodeId string, compressBytesLen int, rpcHandleFinder RpcHandleFinder, notifyEventFun NotifyEventFun) { ns.natsUrl = natsUrl ns.NoRandomize = noRandomize ns.localNodeId = localNodeId ns.compressBytesLen = compressBytesLen ns.notifyEventFun = notifyEventFun - ns.initBaseServer(compressBytesLen,rpcHandleFinder) - ns.nodeSubTopic = "os."+localNodeId //服务器 + ns.initBaseServer(compressBytesLen, rpcHandleFinder) + ns.nodeSubTopic = "os." + localNodeId //服务器 } diff --git a/rpc/pbprocessor.go b/rpc/pbprocessor.go index d618d3b..b2799e7 100644 --- a/rpc/pbprocessor.go +++ b/rpc/pbprocessor.go @@ -1,23 +1,23 @@ package rpc import ( + "fmt" "github.com/duanhf2012/origin/v2/util/sync" "google.golang.org/protobuf/proto" - "fmt" ) type PBProcessor struct { } -var rpcPbResponseDataPool =sync.NewPool(make(chan interface{},10240), func()interface{}{ +var rpcPbResponseDataPool = sync.NewPool(make(chan interface{}, 10240), func() interface{} { return &PBRpcResponseData{} }) -var rpcPbRequestDataPool =sync.NewPool(make(chan interface{},10240), func()interface{}{ +var rpcPbRequestDataPool = sync.NewPool(make(chan interface{}, 10240), func() interface{} { return &PBRpcRequestData{} }) -func (slf *PBRpcRequestData) MakeRequest(seq uint64,rpcMethodId uint32,serviceMethod string,noReply bool,inParam []byte) *PBRpcRequestData{ +func (slf *PBRpcRequestData) MakeRequest(seq uint64, rpcMethodId uint32, serviceMethod string, noReply bool, inParam []byte) *PBRpcRequestData { slf.Seq = seq slf.RpcMethodId = rpcMethodId slf.ServiceMethod = serviceMethod @@ -27,8 +27,7 @@ func (slf *PBRpcRequestData) MakeRequest(seq uint64,rpcMethodId uint32,serviceMe return slf } - -func (slf *PBRpcResponseData) MakeRespone(seq uint64,err RpcError,reply []byte) *PBRpcResponseData{ +func (slf *PBRpcResponseData) MakeResponse(seq uint64, err RpcError, reply []byte) *PBRpcResponseData { slf.Seq = seq slf.Error = err.Error() slf.Reply = reply @@ -36,61 +35,61 @@ func (slf *PBRpcResponseData) MakeRespone(seq uint64,err RpcError,reply []byte) return slf } -func (slf *PBProcessor) Marshal(v interface{}) ([]byte, error){ +func (slf *PBProcessor) Marshal(v interface{}) ([]byte, error) { return proto.Marshal(v.(proto.Message)) } -func (slf *PBProcessor) Unmarshal(data []byte, msg interface{}) error{ - protoMsg,ok := msg.(proto.Message) +func (slf *PBProcessor) Unmarshal(data []byte, msg interface{}) error { + protoMsg, ok := msg.(proto.Message) if ok == false { - return fmt.Errorf("%+v is not of proto.Message type",msg) + return fmt.Errorf("%+v is not of proto.Message type", msg) } return proto.Unmarshal(data, protoMsg) } -func (slf *PBProcessor) MakeRpcRequest(seq uint64,rpcMethodId uint32,serviceMethod string,noReply bool,inParam []byte) IRpcRequestData{ +func (slf *PBProcessor) MakeRpcRequest(seq uint64, rpcMethodId uint32, serviceMethod string, noReply bool, inParam []byte) IRpcRequestData { pGogoPbRpcRequestData := rpcPbRequestDataPool.Get().(*PBRpcRequestData) - pGogoPbRpcRequestData.MakeRequest(seq,rpcMethodId,serviceMethod,noReply,inParam) + pGogoPbRpcRequestData.MakeRequest(seq, rpcMethodId, serviceMethod, noReply, inParam) return pGogoPbRpcRequestData } -func (slf *PBProcessor) MakeRpcResponse(seq uint64,err RpcError,reply []byte) IRpcResponseData { +func (slf *PBProcessor) MakeRpcResponse(seq uint64, err RpcError, reply []byte) IRpcResponseData { pPBRpcResponseData := rpcPbResponseDataPool.Get().(*PBRpcResponseData) - pPBRpcResponseData.MakeRespone(seq,err,reply) + pPBRpcResponseData.MakeResponse(seq, err, reply) return pPBRpcResponseData } -func (slf *PBProcessor) ReleaseRpcRequest(rpcRequestData IRpcRequestData){ +func (slf *PBProcessor) ReleaseRpcRequest(rpcRequestData IRpcRequestData) { rpcPbRequestDataPool.Put(rpcRequestData) } -func (slf *PBProcessor) ReleaseRpcResponse(rpcResponseData IRpcResponseData){ +func (slf *PBProcessor) ReleaseRpcResponse(rpcResponseData IRpcResponseData) { rpcPbResponseDataPool.Put(rpcResponseData) } func (slf *PBProcessor) IsParse(param interface{}) bool { - _,ok := param.(proto.Message) + _, ok := param.(proto.Message) return ok } -func (slf *PBProcessor) GetProcessorType() RpcProcessorType{ +func (slf *PBProcessor) GetProcessorType() RpcProcessorType { return RpcProcessorPB } -func (slf *PBProcessor) Clone(src interface{}) (interface{},error){ - srcMsg,ok := src.(proto.Message) +func (slf *PBProcessor) Clone(src interface{}) (interface{}, error) { + srcMsg, ok := src.(proto.Message) if ok == false { - return nil,fmt.Errorf("param is not of proto.message type") + return nil, fmt.Errorf("param is not of proto.message type") } - return proto.Clone(srcMsg),nil + return proto.Clone(srcMsg), nil } -func (slf *PBRpcRequestData) IsNoReply() bool{ +func (slf *PBRpcRequestData) IsNoReply() bool { return slf.GetNoReply() } -func (slf *PBRpcResponseData) GetErr() *RpcError { +func (slf *PBRpcResponseData) GetErr() *RpcError { if slf.GetError() == "" { return nil } @@ -98,9 +97,3 @@ func (slf *PBRpcResponseData) GetErr() *RpcError { err := RpcError(slf.GetError()) return &err } - - - - - - diff --git a/rpc/rclient.go b/rpc/rclient.go index 0f459a5..ff85a95 100644 --- a/rpc/rclient.go +++ b/rpc/rclient.go @@ -11,7 +11,7 @@ import ( "time" ) -//跨结点连接的Client +// RClient 跨结点连接的Client type RClient struct { selfClient *Client network.TCPClient @@ -27,7 +27,7 @@ func (rc *RClient) IsConnected() bool { return rc.conn != nil && rc.conn.IsConnected() == true } -func (rc *RClient) GetConn() *network.TCPConn{ +func (rc *RClient) GetConn() *network.TCPConn { rc.Lock() conn := rc.conn rc.Unlock() @@ -35,40 +35,40 @@ func (rc *RClient) GetConn() *network.TCPConn{ return conn } -func (rc *RClient) SetConn(conn *network.TCPConn){ +func (rc *RClient) SetConn(conn *network.TCPConn) { rc.Lock() rc.conn = conn rc.Unlock() } -func (rc *RClient) WriteMsg (nodeId string,args ...[]byte) error{ +func (rc *RClient) WriteMsg(nodeId string, args ...[]byte) error { return rc.conn.WriteMsg(args...) } -func (rc *RClient) Go(nodeId string,timeout time.Duration,rpcHandler IRpcHandler,noReply bool, serviceMethod string, args interface{}, reply interface{}) *Call { +func (rc *RClient) Go(nodeId string, timeout time.Duration, rpcHandler IRpcHandler, noReply bool, serviceMethod string, args interface{}, reply interface{}) *Call { _, 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.ErrorAttr("error", err)) call := MakeCall() call.DoError(err) return call } - return rc.selfClient.rawGo(nodeId,rc,timeout,rpcHandler,processor, noReply, 0, serviceMethod, InParam, reply) + return rc.selfClient.rawGo(nodeId, rc, timeout, rpcHandler, processor, noReply, 0, serviceMethod, InParam, reply) } -func (rc *RClient) RawGo(nodeId string,timeout time.Duration,rpcHandler IRpcHandler,processor IRpcProcessor, noReply bool, rpcMethodId uint32, serviceMethod string, rawArgs []byte, reply interface{}) *Call { - return rc.selfClient.rawGo(nodeId,rc,timeout,rpcHandler,processor, noReply, rpcMethodId, serviceMethod, rawArgs, reply) +func (rc *RClient) RawGo(nodeId string, timeout time.Duration, rpcHandler IRpcHandler, processor IRpcProcessor, noReply bool, rpcMethodId uint32, serviceMethod string, rawArgs []byte, reply interface{}) *Call { + 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{}, cancelable bool) (CancelRpc, error) { + cancelRpc, err := rc.selfClient.asyncCall(nodeId, rc, timeout, rpcHandler, serviceMethod, callback, args, replyParam, cancelable) if err != nil { callback.Call([]reflect.Value{reflect.ValueOf(replyParam), reflect.ValueOf(err)}) } - return cancelRpc,nil + return cancelRpc, nil } func (rc *RClient) Run() { @@ -77,19 +77,19 @@ func (rc *RClient) Run() { buf := make([]byte, 4096) l := runtime.Stack(buf, false) errString := fmt.Sprint(r) - log.Dump(string(buf[:l]),log.String("error",errString)) + log.Dump(string(buf[:l]), log.String("error", errString)) } }() var eventData RpcConnEvent eventData.IsConnect = true - eventData.NodeId = rc.selfClient.GetTargetNodeId() + eventData.NodeId = rc.selfClient.GetTargetNodeId() rc.notifyEventFun(&eventData) - + 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.ErrorAttr("error", err)) return } @@ -108,13 +108,13 @@ func (rc *RClient) OnClose() { rc.notifyEventFun(&connEvent) } -func NewRClient(targetNodeId string, addr string, maxRpcParamLen uint32,compressBytesLen int,callSet *CallSet,notifyEventFun NotifyEventFun) *Client{ +func NewRClient(targetNodeId string, addr string, maxRpcParamLen uint32, compressBytesLen int, callSet *CallSet, notifyEventFun NotifyEventFun) *Client { client := &Client{} client.clientId = atomic.AddUint32(&clientSeq, 1) client.targetNodeId = targetNodeId client.compressBytesLen = compressBytesLen - c:= &RClient{} + c := &RClient{} c.selfClient = client c.Addr = addr c.ConnectInterval = DefaultConnectInterval @@ -140,13 +140,11 @@ func NewRClient(targetNodeId string, addr string, maxRpcParamLen uint32,compress return client } - func (rc *RClient) Close(waitDone bool) { rc.TCPClient.Close(waitDone) rc.selfClient.cleanPending() } +func (rc *RClient) Bind(server IServer) { -func (rc *RClient) Bind(server IServer){ - -} \ No newline at end of file +} diff --git a/rpc/rpchandler.go b/rpc/rpchandler.go index fedf455..e56c582 100644 --- a/rpc/rpchandler.go +++ b/rpc/rpchandler.go @@ -3,20 +3,21 @@ package rpc import ( "errors" "fmt" + "github.com/duanhf2012/origin/v2/event" "github.com/duanhf2012/origin/v2/log" "reflect" "runtime" "strings" + "time" "unicode" "unicode/utf8" - "github.com/duanhf2012/origin/v2/event" - "time" ) const maxClusterNode int = 32 -type FuncRpcClient func(nodeId string, serviceMethod string,filterRetire bool, client []*Client) (error, []*Client) +type FuncRpcClient func(nodeId string, serviceMethod string, filterRetire bool, client []*Client) (error, []*Client) type FuncRpcServer func() IServer + const NodeIdNull = "" var nilError = reflect.Zero(reflect.TypeOf((*error)(nil)).Elem()) @@ -47,7 +48,7 @@ type RpcMethodInfo struct { rpcProcessorType RpcProcessorType } -type RawRpcCallBack func(rawData []byte) +type RawRpcCallBack func(rawData []byte) type IRpcHandlerChannel interface { PushRpcResponse(call *Call) error @@ -66,7 +67,7 @@ type RpcHandler struct { //pClientList []*Client } -//type TriggerRpcConnEvent func(bConnect bool, clientSeq uint32, nodeId string) +// NotifyEventToAllService type TriggerRpcConnEvent func(bConnect bool, clientSeq uint32, nodeId string) type NotifyEventToAllService func(event event.IEvent) type INodeConnListener interface { @@ -85,7 +86,8 @@ type IDiscoveryServiceListener interface { } type CancelRpc func() -func emptyCancelRpc(){} + +func emptyCancelRpc() {} type IRpcHandler interface { IRpcHandlerChannel @@ -94,17 +96,17 @@ type IRpcHandler interface { GetRpcHandler() IRpcHandler HandlerRpcRequest(request *RpcRequest) HandlerRpcResponseCB(call *Call) - CallMethod(client *Client,ServiceMethod string, param interface{},callBack reflect.Value, reply interface{}) error + CallMethod(client *Client, ServiceMethod string, param interface{}, callBack reflect.Value, reply interface{}) error Call(serviceMethod string, args interface{}, reply interface{}) error CallNode(nodeId string, serviceMethod string, args interface{}, reply interface{}) error AsyncCall(serviceMethod string, args interface{}, callback interface{}) error AsyncCallNode(nodeId string, serviceMethod string, args interface{}, callback interface{}) error - CallWithTimeout(timeout time.Duration,serviceMethod string, args interface{}, reply interface{}) error - CallNodeWithTimeout(timeout time.Duration,nodeId string, serviceMethod string, args interface{}, reply interface{}) error - AsyncCallWithTimeout(timeout time.Duration,serviceMethod string, args interface{}, callback interface{}) (CancelRpc,error) - AsyncCallNodeWithTimeout(timeout time.Duration,nodeId string, serviceMethod string, args interface{}, callback interface{}) (CancelRpc,error) + CallWithTimeout(timeout time.Duration, serviceMethod string, args interface{}, reply interface{}) error + CallNodeWithTimeout(timeout time.Duration, nodeId string, serviceMethod string, args interface{}, reply interface{}) error + AsyncCallWithTimeout(timeout time.Duration, serviceMethod string, args interface{}, callback interface{}) (CancelRpc, error) + AsyncCallNodeWithTimeout(timeout time.Duration, nodeId string, serviceMethod string, args interface{}, callback interface{}) (CancelRpc, error) Go(serviceMethod string, args interface{}) error GoNode(nodeId string, serviceMethod string, args interface{}) error @@ -221,7 +223,7 @@ func (handler *RpcHandler) HandlerRpcResponseCB(call *Call) { buf := make([]byte, 4096) l := runtime.Stack(buf, false) errString := fmt.Sprint(r) - log.Dump(string(buf[:l]),log.String("error",errString)) + log.Dump(string(buf[:l]), log.String("error", errString)) } }() @@ -243,7 +245,7 @@ func (handler *RpcHandler) HandlerRpcRequest(request *RpcRequest) { buf := make([]byte, 4096) l := runtime.Stack(buf, false) errString := fmt.Sprint(r) - log.Dump(string(buf[:l]),log.String("error",errString)) + log.Dump(string(buf[:l]), log.String("error", errString)) rpcErr := RpcError("call error : core dumps") if request.requestHandle != nil { request.requestHandle(nil, rpcErr) @@ -256,12 +258,12 @@ func (handler *RpcHandler) HandlerRpcRequest(request *RpcRequest) { if rawRpcId > 0 { v, ok := handler.mapRawFunctions[rawRpcId] if ok == false { - log.Error("RpcHandler cannot find request rpc id",log.Uint32("rawRpcId",rawRpcId)) + log.Error("RpcHandler cannot find request rpc id", log.Uint32("rawRpcId", rawRpcId)) return } - rawData,ok := request.inParam.([]byte) + rawData, ok := request.inParam.([]byte) if ok == false { - log.Error("RpcHandler cannot convert",log.String("RpcHandlerName",handler.rpcHandler.GetName()),log.Uint32("rawRpcId",rawRpcId)) + log.Error("RpcHandler cannot convert", log.String("RpcHandlerName", handler.rpcHandler.GetName()), log.Uint32("rawRpcId", rawRpcId)) return } @@ -273,7 +275,7 @@ func (handler *RpcHandler) HandlerRpcRequest(request *RpcRequest) { v, ok := handler.mapFunctions[request.RpcRequestData.GetServiceMethod()] if ok == false { err := "RpcHandler " + handler.rpcHandler.GetName() + " cannot find " + request.RpcRequestData.GetServiceMethod() - log.Error("HandlerRpcRequest cannot find serviceMethod",log.String("RpcHandlerName",handler.rpcHandler.GetName()),log.String("serviceMethod",request.RpcRequestData.GetServiceMethod())) + log.Error("HandlerRpcRequest cannot find serviceMethod", log.String("RpcHandlerName", handler.rpcHandler.GetName()), log.String("serviceMethod", request.RpcRequestData.GetServiceMethod())) if request.requestHandle != nil { request.requestHandle(nil, RpcError(err)) } @@ -304,29 +306,29 @@ func (handler *RpcHandler) HandlerRpcRequest(request *RpcRequest) { paramList = append(paramList, oParam) //输出参数 } else if request.requestHandle != nil && v.hasResponder == false { //调用方有返回值,但被调用函数没有返回参数 rErr := "Call Rpc " + request.RpcRequestData.GetServiceMethod() + " without return parameter!" - log.Error("call serviceMethod without return parameter",log.String("serviceMethod",request.RpcRequestData.GetServiceMethod())) + log.Error("call serviceMethod without return parameter", log.String("serviceMethod", request.RpcRequestData.GetServiceMethod())) request.requestHandle(nil, RpcError(rErr)) return } - requestHanle := request.requestHandle + requestHandle := request.requestHandle returnValues := v.method.Func.Call(paramList) errInter := returnValues[0].Interface() if errInter != nil { err = errInter.(error) } - if v.hasResponder == false && requestHanle != nil { - requestHanle(oParam.Interface(), ConvertError(err)) + if v.hasResponder == false && requestHandle != nil { + requestHandle(oParam.Interface(), ConvertError(err)) } } -func (handler *RpcHandler) CallMethod(client *Client,ServiceMethod string, param interface{},callBack reflect.Value, reply interface{}) error { +func (handler *RpcHandler) CallMethod(client *Client, ServiceMethod string, param interface{}, callBack reflect.Value, reply interface{}) error { var err error v, ok := handler.mapFunctions[ServiceMethod] if ok == false { err = errors.New("RpcHandler " + handler.rpcHandler.GetName() + " cannot find" + ServiceMethod) - log.Error("CallMethod cannot find serviceMethod",log.String("rpcHandlerName",handler.rpcHandler.GetName()),log.String("serviceMethod",ServiceMethod)) + log.Error("CallMethod cannot find serviceMethod", log.String("rpcHandlerName", handler.rpcHandler.GetName()), log.String("serviceMethod", ServiceMethod)) return err } @@ -347,31 +349,31 @@ func (handler *RpcHandler) CallMethod(client *Client,ServiceMethod string, param //有返回值时 if reply != nil { //如果是Call同步调用 - hander :=func(Returns interface{}, Err RpcError) { + hd := func(Returns interface{}, Err RpcError) { rpcCall := client.RemovePending(callSeq) if rpcCall == nil { - log.Error("cannot find call seq",log.Uint64("seq",callSeq)) + log.Error("cannot find call seq", log.Uint64("seq", callSeq)) return } //解析数据 - if len(Err)!=0 { + if len(Err) != 0 { rpcCall.Err = Err - }else if Returns != nil { + } else if Returns != nil { _, processor := GetProcessorType(Returns) var bytes []byte - bytes,rpcCall.Err = processor.Marshal(Returns) + bytes, rpcCall.Err = processor.Marshal(Returns) if rpcCall.Err == nil { - rpcCall.Err = processor.Unmarshal(bytes,reply) + rpcCall.Err = processor.Unmarshal(bytes, reply) } } //如果找不到,说明已经超时 rpcCall.Reply = reply - rpcCall.done<-rpcCall + rpcCall.done <- rpcCall } - paramList = append(paramList, reflect.ValueOf(hander)) - }else{//无返回值时,是一个requestHandlerNull空回调 + paramList = append(paramList, reflect.ValueOf(hd)) + } else { //无返回值时,是一个requestHandlerNull空回调 paramList = append(paramList, callBack) } paramList = append(paramList, reflect.ValueOf(param)) @@ -381,11 +383,11 @@ func (handler *RpcHandler) CallMethod(client *Client,ServiceMethod string, param //判断返回值是否错误,有错误时则回调 errInter := returnValues[0].Interface() - if errInter != nil && callBack!=requestHandlerNull{ + if errInter != nil && callBack != requestHandlerNull { err = errInter.(error) callBack.Call([]reflect.Value{reflect.ValueOf(reply), reflect.ValueOf(err)}) } - }else{ + } else { paramList = append(paramList, reflect.ValueOf(handler.GetRpcHandler())) //接受者 paramList = append(paramList, reflect.ValueOf(param)) @@ -394,7 +396,7 @@ func (handler *RpcHandler) CallMethod(client *Client,ServiceMethod string, param //不带返回值参数的RPC函数 if reply == nil { paramList = append(paramList, reflect.New(v.outParamValue.Type().Elem())) - }else{ + } else { //带返回值参数的RPC函数 paramList = append(paramList, reflect.ValueOf(reply)) //输出参数 } @@ -411,14 +413,14 @@ func (handler *RpcHandler) CallMethod(client *Client,ServiceMethod string, param valErr = reflect.ValueOf(err) } - callBack.Call([]reflect.Value{reflect.ValueOf(reply),valErr }) + callBack.Call([]reflect.Value{reflect.ValueOf(reply), valErr}) } } rpcCall := client.FindPending(callSeq) - if rpcCall!=nil { - err = rpcCall.Done().Err - if rpcCall.callback!= nil { + if rpcCall != nil { + err = rpcCall.Done().Err + if rpcCall.callback != nil { valErr := nilError if rpcCall.Err != nil { valErr = reflect.ValueOf(rpcCall.Err) @@ -433,25 +435,25 @@ func (handler *RpcHandler) CallMethod(client *Client,ServiceMethod string, param } func (handler *RpcHandler) goRpc(processor IRpcProcessor, bCast bool, nodeId string, serviceMethod string, args interface{}) error { - pClientList :=make([]*Client,0,maxClusterNode) - err, pClientList := handler.funcRpcClient(nodeId, serviceMethod,false, pClientList) + pClientList := make([]*Client, 0, maxClusterNode) + 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.ErrorAttr("error", err)) } else { - log.Error("cannot find serviceMethod",log.String("serviceMethod",serviceMethod)) + log.Error("cannot find serviceMethod", log.String("serviceMethod", serviceMethod)) } return err } if len(pClientList) > 1 && bCast == false { - log.Error("cannot call serviceMethod more then 1 node",log.String("serviceMethod",serviceMethod)) + log.Error("cannot call serviceMethod more then 1 node", log.String("serviceMethod", serviceMethod)) return errors.New("cannot call more then 1 node") } //2.rpcClient调用 for i := 0; i < len(pClientList); i++ { - pCall := pClientList[i].Go(pClientList[i].GetTargetNodeId(),DefaultRpcTimeout,handler.rpcHandler,true, serviceMethod, args, nil) + pCall := pClientList[i].Go(pClientList[i].GetTargetNodeId(), DefaultRpcTimeout, handler.rpcHandler, true, serviceMethod, args, nil) if pCall.Err != nil { err = pCall.Err } @@ -462,23 +464,23 @@ func (handler *RpcHandler) goRpc(processor IRpcProcessor, bCast bool, nodeId str return err } -func (handler *RpcHandler) callRpc(timeout time.Duration,nodeId string, serviceMethod string, args interface{}, reply interface{}) error { - pClientList :=make([]*Client,0,maxClusterNode) - err, pClientList := handler.funcRpcClient(nodeId, serviceMethod,false, pClientList) +func (handler *RpcHandler) callRpc(timeout time.Duration, nodeId string, serviceMethod string, args interface{}, reply interface{}) error { + 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.ErrorAttr("error", err)) return err } else if len(pClientList) <= 0 { err = errors.New("Call serviceMethod is error:cannot find " + serviceMethod) - log.Error("cannot find serviceMethod",log.String("serviceMethod",serviceMethod)) + log.Error("cannot find serviceMethod", log.String("serviceMethod", serviceMethod)) return err } else if len(pClientList) > 1 { - log.Error("Cannot call more then 1 node!",log.String("serviceMethod",serviceMethod)) + log.Error("Cannot call more then 1 node!", log.String("serviceMethod", serviceMethod)) return errors.New("cannot call more then 1 node") } pClient := pClientList[0] - pCall := pClient.Go(pClient.GetTargetNodeId(),timeout,handler.rpcHandler,false, serviceMethod, args, reply) + pCall := pClient.Go(pClient.GetTargetNodeId(), timeout, handler.rpcHandler, false, serviceMethod, args, reply) err = pCall.Done().Err pClient.RemovePending(pCall.Seq) @@ -486,81 +488,81 @@ func (handler *RpcHandler) callRpc(timeout time.Duration,nodeId string, serviceM return err } -func (handler *RpcHandler) asyncCallRpc(timeout time.Duration,nodeId string, serviceMethod string, args interface{}, callback interface{}) (CancelRpc,error) { +func (handler *RpcHandler) asyncCallRpc(timeout time.Duration, nodeId string, serviceMethod string, args interface{}, callback interface{}) (CancelRpc, error) { fVal := reflect.ValueOf(callback) if fVal.Kind() != reflect.Func { err := errors.New("call " + serviceMethod + " input callback param is error!") - log.Error("input callback param is error",log.String("serviceMethod",serviceMethod)) - return emptyCancelRpc,err + log.Error("input callback param is error", log.String("serviceMethod", serviceMethod)) + return emptyCancelRpc, err } if fVal.Type().NumIn() != 2 { err := errors.New("call " + serviceMethod + " callback param function is error!") - log.Error("callback param function is error",log.String("serviceMethod",serviceMethod)) - return emptyCancelRpc,err + log.Error("callback param function is error", log.String("serviceMethod", serviceMethod)) + return emptyCancelRpc, err } if fVal.Type().In(0).Kind() != reflect.Ptr || fVal.Type().In(1).String() != "error" { err := errors.New("call " + serviceMethod + " callback param function is error!") - log.Error("callback param function is error",log.String("serviceMethod",serviceMethod)) - return emptyCancelRpc,err + log.Error("callback param function is error", log.String("serviceMethod", serviceMethod)) + return emptyCancelRpc, err } reply := reflect.New(fVal.Type().In(0).Elem()).Interface() - pClientList :=make([]*Client,0,1) - err, pClientList := handler.funcRpcClient(nodeId, serviceMethod,false, pClientList[:]) + pClientList := make([]*Client, 0, 1) + err, pClientList := handler.funcRpcClient(nodeId, serviceMethod, false, pClientList[:]) if len(pClientList) == 0 || err != nil { if err == nil { - if nodeId != NodeIdNull { - err = fmt.Errorf("cannot find %s from nodeId %d",serviceMethod,nodeId) - }else { - err = fmt.Errorf("No %s service found in the origin network",serviceMethod) + if nodeId != NodeIdNull { + err = fmt.Errorf("cannot find %s from nodeId %s", serviceMethod, nodeId) + } else { + err = fmt.Errorf("no %s service found in the origin network", serviceMethod) } } fVal.Call([]reflect.Value{reflect.ValueOf(reply), reflect.ValueOf(err)}) - log.Error("cannot find serviceMethod from node",log.String("serviceMethod",serviceMethod),log.String("nodeId",nodeId)) - return emptyCancelRpc,nil + log.Error("cannot find serviceMethod from node", log.String("serviceMethod", serviceMethod), log.String("nodeId", nodeId)) + return emptyCancelRpc, nil } if len(pClientList) > 1 { err := errors.New("cannot call more then 1 node") fVal.Call([]reflect.Value{reflect.ValueOf(reply), reflect.ValueOf(err)}) - log.Error("cannot call more then 1 node",log.String("serviceMethod",serviceMethod)) - return emptyCancelRpc,nil + log.Error("cannot call more then 1 node", log.String("serviceMethod", serviceMethod)) + return emptyCancelRpc, nil } //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, false) } func (handler *RpcHandler) GetName() string { return handler.rpcHandler.GetName() } -func (handler *RpcHandler) CallWithTimeout(timeout time.Duration,serviceMethod string, args interface{}, reply interface{}) error { - return handler.callRpc(timeout,NodeIdNull, serviceMethod, args, reply) +func (handler *RpcHandler) CallWithTimeout(timeout time.Duration, serviceMethod string, args interface{}, reply interface{}) error { + return handler.callRpc(timeout, NodeIdNull, serviceMethod, args, reply) } -func (handler *RpcHandler) CallNodeWithTimeout(timeout time.Duration,nodeId string, serviceMethod string, args interface{}, reply interface{}) error{ - return handler.callRpc(timeout,nodeId, serviceMethod, args, reply) +func (handler *RpcHandler) CallNodeWithTimeout(timeout time.Duration, nodeId string, serviceMethod string, args interface{}, reply interface{}) error { + return handler.callRpc(timeout, nodeId, serviceMethod, args, reply) } -func (handler *RpcHandler) AsyncCallWithTimeout(timeout time.Duration,serviceMethod string, args interface{}, callback interface{}) (CancelRpc,error){ - return handler.asyncCallRpc(timeout,NodeIdNull, serviceMethod, args, callback) +func (handler *RpcHandler) AsyncCallWithTimeout(timeout time.Duration, serviceMethod string, args interface{}, callback interface{}) (CancelRpc, error) { + return handler.asyncCallRpc(timeout, NodeIdNull, serviceMethod, args, callback) } -func (handler *RpcHandler) AsyncCallNodeWithTimeout(timeout time.Duration,nodeId string, serviceMethod string, args interface{}, callback interface{}) (CancelRpc,error){ - return handler.asyncCallRpc(timeout,nodeId, serviceMethod, args, callback) +func (handler *RpcHandler) AsyncCallNodeWithTimeout(timeout time.Duration, nodeId string, serviceMethod string, args interface{}, callback interface{}) (CancelRpc, error) { + return handler.asyncCallRpc(timeout, nodeId, serviceMethod, args, callback) } func (handler *RpcHandler) AsyncCall(serviceMethod string, args interface{}, callback interface{}) error { - _,err := handler.asyncCallRpc(DefaultRpcTimeout,NodeIdNull, serviceMethod, args, callback) + _, err := handler.asyncCallRpc(DefaultRpcTimeout, NodeIdNull, serviceMethod, args, callback) return err } func (handler *RpcHandler) Call(serviceMethod string, args interface{}, reply interface{}) error { - return handler.callRpc(DefaultRpcTimeout,NodeIdNull, serviceMethod, args, reply) + return handler.callRpc(DefaultRpcTimeout, NodeIdNull, serviceMethod, args, reply) } func (handler *RpcHandler) Go(serviceMethod string, args interface{}) error { @@ -568,13 +570,13 @@ func (handler *RpcHandler) Go(serviceMethod string, args interface{}) error { } func (handler *RpcHandler) AsyncCallNode(nodeId string, serviceMethod string, args interface{}, callback interface{}) error { - _,err:= handler.asyncCallRpc(DefaultRpcTimeout,nodeId, serviceMethod, args, callback) + _, err := handler.asyncCallRpc(DefaultRpcTimeout, nodeId, serviceMethod, args, callback) return err } func (handler *RpcHandler) CallNode(nodeId string, serviceMethod string, args interface{}, reply interface{}) error { - return handler.callRpc(DefaultRpcTimeout,nodeId, serviceMethod, args, reply) + return handler.callRpc(DefaultRpcTimeout, nodeId, serviceMethod, args, reply) } func (handler *RpcHandler) GoNode(nodeId string, serviceMethod string, args interface{}) error { @@ -587,15 +589,15 @@ func (handler *RpcHandler) CastGo(serviceMethod string, args interface{}) error func (handler *RpcHandler) RawGoNode(rpcProcessorType RpcProcessorType, nodeId string, rpcMethodId uint32, serviceName string, rawArgs []byte) error { processor := GetProcessor(uint8(rpcProcessorType)) - pClientList := make([]*Client,0,1) - err, pClientList := handler.funcRpcClient(nodeId, serviceName,false, pClientList) + 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.ErrorAttr("error", err)) return err } if len(pClientList) > 1 { err := errors.New("cannot call more then 1 node") - log.Error("cannot call more then 1 node",log.String("serviceName",serviceName)) + log.Error("cannot call more then 1 node", log.String("serviceName", serviceName)) return err } @@ -603,7 +605,7 @@ func (handler *RpcHandler) RawGoNode(rpcProcessorType RpcProcessorType, nodeId s //如果调用本结点服务 for i := 0; i < len(pClientList); i++ { //跨node调用 - pCall := pClientList[i].RawGo(pClientList[i].GetTargetNodeId(),DefaultRpcTimeout,handler.rpcHandler,processor, true, rpcMethodId, serviceName, rawArgs, nil) + pCall := pClientList[i].RawGo(pClientList[i].GetTargetNodeId(), DefaultRpcTimeout, handler.rpcHandler, processor, true, rpcMethodId, serviceName, rawArgs, nil) if pCall.Err != nil { err = pCall.Err } @@ -621,7 +623,7 @@ func (handler *RpcHandler) RegRawRpc(rpcMethodId uint32, rawRpcCB RawRpcCallBack func (handler *RpcHandler) UnmarshalInParam(rpcProcessor IRpcProcessor, serviceMethod string, rawRpcMethodId uint32, inParam []byte) (interface{}, error) { if rawRpcMethodId > 0 { - return inParam,nil + return inParam, nil } v, ok := handler.mapFunctions[serviceMethod] @@ -635,7 +637,6 @@ func (handler *RpcHandler) UnmarshalInParam(rpcProcessor IRpcProcessor, serviceM return param, err } - -func (handler *RpcHandler) GetRpcServer() FuncRpcServer{ +func (handler *RpcHandler) GetRpcServer() FuncRpcServer { return handler.funcRpcServer } diff --git a/rpc/server.go b/rpc/server.go index fb4fb06..0055960 100644 --- a/rpc/server.go +++ b/rpc/server.go @@ -7,17 +7,18 @@ import ( "math" "net" "reflect" + "runtime" "strings" "time" - "runtime" ) -const Default_ReadWriteDeadline = 15*time.Second +const Default_ReadWriteDeadline = 15 * time.Second + type RpcProcessorType uint8 const ( - RpcProcessorJson RpcProcessorType = 0 - RpcProcessorPB RpcProcessorType = 1 + RpcProcessorJson RpcProcessorType = 0 + RpcProcessorPB RpcProcessorType = 1 ) var arrayProcessor = []IRpcProcessor{&JsonProcessor{}, &PBProcessor{}} @@ -28,20 +29,20 @@ type IServer interface { Start() error Stop() - 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) + 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) } -type writeResponse func(processor IRpcProcessor,connTag string, serviceMethod string, seq uint64, reply interface{}, rpcError RpcError) +type writeResponse func(processor IRpcProcessor, connTag string, serviceMethod string, seq uint64, reply interface{}, rpcError RpcError) type Server struct { BaseServer - functions map[interface{}]interface{} - rpcServer *network.TCPServer + functions map[interface{}]interface{} + rpcServer *network.TCPServer - listenAddr string + listenAddr string maxRpcParamLen uint32 } @@ -73,15 +74,15 @@ func GetProcessor(processorType uint8) IRpcProcessor { return arrayProcessor[processorType] } -func (server *Server) Init(listenAddr string, maxRpcParamLen uint32,compressBytesLen int,rpcHandleFinder RpcHandleFinder) { - server.initBaseServer(compressBytesLen,rpcHandleFinder) +func (server *Server) Init(listenAddr string, maxRpcParamLen uint32, compressBytesLen int, rpcHandleFinder RpcHandleFinder) { + server.initBaseServer(compressBytesLen, rpcHandleFinder) server.listenAddr = listenAddr server.maxRpcParamLen = maxRpcParamLen server.rpcServer = &network.TCPServer{} } -func (server *Server) Start() error{ +func (server *Server) Start() error { splitAddr := strings.Split(server.listenAddr, ":") if len(splitAddr) != 2 { return fmt.Errorf("listen addr is failed,listenAddr:%s", server.listenAddr) @@ -89,7 +90,6 @@ func (server *Server) Start() error{ server.rpcServer.Addr = ":" + splitAddr[1] server.rpcServer.MinMsgLen = 2 - server.compressBytesLen = server.compressBytesLen if server.maxRpcParamLen > 0 { server.rpcServer.MaxMsgLen = server.maxRpcParamLen } else { @@ -107,7 +107,7 @@ func (server *Server) Start() error{ return server.rpcServer.Start() } -func (server *Server) Stop(){ +func (server *Server) Stop() { server.rpcServer.Close() } @@ -130,32 +130,32 @@ func (agent *RpcAgent) WriteResponse(processor IRpcProcessor, connTag string, se defer processor.ReleaseRpcResponse(rpcResponse.RpcResponseData) if errM != nil { - log.Error("mashal RpcResponseData failed",log.String("serviceMethod",serviceMethod),log.ErrorAttr("error",errM)) + log.Error("marshal RpcResponseData failed", log.String("serviceMethod", serviceMethod), log.ErrorAttr("error", errM)) return } - var compressBuff[]byte + var compressBuff []byte bCompress := uint8(0) - if agent.rpcServer.compressBytesLen >0 && len(bytes) >= agent.rpcServer.compressBytesLen { + if agent.rpcServer.compressBytesLen > 0 && len(bytes) >= agent.rpcServer.compressBytesLen { var cErr error - compressBuff,cErr = compressor.CompressBlock(bytes) + 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.ErrorAttr("error", cErr)) return } if len(compressBuff) < len(bytes) { bytes = compressBuff - bCompress = 1<<7 + bCompress = 1 << 7 } } - errM = agent.conn.WriteMsg([]byte{uint8(processor.GetProcessorType())|bCompress}, bytes) - if cap(compressBuff) >0 { + errM = agent.conn.WriteMsg([]byte{uint8(processor.GetProcessorType()) | bCompress}, bytes) + if cap(compressBuff) > 0 { 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.ErrorAttr("error", errM)) } } @@ -163,27 +163,29 @@ func (agent *RpcAgent) Run() { defer func() { if r := recover(); r != nil { buf := make([]byte, 4096) - l := runtime.Stack(buf, false) + l := runtime.Stack(buf, false) errString := fmt.Sprint(r) - log.Dump(string(buf[:l]),log.String("error",errString)) + log.Dump(string(buf[:l]), log.String("error", errString)) } }() for { data, err := agent.conn.ReadMsg() if err != nil { - log.Error("read message is error",log.String("remoteAddress",agent.conn.RemoteAddr().String()),log.ErrorAttr("error",err)) - //will close tcpconn + //will close conn + log.Error("read message is error", log.String("remoteAddress", agent.conn.RemoteAddr().String()), log.ErrorAttr("error", err)) break } - defer agent.conn.ReleaseReadMsg(data) - err = agent.rpcServer.processRpcRequest( data,"",agent.WriteResponse) + err = agent.rpcServer.processRpcRequest(data, "", agent.WriteResponse) if err != nil { - log.Error("processRpcRequest is error",log.String("remoteAddress",agent.conn.RemoteAddr().String()),log.ErrorAttr("error",err)) - //will close tcpconn + //will close conn + agent.conn.ReleaseReadMsg(data) + log.Error("processRpcRequest is error", log.String("remoteAddress", agent.conn.RemoteAddr().String()), log.ErrorAttr("error", err)) + break } + agent.conn.ReleaseReadMsg(data) } } @@ -214,4 +216,3 @@ func (server *Server) NewAgent(c *network.TCPConn) network.Agent { return agent } - diff --git a/service/module.go b/service/module.go index 50554b8..909ff33 100644 --- a/service/module.go +++ b/service/module.go @@ -6,11 +6,11 @@ import ( "sync/atomic" "time" + "github.com/duanhf2012/origin/v2/concurrent" "github.com/duanhf2012/origin/v2/event" "github.com/duanhf2012/origin/v2/log" rpcHandle "github.com/duanhf2012/origin/v2/rpc" "github.com/duanhf2012/origin/v2/util/timer" - "github.com/duanhf2012/origin/v2/concurrent" ) const InitModuleId = 1e9 @@ -117,7 +117,7 @@ func (m *Module) AddModule(module IModule) (uint32, error) { m.child[module.GetModuleId()] = module m.ancestor.getBaseModule().(*Module).descendants[module.GetModuleId()] = module - log.Debug("Add module "+module.GetModuleName()+ " completed") + log.Debug("Add module " + module.GetModuleName() + " completed") return module.GetModuleId(), nil } @@ -131,7 +131,7 @@ func (m *Module) ReleaseModule(moduleId uint32) { pModule.self.OnRelease() pModule.GetEventHandler().Destroy() - log.Debug("Release module "+ pModule.GetModuleName()) + log.Debug("Release module " + pModule.GetModuleName()) for pTimer := range pModule.mapActiveTimer { pTimer.Cancel() } @@ -277,7 +277,7 @@ func (m *Module) SafeNewTicker(tickerId *uint64, d time.Duration, AdditionData i } func (m *Module) CancelTimerId(timerId *uint64) bool { - if timerId==nil || *timerId == 0 { + if timerId == nil || *timerId == 0 { log.Warning("timerId is invalid") return false } @@ -289,7 +289,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.Stack("cannot find timer id ", log.Uint64("timerId", *timerId)) return false } diff --git a/service/service.go b/service/service.go index 403d25c..b4f19ec 100644 --- a/service/service.go +++ b/service/service.go @@ -1,8 +1,10 @@ package service import ( + "encoding/json" "errors" "fmt" + "github.com/duanhf2012/origin/v2/concurrent" "github.com/duanhf2012/origin/v2/event" "github.com/duanhf2012/origin/v2/log" "github.com/duanhf2012/origin/v2/profiler" @@ -13,18 +15,14 @@ import ( "strconv" "sync" "sync/atomic" - "github.com/duanhf2012/origin/v2/concurrent" - "encoding/json" ) var timerDispatcherLen = 100000 var maxServiceEventChannelNum = 2000000 - - type IService interface { concurrent.IConcurrent - Init(iService IService,getClientFun rpc.FuncRpcClient,getServerFun rpc.FuncRpcServer,serviceCfg interface{}) + Init(iService IService, getClientFun rpc.FuncRpcClient, getServerFun rpc.FuncRpcServer, serviceCfg interface{}) Stop() Start() @@ -37,7 +35,7 @@ type IService interface { SetName(serviceName string) GetName() string GetRpcHandler() rpc.IRpcHandler - GetServiceCfg()interface{} + GetServiceCfg() interface{} GetProfiler() *profiler.Profiler GetServiceEventChannelNum() int GetServiceTimerChannelNum() int @@ -52,71 +50,67 @@ type IService interface { type Service struct { Module - rpcHandler rpc.RpcHandler //rpc - name string //service name - wg sync.WaitGroup - serviceCfg interface{} - goroutineNum int32 - startStatus bool - isRelease int32 - retire int32 - eventProcessor event.IEventProcessor - profiler *profiler.Profiler //性能分析器 - nodeConnLister rpc.INodeConnListener - natsConnListener rpc.INatsConnListener + rpcHandler rpc.RpcHandler //rpc + name string //service name + wg sync.WaitGroup + serviceCfg interface{} + goroutineNum int32 + startStatus bool + isRelease int32 + retire int32 + eventProcessor event.IEventProcessor + profiler *profiler.Profiler //性能分析器 + nodeConnLister rpc.INodeConnListener + natsConnListener rpc.INatsConnListener discoveryServiceLister rpc.IDiscoveryServiceListener - chanEvent chan event.IEvent - closeSig chan struct{} + chanEvent chan event.IEvent + closeSig chan struct{} } - - // DiscoveryServiceEvent 发现服务结点 -type DiscoveryServiceEvent struct{ +type DiscoveryServiceEvent struct { IsDiscovery bool ServiceName []string - NodeId string + NodeId string } type EtcdServiceRecordEvent struct { NetworkName string - TTLSecond int64 - RecordKey string - RecordInfo string + TTLSecond int64 + RecordKey string + RecordInfo string } type Empty struct { - } -func SetMaxServiceChannel(maxEventChannel int){ +func SetMaxServiceChannel(maxEventChannel int) { maxServiceEventChannelNum = maxEventChannel } -func (rpcEventData *DiscoveryServiceEvent) GetEventType() event.EventType{ +func (rpcEventData *DiscoveryServiceEvent) GetEventType() event.EventType { return event.Sys_Event_DiscoverService } - -func (s *Service) OnSetup(iService IService){ +func (s *Service) OnSetup(iService IService) { if iService.GetName() == "" { s.name = reflect.Indirect(reflect.ValueOf(iService)).Type().Name() } } -func (s *Service) OpenProfiler() { +func (s *Service) OpenProfiler() { s.profiler = profiler.RegProfiler(s.GetName()) - if s.profiler==nil { - log.Fatal("rofiler.RegProfiler "+s.GetName()+" fail.") + if s.profiler == nil { + log.Fatal("profiler.RegProfiler " + s.GetName() + " fail.") } } -func (s *Service) IsRetire() bool{ +func (s *Service) IsRetire() bool { return atomic.LoadInt32(&s.retire) != 0 } -func (s *Service) SetRetire(){ - atomic.StoreInt32(&s.retire,1) +func (s *Service) SetRetire() { + atomic.StoreInt32(&s.retire, 1) ev := event.NewEvent() ev.Type = event.Sys_Event_Retire @@ -124,40 +118,40 @@ func (s *Service) SetRetire(){ s.pushEvent(ev) } -func (s *Service) Init(iService IService,getClientFun rpc.FuncRpcClient,getServerFun rpc.FuncRpcServer,serviceCfg interface{}) { +func (s *Service) Init(iService IService, getClientFun rpc.FuncRpcClient, getServerFun rpc.FuncRpcServer, serviceCfg interface{}) { s.closeSig = make(chan struct{}) - s.dispatcher =timer.NewDispatcher(timerDispatcherLen) + s.dispatcher = timer.NewDispatcher(timerDispatcherLen) if s.chanEvent == nil { - s.chanEvent = make(chan event.IEvent,maxServiceEventChannelNum) + s.chanEvent = make(chan event.IEvent, maxServiceEventChannelNum) } - s.rpcHandler.InitRpcHandler(iService.(rpc.IRpcHandler),getClientFun,getServerFun,iService.(rpc.IRpcHandlerChannel)) + s.rpcHandler.InitRpcHandler(iService.(rpc.IRpcHandler), getClientFun, getServerFun, iService.(rpc.IRpcHandlerChannel)) s.IRpcHandler = &s.rpcHandler s.self = iService.(IModule) //初始化祖先 s.ancestor = iService.(IModule) - s.seedModuleId =InitModuleId + s.seedModuleId = InitModuleId s.descendants = map[uint32]IModule{} s.serviceCfg = serviceCfg s.goroutineNum = 1 s.eventProcessor = event.NewEventProcessor() s.eventProcessor.Init(s) - s.eventHandler = event.NewEventHandler() + s.eventHandler = event.NewEventHandler() s.eventHandler.Init(s.eventProcessor) s.Module.IConcurrent = &concurrent.Concurrent{} } func (s *Service) Start() { s.startStatus = true - atomic.StoreInt32(&s.isRelease,0) + atomic.StoreInt32(&s.isRelease, 0) var waitRun sync.WaitGroup - log.Info(s.GetName()+" service is running",) + log.Info(s.GetName() + " service is running") s.self.(IService).OnStart() - for i:=int32(0);i< s.goroutineNum;i++{ + for i := int32(0); i < s.goroutineNum; i++ { s.wg.Add(1) waitRun.Add(1) - go func(){ + go func() { waitRun.Done() s.run() }() @@ -169,79 +163,79 @@ func (s *Service) Start() { func (s *Service) run() { defer s.wg.Done() var bStop = false - - concurrent := s.IConcurrent.(*concurrent.Concurrent) - concurrentCBChannel := concurrent.GetCallBackChannel() - for{ + cr := s.IConcurrent.(*concurrent.Concurrent) + concurrentCBChannel := cr.GetCallBackChannel() + + for { var analyzer *profiler.Analyzer select { - case <- s.closeSig: + case <-s.closeSig: bStop = true s.Release() - concurrent.Close() - case cb:=<-concurrentCBChannel: - concurrent.DoCallback(cb) - case ev := <- s.chanEvent: + cr.Close() + case cb := <-concurrentCBChannel: + cr.DoCallback(cb) + case ev := <-s.chanEvent: switch ev.GetEventType() { case event.Sys_Event_Retire: - log.Info("service OnRetire",log.String("servceName",s.GetName())) + log.Info("service OnRetire", log.String("serviceName", s.GetName())) s.self.(IService).OnRetire() case event.ServiceRpcRequestEvent: - cEvent,ok := ev.(*event.Event) + cEvent, ok := ev.(*event.Event) if ok == false { log.Error("Type event conversion error") break } - rpcRequest,ok := cEvent.Data.(*rpc.RpcRequest) + rpcRequest, ok := cEvent.Data.(*rpc.RpcRequest) if ok == false { log.Error("Type *rpc.RpcRequest conversion error") break } - if s.profiler!=nil { - analyzer = s.profiler.Push("[Req]"+rpcRequest.RpcRequestData.GetServiceMethod()) + if s.profiler != nil { + analyzer = s.profiler.Push("[Req]" + rpcRequest.RpcRequestData.GetServiceMethod()) } s.GetRpcHandler().HandlerRpcRequest(rpcRequest) - if analyzer!=nil { + if analyzer != nil { analyzer.Pop() analyzer = nil } event.DeleteEvent(cEvent) case event.ServiceRpcResponseEvent: - cEvent,ok := ev.(*event.Event) + cEvent, ok := ev.(*event.Event) if ok == false { log.Error("Type event conversion error") break } - rpcResponseCB,ok := cEvent.Data.(*rpc.Call) + rpcResponseCB, ok := cEvent.Data.(*rpc.Call) if ok == false { log.Error("Type *rpc.Call conversion error") break } - if s.profiler!=nil { + if s.profiler != nil { analyzer = s.profiler.Push("[Res]" + rpcResponseCB.ServiceMethod) } s.GetRpcHandler().HandlerRpcResponseCB(rpcResponseCB) - if analyzer!=nil { + if analyzer != nil { analyzer.Pop() analyzer = nil } event.DeleteEvent(cEvent) default: - if s.profiler!=nil { - analyzer = s.profiler.Push("[SEvent]"+strconv.Itoa(int(ev.GetEventType()))) + if s.profiler != nil { + analyzer = s.profiler.Push("[SEvent]" + strconv.Itoa(int(ev.GetEventType()))) } s.eventProcessor.EventHandler(ev) - if analyzer!=nil { + if analyzer != nil { analyzer.Pop() analyzer = nil } } - case t := <- s.dispatcher.ChanTimer: + case t := <-s.dispatcher.ChanTimer: if s.profiler != nil { - analyzer = s.profiler.Push("[timer]"+t.GetName()) + analyzer = s.profiler.Push("[timer]" + t.GetName()) } t.Do() if analyzer != nil { @@ -256,7 +250,7 @@ func (s *Service) run() { } } -func (s *Service) GetName() string{ +func (s *Service) GetName() string { return s.name } @@ -264,138 +258,138 @@ func (s *Service) SetName(serviceName string) { s.name = serviceName } -func (s *Service) Release(){ +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.Dump(string(buf[:l]), log.String("error", errString)) } }() - if atomic.AddInt32(&s.isRelease,-1) == -1{ + if atomic.AddInt32(&s.isRelease, -1) == -1 { s.self.OnRelease() } } -func (s *Service) OnRelease(){ +func (s *Service) OnRelease() { } func (s *Service) OnInit() error { return nil } -func (s *Service) Stop(){ - log.Info("stop "+s.GetName()+" service ") +func (s *Service) Stop() { + log.Info("stop " + s.GetName() + " service ") close(s.closeSig) s.wg.Wait() - log.Info(s.GetName()+" service has been stopped") + log.Info(s.GetName() + " service has been stopped") } -func (s *Service) GetServiceCfg()interface{}{ +func (s *Service) GetServiceCfg() interface{} { return s.serviceCfg } -func (s *Service) ParseServiceCfg(cfg interface{}) error{ +func (s *Service) ParseServiceCfg(cfg interface{}) error { if s.serviceCfg == nil { return errors.New("no service configuration found") } rv := reflect.ValueOf(s.serviceCfg) - if rv.Kind() == reflect.Ptr && rv.IsNil() { + if rv.Kind() == reflect.Ptr && rv.IsNil() { return errors.New("no service configuration found") } - bytes,err := json.Marshal(s.serviceCfg) + bytes, err := json.Marshal(s.serviceCfg) if err != nil { return err } - return json.Unmarshal(bytes,cfg) + return json.Unmarshal(bytes, cfg) } -func (s *Service) GetProfiler() *profiler.Profiler{ +func (s *Service) GetProfiler() *profiler.Profiler { return s.profiler } -func (s *Service) RegEventReceiverFunc(eventType event.EventType, receiver event.IEventHandler,callback event.EventCallBack){ - s.eventProcessor.RegEventReceiverFunc(eventType, receiver,callback) +func (s *Service) RegEventReceiverFunc(eventType event.EventType, receiver event.IEventHandler, callback event.EventCallBack) { + s.eventProcessor.RegEventReceiverFunc(eventType, receiver, callback) } -func (s *Service) UnRegEventReceiverFunc(eventType event.EventType, receiver event.IEventHandler){ +func (s *Service) UnRegEventReceiverFunc(eventType event.EventType, receiver event.IEventHandler) { s.eventProcessor.UnRegEventReceiverFun(eventType, receiver) } -func (s *Service) RegRawRpc(rpcMethodId uint32,rawRpcCB rpc.RawRpcCallBack){ - s.rpcHandler.RegRawRpc(rpcMethodId,rawRpcCB) +func (s *Service) RegRawRpc(rpcMethodId uint32, rawRpcCB rpc.RawRpcCallBack) { + s.rpcHandler.RegRawRpc(rpcMethodId, rawRpcCB) } -func (s *Service) OnStart(){ +func (s *Service) OnStart() { } -func (s *Service) OnNodeConnEvent(ev event.IEvent){ - event := ev.(*rpc.RpcConnEvent) - if event.IsConnect { - s.nodeConnLister.OnNodeConnected(event.NodeId) - }else{ - s.nodeConnLister.OnNodeDisconnect(event.NodeId) +func (s *Service) OnNodeConnEvent(ev event.IEvent) { + re := ev.(*rpc.RpcConnEvent) + if re.IsConnect { + s.nodeConnLister.OnNodeConnected(re.NodeId) + } else { + s.nodeConnLister.OnNodeDisconnect(re.NodeId) } } -func (s *Service) OnNatsConnEvent(ev event.IEvent){ +func (s *Service) OnNatsConnEvent(ev event.IEvent) { event := ev.(*rpc.NatsConnEvent) if event.IsConnect { s.natsConnListener.OnNatsConnected() - }else{ + } else { s.natsConnListener.OnNatsDisconnect() } } -func (s *Service) OnDiscoverServiceEvent(ev event.IEvent){ - event := ev.(*DiscoveryServiceEvent) - if event.IsDiscovery { - s.discoveryServiceLister.OnDiscoveryService(event.NodeId,event.ServiceName) - }else{ - s.discoveryServiceLister.OnUnDiscoveryService(event.NodeId,event.ServiceName) +func (s *Service) OnDiscoverServiceEvent(ev event.IEvent) { + de := ev.(*DiscoveryServiceEvent) + if de.IsDiscovery { + s.discoveryServiceLister.OnDiscoveryService(de.NodeId, de.ServiceName) + } else { + s.discoveryServiceLister.OnUnDiscoveryService(de.NodeId, de.ServiceName) } } func (s *Service) RegNodeConnListener(nodeConnListener rpc.INodeConnListener) { s.nodeConnLister = nodeConnListener - s.RegEventReceiverFunc(event.Sys_Event_Node_Conn_Event,s.GetEventHandler(),s.OnNodeConnEvent) + s.RegEventReceiverFunc(event.Sys_Event_Node_Conn_Event, s.GetEventHandler(), s.OnNodeConnEvent) RegRpcEventFun(s.GetName()) } func (s *Service) UnRegNodeConnListener() { - s.UnRegEventReceiverFunc(event.Sys_Event_Node_Conn_Event,s.GetEventHandler()) + s.UnRegEventReceiverFunc(event.Sys_Event_Node_Conn_Event, s.GetEventHandler()) UnRegRpcEventFun(s.GetName()) } func (s *Service) RegNatsConnListener(natsConnListener rpc.INatsConnListener) { s.natsConnListener = natsConnListener - s.RegEventReceiverFunc(event.Sys_Event_Nats_Conn_Event,s.GetEventHandler(),s.OnNatsConnEvent) + s.RegEventReceiverFunc(event.Sys_Event_Nats_Conn_Event, s.GetEventHandler(), s.OnNatsConnEvent) RegRpcEventFun(s.GetName()) } func (s *Service) UnRegNatsConnListener() { - s.UnRegEventReceiverFunc(event.Sys_Event_Nats_Conn_Event,s.GetEventHandler()) + s.UnRegEventReceiverFunc(event.Sys_Event_Nats_Conn_Event, s.GetEventHandler()) UnRegRpcEventFun(s.GetName()) } func (s *Service) RegDiscoverListener(discoveryServiceListener rpc.IDiscoveryServiceListener) { s.discoveryServiceLister = discoveryServiceListener - s.RegEventReceiverFunc(event.Sys_Event_DiscoverService,s.GetEventHandler(),s.OnDiscoverServiceEvent) + s.RegEventReceiverFunc(event.Sys_Event_DiscoverService, s.GetEventHandler(), s.OnDiscoverServiceEvent) RegRpcEventFun(s.GetName()) } func (s *Service) UnRegDiscoverListener() { - s.UnRegEventReceiverFunc(event.Sys_Event_DiscoverService,s.GetEventHandler()) + s.UnRegEventReceiverFunc(event.Sys_Event_DiscoverService, s.GetEventHandler()) UnRegRpcEventFun(s.GetName()) } -func (s *Service) PushRpcRequest(rpcRequest *rpc.RpcRequest) error{ +func (s *Service) PushRpcRequest(rpcRequest *rpc.RpcRequest) error { ev := event.NewEvent() ev.Type = event.ServiceRpcRequestEvent ev.Data = rpcRequest @@ -403,7 +397,7 @@ func (s *Service) PushRpcRequest(rpcRequest *rpc.RpcRequest) error{ return s.pushEvent(ev) } -func (s *Service) PushRpcResponse(call *rpc.Call) error{ +func (s *Service) PushRpcResponse(call *rpc.Call) error { ev := event.NewEvent() ev.Type = event.ServiceRpcResponseEvent ev.Data = call @@ -411,13 +405,13 @@ func (s *Service) PushRpcResponse(call *rpc.Call) error{ return s.pushEvent(ev) } -func (s *Service) PushEvent(ev event.IEvent) error{ +func (s *Service) PushEvent(ev event.IEvent) error { return s.pushEvent(ev) } -func (s *Service) pushEvent(ev event.IEvent) error{ +func (s *Service) pushEvent(ev event.IEvent) error { if len(s.chanEvent) >= maxServiceEventChannelNum { - err := errors.New("The event channel in the service is full") + err := errors.New("the event channel in the service is full") log.Error(err.Error()) return err } @@ -426,25 +420,25 @@ func (s *Service) pushEvent(ev event.IEvent) error{ return nil } -func (s *Service) GetServiceEventChannelNum() int{ +func (s *Service) GetServiceEventChannelNum() int { return len(s.chanEvent) } -func (s *Service) GetServiceTimerChannelNum() int{ +func (s *Service) GetServiceTimerChannelNum() int { return len(s.dispatcher.ChanTimer) } -func (s *Service) SetEventChannelNum(num int){ +func (s *Service) SetEventChannelNum(num int) { if s.chanEvent == nil { - s.chanEvent = make(chan event.IEvent,num) - }else { + s.chanEvent = make(chan event.IEvent, num) + } else { panic("this stage cannot be set") } } func (s *Service) SetGoRoutineNum(goroutineNum int32) bool { //已经开始状态不允许修改协程数量,打开性能分析器不允许开多线程 - if s.startStatus == true || s.profiler!=nil { + if s.startStatus == true || s.profiler != nil { log.Error("open profiler mode is not allowed to set Multi-coroutine.") return false } @@ -453,5 +447,5 @@ func (s *Service) SetGoRoutineNum(goroutineNum int32) bool { return true } -func (s *Service) OnRetire(){ +func (s *Service) OnRetire() { } diff --git a/sysmodule/frametimer/FrameTimerModule.go b/sysmodule/frametimer/FrameTimerModule.go index 0b93dca..3b0d06c 100644 --- a/sysmodule/frametimer/FrameTimerModule.go +++ b/sysmodule/frametimer/FrameTimerModule.go @@ -58,7 +58,7 @@ func (ft *FrameTimer) addTimerData(timerID FrameTimerID, frameNum FrameNumber, t td.cb = cb td.idx = -1 td.tickerFrameNum = tickerFrameNum - + td.ctx = ctx ft.mapTimer[timerID] = td return td } diff --git a/sysmodule/ginmodule/GinModule.go b/sysmodule/ginmodule/GinModule.go index 1fcf362..71600be 100644 --- a/sysmodule/ginmodule/GinModule.go +++ b/sysmodule/ginmodule/GinModule.go @@ -6,11 +6,11 @@ import ( "github.com/duanhf2012/origin/v2/log" "github.com/duanhf2012/origin/v2/service" "github.com/gin-gonic/gin" + "io" "log/slog" "net/http" "strings" "time" - "io" ) type IGinProcessor interface { @@ -23,12 +23,12 @@ type GinModule struct { *gin.Engine srv *http.Server - listenAddr string + listenAddr string handleTimeout time.Duration - processor []IGinProcessor + processor []IGinProcessor } -func (gm *GinModule) Init(addr string, handleTimeout time.Duration,engine *gin.Engine) { +func (gm *GinModule) Init(addr string, handleTimeout time.Duration, engine *gin.Engine) { gm.listenAddr = addr gm.handleTimeout = handleTimeout gm.Engine = engine @@ -96,91 +96,91 @@ func (gm *GinModule) Stop(ctx context.Context) { type SafeContext struct { *gin.Context - chanWait chan struct{} + chanWait chan struct{} } func (c *SafeContext) JSONAndDone(code int, obj any) { - c.Context.JSON(code,obj) + c.Context.JSON(code, obj) c.Done() } -func (c *SafeContext) AsciiJSONAndDone(code int, obj any){ - c.Context.AsciiJSON(code,obj) +func (c *SafeContext) AsciiJSONAndDone(code int, obj any) { + c.Context.AsciiJSON(code, obj) c.Done() } -func (c *SafeContext) PureJSONAndDone(code int, obj any){ - c.Context.PureJSON(code,obj) +func (c *SafeContext) PureJSONAndDone(code int, obj any) { + c.Context.PureJSON(code, obj) c.Done() } -func (c *SafeContext) XMLAndDone(code int, obj any){ - c.Context.XML(code,obj) +func (c *SafeContext) XMLAndDone(code int, obj any) { + c.Context.XML(code, obj) c.Done() } -func (c *SafeContext) YAMLAndDone(code int, obj any){ - c.Context.YAML(code,obj) +func (c *SafeContext) YAMLAndDone(code int, obj any) { + c.Context.YAML(code, obj) c.Done() } -func (c *SafeContext) TOMLAndDone(code int, obj any){ - c.Context.TOML(code,obj) +func (c *SafeContext) TOMLAndDone(code int, obj any) { + c.Context.TOML(code, obj) c.Done() } -func (c *SafeContext) ProtoBufAndDone(code int, obj any){ - c.Context.ProtoBuf(code,obj) +func (c *SafeContext) ProtoBufAndDone(code int, obj any) { + c.Context.ProtoBuf(code, obj) c.Done() } -func (c *SafeContext) StringAndDone(code int, format string, values ...any){ - c.Context.String(code,format,values...) +func (c *SafeContext) StringAndDone(code int, format string, values ...any) { + c.Context.String(code, format, values...) c.Done() } -func (c *SafeContext) RedirectAndDone(code int, location string){ - c.Context.Redirect(code,location) +func (c *SafeContext) RedirectAndDone(code int, location string) { + c.Context.Redirect(code, location) c.Done() } -func (c *SafeContext) DataAndDone(code int, contentType string, data []byte){ - c.Context.Data(code,contentType,data) +func (c *SafeContext) DataAndDone(code int, contentType string, data []byte) { + c.Context.Data(code, contentType, data) c.Done() } -func (c *SafeContext) DataFromReaderAndDone(code int, contentLength int64, contentType string, reader io.Reader, extraHeaders map[string]string){ - c.DataFromReader(code,contentLength,contentType,reader,extraHeaders) +func (c *SafeContext) DataFromReaderAndDone(code int, contentLength int64, contentType string, reader io.Reader, extraHeaders map[string]string) { + c.DataFromReader(code, contentLength, contentType, reader, extraHeaders) c.Done() } -func (c *SafeContext) HTMLAndDone(code int, name string, obj any){ - c.Context.HTML(code,name,obj) +func (c *SafeContext) HTMLAndDone(code int, name string, obj any) { + c.Context.HTML(code, name, obj) c.Done() } -func (c *SafeContext) IndentedJSONAndDone(code int, obj any){ - c.Context.IndentedJSON(code,obj) +func (c *SafeContext) IndentedJSONAndDone(code int, obj any) { + c.Context.IndentedJSON(code, obj) c.Done() } -func (c *SafeContext) SecureJSONAndDone(code int, obj any){ - c.Context.SecureJSON(code,obj) +func (c *SafeContext) SecureJSONAndDone(code int, obj any) { + c.Context.SecureJSON(code, obj) c.Done() } -func (c *SafeContext) JSONPAndDone(code int, obj any){ - c.Context.JSONP(code,obj) +func (c *SafeContext) JSONPAndDone(code int, obj any) { + c.Context.JSONP(code, obj) c.Done() } -func (c *SafeContext) Done(){ +func (c *SafeContext) Done() { c.chanWait <- struct{}{} } type GinEvent struct { handlersChain []SafeHandlerFunc - c SafeContext + c SafeContext } type SafeHandlerFunc func(*SafeContext) @@ -199,16 +199,16 @@ func (gm *GinModule) handleMethod(httpMethod, relativePath string, handlers ...S } var ev GinEvent - chanWait := make(chan struct{},2) + chanWait := make(chan struct{}, 2) ev.c.chanWait = chanWait ev.handlersChain = handlers ev.c.Context = c gm.NotifyEvent(&ev) - ctx,cancel := context.WithTimeout(context.Background(), gm.handleTimeout) + ctx, cancel := context.WithTimeout(context.Background(), gm.handleTimeout) defer cancel() - select{ + select { case <-ctx.Done(): log.Error("GinModule process timeout", slog.Any("path", c.Request.URL.Path)) c.AbortWithStatus(http.StatusRequestTimeout) diff --git a/sysmodule/httpclientmodule/httpclientpoolmodule.go b/sysmodule/httpclientmodule/httpclientpoolmodule.go index 644e7f2..f4456e0 100644 --- a/sysmodule/httpclientmodule/httpclientpoolmodule.go +++ b/sysmodule/httpclientmodule/httpclientpoolmodule.go @@ -39,19 +39,19 @@ func (slf *SyncHttpResponse) Get(timeoutMs int) HttpResponse { return rsp } return HttpResponse{ - Err: fmt.Errorf("Getting the return result timeout [%d]ms", timeoutMs), + Err: fmt.Errorf("getting the return result timeout [%d]ms", timeoutMs), } } -func (m *HttpClientModule) InitHttpClient(transport http.RoundTripper,timeout time.Duration,checkRedirect func(req *http.Request, via []*http.Request) error){ +func (m *HttpClientModule) InitHttpClient(transport http.RoundTripper, timeout time.Duration, checkRedirect func(req *http.Request, via []*http.Request) error) { m.client = &http.Client{ - Transport: transport, - Timeout: timeout, + Transport: transport, + Timeout: timeout, CheckRedirect: checkRedirect, } } -func (m *HttpClientModule) Init(proxyUrl string, maxpool int, dialTimeout time.Duration,dialKeepAlive time.Duration,idleConnTimeout time.Duration,timeout time.Duration) { +func (m *HttpClientModule) Init(proxyUrl string, maxPool int, dialTimeout time.Duration, dialKeepAlive time.Duration, idleConnTimeout time.Duration, timeout time.Duration) { type ProxyFun func(_ *http.Request) (*url.URL, error) var proxyFun ProxyFun if proxyUrl != "" { @@ -66,8 +66,8 @@ func (m *HttpClientModule) Init(proxyUrl string, maxpool int, dialTimeout time.D Timeout: dialTimeout, KeepAlive: dialKeepAlive, }).DialContext, - MaxIdleConns: maxpool, - MaxIdleConnsPerHost: maxpool, + MaxIdleConns: maxPool, + MaxIdleConnsPerHost: maxPool, IdleConnTimeout: idleConnTimeout, Proxy: proxyFun, TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, diff --git a/sysmodule/mongodbmodule/mongodbmodule.go b/sysmodule/mongodbmodule/mongodbmodule.go index 830ae55..4dc3e29 100644 --- a/sysmodule/mongodbmodule/mongodbmodule.go +++ b/sysmodule/mongodbmodule/mongodbmodule.go @@ -75,12 +75,12 @@ func (s *Session) NextSeq(db string, collection string, id interface{}) (int, er return res.Seq, err } -// indexKeys[索引][每个索引key字段] +// EnsureIndex indexKeys[索引][每个索引key字段] func (s *Session) EnsureIndex(db string, collection string, indexKeys [][]string, bBackground bool, sparse bool, asc bool) error { return s.ensureIndex(db, collection, indexKeys, bBackground, false, sparse, asc) } -// indexKeys[索引][每个索引key字段] +// EnsureUniqueIndex indexKeys[索引][每个索引key字段] func (s *Session) EnsureUniqueIndex(db string, collection string, indexKeys [][]string, bBackground bool, sparse bool, asc bool) error { return s.ensureIndex(db, collection, indexKeys, bBackground, true, sparse, asc) } @@ -92,9 +92,9 @@ func (s *Session) ensureIndex(db string, collection string, indexKeys [][]string keysDoc := bson.D{} for _, key := range keys { if asc { - keysDoc = append(keysDoc, bson.E{Key:key,Value:1}) + keysDoc = append(keysDoc, bson.E{Key: key, Value: 1}) } else { - keysDoc = append(keysDoc, bson.E{Key:key,Value:-1}) + keysDoc = append(keysDoc, bson.E{Key: key, Value: -1}) } } diff --git a/sysmodule/mysqlmodule/mysqlmodule.go b/sysmodule/mysqlmodule/mysqlmodule.go index 189ee89..882baf8 100644 --- a/sysmodule/mysqlmodule/mysqlmodule.go +++ b/sysmodule/mysqlmodule/mysqlmodule.go @@ -19,8 +19,8 @@ import ( type SyncFun func() type DBExecute struct { - syncExecuteFun chan SyncFun - syncExecuteExit chan bool + syncExecuteFun chan SyncFun + syncExecuteExit chan bool } type PingExecute struct { @@ -28,22 +28,21 @@ type PingExecute struct { pintExit chan bool } -// DBModule ... type MySQLModule struct { service.Module - db *sql.DB - url string - username string - password string - dbname string - slowDuration time.Duration - pingCoroutine PingExecute - waitGroup sync.WaitGroup + db *sql.DB + url string + username string + password string + dbname string + slowDuration time.Duration + pingCoroutine PingExecute + waitGroup sync.WaitGroup } // Tx ... type Tx struct { - tx *sql.Tx + tx *sql.Tx slowDuration time.Duration } @@ -53,7 +52,7 @@ type DBResult struct { RowsAffected int64 rowNum int - RowInfo map[string][]interface{} //map[fieldname][row]sql.NullString + RowInfo map[string][]interface{} //map[fieldName][row]sql.NullString } type DataSetList struct { @@ -63,19 +62,17 @@ type DataSetList struct { blur bool } - type dbControl interface { Exec(query string, args ...interface{}) (sql.Result, error) Query(query string, args ...interface{}) (*sql.Rows, error) } - -func (m *MySQLModule) Init( url string, userName string, password string, dbname string,maxConn int) error { +func (m *MySQLModule) Init(url string, userName string, password string, dbname string, maxConn int) error { m.url = url m.username = userName m.password = password m.dbname = dbname - m.pingCoroutine = PingExecute{tickerPing : time.NewTicker(5*time.Second), pintExit : make(chan bool, 1)} + m.pingCoroutine = PingExecute{tickerPing: time.NewTicker(5 * time.Second), pintExit: make(chan bool, 1)} return m.connect(maxConn) } @@ -85,12 +82,12 @@ func (m *MySQLModule) SetQuerySlowTime(slowDuration time.Duration) { } func (m *MySQLModule) Query(strQuery string, args ...interface{}) (*DataSetList, error) { - return query(m.slowDuration, m.db,strQuery,args...) + return query(m.slowDuration, m.db, strQuery, args...) } // Exec ... func (m *MySQLModule) Exec(strSql string, args ...interface{}) (*DBResult, error) { - return exec(m.slowDuration, m.db,strSql,args...) + return exec(m.slowDuration, m.db, strSql, args...) } // Begin starts a transaction. @@ -116,14 +113,14 @@ func (slf *Tx) Commit() error { return slf.tx.Commit() } -// QueryEx executes a query that return rows. +// Query executes a query that return rows. func (slf *Tx) Query(strQuery string, args ...interface{}) (*DataSetList, error) { - return query(slf.slowDuration,slf.tx,strQuery,args...) + return query(slf.slowDuration, slf.tx, strQuery, args...) } // Exec executes a query that doesn't return rows. func (slf *Tx) Exec(strSql string, args ...interface{}) (*DBResult, error) { - return exec(slf.slowDuration,slf.tx,strSql,args...) + return exec(slf.slowDuration, slf.tx, strSql, args...) } // Connect ... @@ -168,7 +165,7 @@ func (m *MySQLModule) runPing() { } } -func checkArgs(args ...interface{}) error { +func checkArgs(args ...interface{}) error { for _, val := range args { if reflect.TypeOf(val).Kind() == reflect.String { retVal := val.(string) @@ -211,33 +208,33 @@ func checkArgs(args ...interface{}) error { return nil } -func checkSlow(slowDuration time.Duration,Time time.Duration) bool { - if slowDuration != 0 && Time >=slowDuration { +func checkSlow(slowDuration time.Duration, Time time.Duration) bool { + if slowDuration != 0 && Time >= slowDuration { return true } return false } -func query(slowDuration time.Duration,db dbControl,strQuery string, args ...interface{}) (*DataSetList, error) { +func query(slowDuration time.Duration, db dbControl, strQuery string, args ...interface{}) (*DataSetList, error) { datasetList := DataSetList{} datasetList.tag = "json" datasetList.blur = true if checkArgs(args) != nil { log.Error("CheckArgs is error :%s", strQuery) - return &datasetList, fmt.Errorf("CheckArgs is error!") + return &datasetList, fmt.Errorf("checkArgs is error") } if db == nil { log.Error("cannot connect database:%s", strQuery) - return &datasetList, fmt.Errorf("cannot connect database!") + return &datasetList, fmt.Errorf("cannot connect database") } TimeFuncStart := time.Now() rows, err := db.Query(strQuery, args...) timeFuncPass := time.Since(TimeFuncStart) - if checkSlow(slowDuration,timeFuncPass) { + if checkSlow(slowDuration, timeFuncPass) { log.Error("DBModule QueryEx Time %s , Query :%s , args :%+v", timeFuncPass, strQuery, args) } if err != nil { @@ -256,7 +253,7 @@ func query(slowDuration time.Duration,db dbControl,strQuery string, args ...inte if dbResult.RowInfo == nil { dbResult.RowInfo = make(map[string][]interface{}) } - //RowInfo map[string][][]sql.NullString //map[fieldname][row][column]sql.NullString + colField, err := rows.Columns() if err != nil { return &datasetList, err @@ -268,10 +265,10 @@ func query(slowDuration time.Duration,db dbControl,strQuery string, args ...inte } rows.Scan(valuePtrs...) - for idx, fieldname := range colField { - fieldRowData := dbResult.RowInfo[strings.ToLower(fieldname)] + for idx, fieldName := range colField { + fieldRowData := dbResult.RowInfo[strings.ToLower(fieldName)] fieldRowData = append(fieldRowData, valuePtrs[idx]) - dbResult.RowInfo[strings.ToLower(fieldname)] = fieldRowData + dbResult.RowInfo[strings.ToLower(fieldName)] = fieldRowData } dbResult.rowNum += 1 } @@ -282,7 +279,7 @@ func query(slowDuration time.Duration,db dbControl,strQuery string, args ...inte if hasRet == false { if rows.Err() != nil { - log.Error( "Query:%s(%+v)", strQuery, rows) + log.Error("Query:%s(%+v)", strQuery, rows) } break } @@ -291,22 +288,22 @@ func query(slowDuration time.Duration,db dbControl,strQuery string, args ...inte return &datasetList, nil } -func exec(slowDuration time.Duration,db dbControl,strSql string, args ...interface{}) (*DBResult, error) { +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) - return ret, fmt.Errorf("cannot connect database!") + return ret, fmt.Errorf("cannot connect database") } if checkArgs(args) != nil { log.Error("CheckArgs is error :%s", strSql) - return ret, fmt.Errorf("CheckArgs is error!") + return ret, fmt.Errorf("checkArgs is error") } TimeFuncStart := time.Now() res, err := db.Exec(strSql, args...) timeFuncPass := time.Since(TimeFuncStart) - if checkSlow(slowDuration,timeFuncPass) { + if checkSlow(slowDuration, timeFuncPass) { log.Error("DBModule QueryEx Time %s , Query :%s , args :%+v", timeFuncPass, strSql, args) } if err != nil { @@ -361,21 +358,21 @@ func (ds *DataSetList) slice2interface(in interface{}) error { } v := reflect.ValueOf(in).Elem() - newv := reflect.MakeSlice(v.Type(), 0, length) - v.Set(newv) + newV := reflect.MakeSlice(v.Type(), 0, length) + v.Set(newV) v.SetLen(length) for i := 0; i < length; i++ { - idxv := v.Index(i) - if idxv.Kind() == reflect.Ptr { - newObj := reflect.New(idxv.Type().Elem()) + idxV := v.Index(i) + if idxV.Kind() == reflect.Ptr { + newObj := reflect.New(idxV.Type().Elem()) v.Index(i).Set(newObj) - idxv = newObj + idxV = newObj } else { - idxv = idxv.Addr() + idxV = idxV.Addr() } - err := ds.rowData2interface(i, ds.dataSetList[ds.currentDataSetIdx].RowInfo, idxv) + err := ds.rowData2interface(i, ds.dataSetList[ds.currentDataSetIdx].RowInfo, idxV) if err != nil { return err } @@ -390,7 +387,7 @@ func (ds *DataSetList) rowData2interface(rowIdx int, m map[string][]interface{}, typ := t.Elem() if !val.IsValid() { - return errors.New("Incorrect data type!") + return errors.New("incorrect data type") } for i := 0; i < val.NumField(); i++ { @@ -402,11 +399,11 @@ func (ds *DataSetList) rowData2interface(rowIdx int, m map[string][]interface{}, } if tag != "" && tag != "-" { - vtag := strings.ToLower(tag) - columnData, ok := m[vtag] + vTag := strings.ToLower(tag) + columnData, ok := m[vTag] if ok == false { if !ds.blur { - return fmt.Errorf("Cannot find filed name %s!", vtag) + return fmt.Errorf("cannot find filed name %s", vTag) } continue } @@ -416,12 +413,12 @@ func (ds *DataSetList) rowData2interface(rowIdx int, m map[string][]interface{}, meta := columnData[rowIdx].(*sql.NullString) if !ok { if !ds.blur { - return fmt.Errorf("No corresponding field was found in the result set %s!", tag) + return fmt.Errorf("no corresponding field was found in the result set %s", tag) } continue } if !value.CanSet() { - return errors.New("Struct fields do not have read or write permissions!") + return errors.New("struct fields do not have read or write permissions") } if meta.Valid == false { @@ -460,7 +457,7 @@ func (ds *DataSetList) rowData2interface(rowIdx int, m map[string][]interface{}, } value.SetBool(b) default: - return errors.New("The database map has unrecognized data types!") + return errors.New("the database map has unrecognized data types") } } } diff --git a/sysmodule/redismodule/redismodule.go b/sysmodule/redismodule/redismodule.go index f4b795b..da70838 100644 --- a/sysmodule/redismodule/redismodule.go +++ b/sysmodule/redismodule/redismodule.go @@ -90,18 +90,18 @@ func (m *RedisModule) Init(redisCfg *ConfigRedis) { func (m *RedisModule) getConn() (redis.Conn, error) { if m.redisPool == nil { log.Error("Not Init RedisModule") - return nil, fmt.Errorf("Not Init RedisModule") + return nil, fmt.Errorf("not Init RedisModule") } conn := m.redisPool.Get() if conn == nil { log.Error("Cannot get connection") - return nil, fmt.Errorf("Cannot get connection") + return nil, fmt.Errorf("cannot get connection") } 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,reason:%v", err) } conn.Close() return nil, err @@ -153,7 +153,7 @@ func (m *RedisModule) SetStringJSONExpire(key interface{}, val interface{}, expi func (m *RedisModule) setStringByExpire(key, value, expire interface{}) error { if key == "" { - return errors.New("Key Is Empty") + return errors.New("key is empty") } conn, err := m.getConn() @@ -474,7 +474,7 @@ func (m *RedisModule) DelStringKeyList(keys []interface{}) (map[interface{}]bool func (m *RedisModule) SetHash(redisKey, hashKey, value interface{}) error { if redisKey == "" || hashKey == "" { - return errors.New("Key Is Empty") + return errors.New("key is empty") } conn, err := m.getConn() if err != nil { @@ -490,10 +490,9 @@ func (m *RedisModule) SetHash(redisKey, hashKey, value interface{}) error { return retErr } -// GetRedisAllHashJSON ... func (m *RedisModule) GetAllHashJSON(redisKey string) (map[string]string, error) { if redisKey == "" { - return nil, errors.New("Key Is Empty") + return nil, errors.New("key is empty") } conn, err := m.getConn() if err != nil { @@ -513,7 +512,7 @@ func (m *RedisModule) GetAllHashJSON(redisKey string) (map[string]string, error) func (m *RedisModule) GetHash(redisKey interface{}, fieldKey interface{}) (string, error) { if redisKey == "" || fieldKey == "" { log.Error("GetHashValueByKey key is empty!") - return "", errors.New("Key Is Empty") + return "", errors.New("key is empty") } conn, err := m.getConn() if err != nil { @@ -527,7 +526,7 @@ func (m *RedisModule) GetHash(redisKey interface{}, fieldKey interface{}) (strin return "", err } if value == nil { - return "", errors.New("Reids Get Hash nil") + return "", errors.New("redis get hash nil") } return redis.String(value, nil) @@ -536,7 +535,7 @@ func (m *RedisModule) GetHash(redisKey interface{}, fieldKey interface{}) (strin func (m *RedisModule) GetMuchHash(args ...interface{}) ([]string, error) { if len(args) < 2 { log.Error("GetHashValueByHashKeyList key len less than two!") - return nil, errors.New("Key Is Empty") + return nil, errors.New("key is empty") } conn, err := m.getConn() if err != nil { @@ -550,11 +549,11 @@ func (m *RedisModule) GetMuchHash(args ...interface{}) ([]string, error) { return nil, err } if value == nil { - return nil, errors.New("Reids Get Hash nil") + return nil, errors.New("redis get hash nil") } valueList := value.([]interface{}) - retList := []string{} + var retList []string for _, valueItem := range valueList { valueByte, ok := valueItem.([]byte) if !ok { @@ -572,7 +571,7 @@ func (m *RedisModule) ScanMatchKeys(cursorValue int, redisKey string, count int) nextCursorValue := 0 if redisKey == "" { log.Error("ScanMatchKeys key is empty!") - return nextCursorValue, nil, errors.New("Key Is Empty") + return nextCursorValue, nil, errors.New("key is empty") } conn, err := m.getConn() @@ -587,7 +586,7 @@ func (m *RedisModule) ScanMatchKeys(cursorValue int, redisKey string, count int) return nextCursorValue, nil, err } if value == nil { - return nextCursorValue, nil, errors.New("Reids Get Hash nil") + return nextCursorValue, nil, errors.New("redis get hash nil") } valueList := value.([]interface{}) @@ -669,7 +668,7 @@ func (m *RedisModule) RPushListJSON(key interface{}, value ...interface{}) error // LPUSH和RPUSH func (m *RedisModule) setListPush(setType string, args ...interface{}) error { if setType != "LPUSH" && setType != "RPUSH" { - return errors.New("Redis List Push Type Error,Must Be LPUSH or RPUSH") + return errors.New("redis list push type error,must be LPUSH or RPUSH") } conn, err := m.getConn() if err != nil { @@ -697,7 +696,6 @@ func (m *RedisModule) setListJSONPush(setType string, key interface{}, value ... return m.setListPush(setType, args...) } -// Lrange ... func (m *RedisModule) LRangeList(key string, start, end int) ([]string, error) { conn, err := m.getConn() if err != nil { @@ -714,7 +712,7 @@ func (m *RedisModule) LRangeList(key string, start, end int) ([]string, error) { return redis.Strings(reply, err) } -// 获取List的长度 +// GetListLen 获取List的长度 func (m *RedisModule) GetListLen(key string) (int, error) { conn, err := m.getConn() if err != nil { @@ -730,7 +728,7 @@ func (m *RedisModule) GetListLen(key string) (int, error) { return redis.Int(reply, err) } -// 弹出List最后条记录 +// RPOPListValue 弹出List最后条记录 func (m *RedisModule) RPOPListValue(key string) (string, error) { conn, err := m.getConn() if err != nil { @@ -782,7 +780,7 @@ func (m *RedisModule) LRange(key string, start, stop int) ([]byte, error) { return makeListJson(reply.([]interface{}), false), nil } -// 弹出list(消息队列)数据,数据放入out fromLeft表示是否从左侧弹出 block表示是否阻塞 timeout表示阻塞超时 +// ListPopJson 弹出list(消息队列)数据,数据放入out fromLeft表示是否从左侧弹出 block表示是否阻塞 timeout表示阻塞超时 func (m *RedisModule) ListPopJson(key string, fromLeft, block bool, timeout int, out interface{}) error { b, err := m.ListPop(key, fromLeft, block, timeout) if err != nil { @@ -795,7 +793,7 @@ func (m *RedisModule) ListPopJson(key string, fromLeft, block bool, timeout int, return nil } -// 弹出list(消息队列)数据 fromLeft表示是否从左侧弹出 block表示是否阻塞 timeout表示阻塞超时 +// ListPop 弹出list(消息队列)数据 fromLeft表示是否从左侧弹出 block表示是否阻塞 timeout表示阻塞超时 func (m *RedisModule) ListPop(key string, fromLeft, block bool, timeout int) ([]byte, error) { cmd := "" if fromLeft { @@ -837,7 +835,7 @@ func (m *RedisModule) ListPop(key string, fromLeft, block bool, timeout int) ([] return b, nil } -// 有序集合插入Json +// ZADDInsertJson 有序集合插入Json func (m *RedisModule) ZADDInsertJson(key string, score float64, value interface{}) error { conn, err := m.getConn() @@ -857,7 +855,7 @@ func (m *RedisModule) ZADDInsertJson(key string, score float64, value interface{ return nil } -// 有序集合插入 +// ZADDInsert 有序集合插入 func (m *RedisModule) ZADDInsert(key string, score float64, Data interface{}) error { conn, err := m.getConn() if err != nil { @@ -897,7 +895,7 @@ func (m *RedisModule) ZRangeJSON(key string, start, stop int, ascend bool, withS return nil } -// 取有序set指定排名 ascend=true表示按升序遍历 否则按降序遍历 +// ZRange 取有序set指定排名 ascend=true表示按升序遍历 否则按降序遍历 func (m *RedisModule) ZRange(key string, start, stop int, ascend bool, withScores bool) ([]byte, error) { conn, err := m.getConn() if err != nil { @@ -921,7 +919,7 @@ func (m *RedisModule) ZRange(key string, start, stop int, ascend bool, withScore return makeListJson(reply.([]interface{}), withScores), nil } -// 获取有序集合长度 +// Zcard 获取有序集合长度 func (m *RedisModule) Zcard(key string) (int, error) { conn, err := m.getConn() if err != nil { @@ -1005,7 +1003,7 @@ func (m *RedisModule) ZRangeByScore(key string, start, stop float64, ascend bool return makeListJson(reply.([]interface{}), withScores), nil } -// 获取指定member的排名 +// ZScore 获取指定member的排名 func (m *RedisModule) ZScore(key string, member interface{}) (float64, error) { conn, err := m.getConn() if err != nil { @@ -1021,7 +1019,7 @@ func (m *RedisModule) ZScore(key string, member interface{}) (float64, error) { return redis.Float64(reply, err) } -// 获取指定member的排名 +// ZRank 获取指定member的排名 func (m *RedisModule) ZRank(key string, member interface{}, ascend bool) (int, error) { conn, err := m.getConn() if err != nil { @@ -1080,7 +1078,7 @@ func (m *RedisModule) ZREMMulti(key string, member ...interface{}) (int, error) func (m *RedisModule) HincrbyHashInt(redisKey, hashKey string, value int) error { if redisKey == "" || hashKey == "" { - return errors.New("Key Is Empty") + return errors.New("key is empty") } conn, err := m.getConn() if err != nil { @@ -1140,7 +1138,7 @@ func (m *RedisModule) Keys(key string) ([]string, error) { return nil, err } - strs := []string{} + var strs []string for _, val := range retList { strVal, ok := val.([]byte) if !ok { diff --git a/sysservice/httpservice/httpservice.go b/sysservice/httpservice/httpservice.go index ceba1b8..0f2c8ab 100644 --- a/sysservice/httpservice/httpservice.go +++ b/sysservice/httpservice/httpservice.go @@ -20,7 +20,6 @@ var DefaultReadTimeout time.Duration = time.Second * 10 var DefaultWriteTimeout time.Duration = time.Second * 10 var DefaultProcessTimeout time.Duration = time.Second * 10 -//http redirect type HttpRedirectData struct { Url string CookieList []*http.Cookie @@ -53,7 +52,7 @@ type IHttpRouter interface { POST(url string, handle HttpHandle) bool Router(session *HttpSession) - SetServeFile(method HTTP_METHOD, urlpath string, dirname string) error + SetServeFile(method HTTP_METHOD, urlPath string, dirname string) error SetFormFileKey(formFileKey string) GetFormFileKey() string AddHttpFiltrate(FiltrateFun HttpFiltrate) bool @@ -93,7 +92,7 @@ type HttpService struct { listenAddr string corsHeader *CORSHeader processTimeout time.Duration - manualStart bool + manualStart bool } type HttpFiltrate func(session *HttpSession) bool //true is pass @@ -118,7 +117,7 @@ func NewHttpHttpRouter() IHttpRouter { return httpRouter } -func (slf *HttpSession) GetRawQuery() string{ +func (slf *HttpSession) GetRawQuery() string { return slf.r.URL.RawQuery } @@ -215,7 +214,7 @@ func (slf *HttpRouter) analysisRouterUrl(url string) (string, error) { //替换所有空格 url = strings.ReplaceAll(url, " ", "") if len(url) <= 1 || url[0] != '/' { - return "", fmt.Errorf("url %s format is error!", url) + return "", fmt.Errorf("url %s format is error", url) } //去掉尾部的/ @@ -300,12 +299,12 @@ func (httpService *HttpService) SetHttpRouter(httpRouter IHttpRouter, eventHandl httpService.RegEventReceiverFunc(event.Sys_Event_Http_Event, eventHandler, httpService.HttpEventHandler) } -func (slf *HttpRouter) SetServeFile(method HTTP_METHOD, urlpath string, dirname string) error { +func (slf *HttpRouter) SetServeFile(method HTTP_METHOD, urlPath string, dirname string) error { _, err := os.Stat(dirname) if err != nil { return err } - matchURL, aErr := slf.analysisRouterUrl(urlpath) + matchURL, aErr := slf.analysisRouterUrl(urlPath) if aErr != nil { return aErr } @@ -350,15 +349,15 @@ func (slf *HttpSession) redirects() { func (httpService *HttpService) OnInit() error { iConfig := httpService.GetServiceCfg() if iConfig == nil { - return fmt.Errorf("%s service config is error!", httpService.GetName()) + return fmt.Errorf("%s service config is error", httpService.GetName()) } httpCfg := iConfig.(map[string]interface{}) addr, ok := httpCfg["ListenAddr"] if ok == false { - return fmt.Errorf("%s service config is error!", httpService.GetName()) + return fmt.Errorf("%s service config is error", httpService.GetName()) } - var readTimeout time.Duration = DefaultReadTimeout - var writeTimeout time.Duration = DefaultWriteTimeout + var readTimeout = DefaultReadTimeout + var writeTimeout = DefaultWriteTimeout if cfgRead, ok := httpCfg["ReadTimeout"]; ok == true { readTimeout = time.Duration(cfgRead.(float64)) * time.Millisecond @@ -370,8 +369,8 @@ func (httpService *HttpService) OnInit() error { if manualStart, ok := httpCfg["ManualStart"]; ok == true { httpService.manualStart = manualStart.(bool) - }else{ - manualStart =false + } else { + manualStart = false } httpService.processTimeout = DefaultProcessTimeout diff --git a/sysservice/messagequeueservice/CustomerSubscriber.go b/sysservice/messagequeueservice/CustomerSubscriber.go index 313b0d5..7867c1a 100644 --- a/sysservice/messagequeueservice/CustomerSubscriber.go +++ b/sysservice/messagequeueservice/CustomerSubscriber.go @@ -24,7 +24,7 @@ type CustomerSubscriber struct { subscribeMethod SubscribeMethod customerId string - isStop int32 //退出标记 + isStop int32 //退出标记 topicCache []TopicData // 从消息队列中取出来的消息的缓存 } @@ -62,13 +62,13 @@ func (cs *CustomerSubscriber) trySetSubscriberBaseInfo(rpcHandler rpc.IRpcHandle cs.serviceName = strRpcMethod[0] if cluster.HasService(fromNodeId, cs.serviceName) == false { - err := fmt.Errorf("nodeId %d cannot found %s", fromNodeId, cs.serviceName) + err := fmt.Errorf("nodeId %s cannot found %s", fromNodeId, cs.serviceName) log.SError(err.Error()) return err } if cluster.GetCluster().IsNodeConnected(fromNodeId) == false { - err := fmt.Errorf("nodeId %d is disconnect", fromNodeId) + err := fmt.Errorf("nodeId %s is disconnect", fromNodeId) log.SError(err.Error()) return err } @@ -84,7 +84,7 @@ func (cs *CustomerSubscriber) trySetSubscriberBaseInfo(rpcHandler rpc.IRpcHandle return nil } -// 开始订阅 +// Subscribe 开始订阅 func (cs *CustomerSubscriber) Subscribe(rpcHandler rpc.IRpcHandler, ss *Subscriber, topic string, subscribeMethod SubscribeMethod, customerId string, fromNodeId string, callBackRpcMethod string, startIndex uint64, oneBatchQuantity int32) error { err := cs.trySetSubscriberBaseInfo(rpcHandler, ss, topic, subscribeMethod, customerId, fromNodeId, callBackRpcMethod, startIndex, oneBatchQuantity) if err != nil { @@ -96,7 +96,7 @@ func (cs *CustomerSubscriber) Subscribe(rpcHandler rpc.IRpcHandler, ss *Subscrib return nil } -// 取消订阅 +// UnSubscribe 取消订阅 func (cs *CustomerSubscriber) UnSubscribe() { atomic.StoreInt32(&cs.isStop, 1) } @@ -163,14 +163,14 @@ func (cs *CustomerSubscriber) subscribe() bool { cs.publishToCustomer(topicData) return true } - + //从持久化数据中来找 - topicData = cs.subscriber.dataPersist.FindTopicData(cs.topic, cs.StartIndex, int64(cs.oneBatchQuantity),cs.topicCache[:0]) + topicData = cs.subscriber.dataPersist.FindTopicData(cs.topic, cs.StartIndex, int64(cs.oneBatchQuantity), cs.topicCache[:0]) return cs.publishToCustomer(topicData) } func (cs *CustomerSubscriber) checkCustomerIsValid() bool { - //1.检查nodeid是否在线,不在线,直接取消订阅 + //1.检查nodeId是否在线,不在线,直接取消订阅 if cluster.GetCluster().IsNodeConnected(cs.fromNodeId) == false { return false } @@ -213,7 +213,7 @@ func (cs *CustomerSubscriber) publishToCustomer(topicData []TopicData) bool { } //推送数据 - err := cs.CallNodeWithTimeout(4*time.Minute,cs.fromNodeId, cs.callBackRpcMethod, &dbQueuePublishReq, &dbQueuePushRes) + err := cs.CallNodeWithTimeout(4*time.Minute, cs.fromNodeId, cs.callBackRpcMethod, &dbQueuePublishReq, &dbQueuePushRes) if err != nil { time.Sleep(time.Second * 1) continue diff --git a/sysservice/messagequeueservice/MemoryQueue.go b/sysservice/messagequeueservice/MemoryQueue.go index b41f3cc..5ae8acc 100644 --- a/sysservice/messagequeueservice/MemoryQueue.go +++ b/sysservice/messagequeueservice/MemoryQueue.go @@ -19,7 +19,7 @@ func (mq *MemoryQueue) Init(cap int32) { mq.topicQueue = make([]TopicData, cap+1) } -// 从队尾Push数据 +// Push 从队尾Push数据 func (mq *MemoryQueue) Push(topicData *TopicData) bool { mq.locker.Lock() defer mq.locker.Unlock() @@ -51,7 +51,7 @@ func (mq *MemoryQueue) findData(startPos int32, startIndex uint64, limit int32) } else { findEndPos = int32(len(mq.topicQueue)) } - + if findStartPos >= findEndPos { return nil, false } @@ -87,21 +87,21 @@ func (mq *MemoryQueue) FindData(startIndex uint64, limit int32, dataQueue []Topi return nil, false } else if mq.head < mq.tail { // 队列没有折叠 - datas,ret := mq.findData(mq.head + 1, startIndex, limit) + datas, ret := mq.findData(mq.head+1, startIndex, limit) if ret { dataQueue = append(dataQueue, datas...) } return dataQueue, ret } else { // 折叠先找后面的部分 - datas,ret := mq.findData(mq.head+1, startIndex, limit) + datas, ret := mq.findData(mq.head+1, startIndex, limit) if ret { dataQueue = append(dataQueue, datas...) return dataQueue, ret } // 后面没找到,从前面开始找 - datas,ret = mq.findData(0, startIndex, limit) + datas, ret = mq.findData(0, startIndex, limit) dataQueue = append(dataQueue, datas...) return dataQueue, ret } diff --git a/sysservice/messagequeueservice/MongoPersist.go b/sysservice/messagequeueservice/MongoPersist.go index 4feead4..52519cd 100644 --- a/sysservice/messagequeueservice/MongoPersist.go +++ b/sysservice/messagequeueservice/MongoPersist.go @@ -77,7 +77,7 @@ func (mp *MongoPersist) OnInit() error { keys = append(keys, "Customer", "Topic") IndexKey = append(IndexKey, keys) s := mp.mongo.TakeSession() - if err := s.EnsureUniqueIndex(mp.dbName, CustomerCollectName, IndexKey, true, true,true); err != nil { + if err := s.EnsureUniqueIndex(mp.dbName, CustomerCollectName, IndexKey, true, true, true); err != nil { log.SError("EnsureUniqueIndex is fail ", err.Error()) return err } @@ -162,7 +162,7 @@ func (mp *MongoPersist) persistTopicData(collectionName string, topicData []Topi _, err := s.Collection(mp.dbName, collectionName).InsertMany(ctx, documents) if err != nil { - log.SError("PersistTopicData InsertMany fail,collect name is ", collectionName," error:",err.Error()) + log.SError("PersistTopicData InsertMany fail,collect name is ", collectionName, " error:", err.Error()) //失败最大重试数量 return retryCount >= mp.retryCount @@ -171,16 +171,16 @@ func (mp *MongoPersist) persistTopicData(collectionName string, topicData []Topi return true } -func (mp *MongoPersist) IsSameDay(timestamp1 int64,timestamp2 int64) bool{ +func (mp *MongoPersist) IsSameDay(timestamp1 int64, timestamp2 int64) bool { t1 := time.Unix(timestamp1, 0) t2 := time.Unix(timestamp2, 0) - return t1.Year() == t2.Year() && t1.Month() == t2.Month()&&t1.Day() == t2.Day() + return t1.Year() == t2.Year() && t1.Month() == t2.Month() && t1.Day() == t2.Day() } // PersistTopicData 持久化数据 func (mp *MongoPersist) PersistTopicData(topic string, topicData []TopicData, retryCount int) ([]TopicData, []TopicData, bool) { if len(topicData) == 0 { - return nil, nil,true + return nil, nil, true } preDate := topicData[0].Seq >> 32 @@ -188,7 +188,7 @@ func (mp *MongoPersist) PersistTopicData(topic string, topicData []TopicData, re for findPos = 1; findPos < len(topicData); findPos++ { newDate := topicData[findPos].Seq >> 32 //说明换天了 - if mp.IsSameDay(int64(preDate),int64(newDate)) == false { + if mp.IsSameDay(int64(preDate), int64(newDate)) == false { break } } @@ -201,14 +201,13 @@ func (mp *MongoPersist) PersistTopicData(topic string, topicData []TopicData, re } //如果成功 - return topicData[findPos:len(topicData)], topicData[0:findPos], true + return topicData[findPos:], topicData[0:findPos], true } // FindTopicData 查找数据 -func (mp *MongoPersist) findTopicData(topic string, startIndex uint64, limit int64,topicBuff []TopicData) ([]TopicData, bool) { +func (mp *MongoPersist) findTopicData(topic string, startIndex uint64, limit int64, topicBuff []TopicData) ([]TopicData, bool) { s := mp.mongo.TakeSession() - condition := bson.D{{Key: "_id", Value: bson.D{{Key: "$gt", Value: startIndex}}}} var findOption options.FindOptions @@ -238,11 +237,7 @@ func (mp *MongoPersist) findTopicData(topic string, startIndex uint64, limit int defer cancelAll() err = cursor.All(ctxAll, &res) if err != nil { - if err != nil { - log.SError("find collect name ", topic, " is error:", err.Error()) - return nil, false - } - + log.Error("find collect name ", topic, " is error", log.ErrorAttr("err", err)) return nil, false } @@ -251,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.SError("collect name ", topic, " Marshal is error:", err.Error()) + log.Error("collect name ", topic, " Marshal is error", log.ErrorAttr("err", err)) return nil, false } continue @@ -262,26 +257,26 @@ func (mp *MongoPersist) findTopicData(topic string, startIndex uint64, limit int return topicBuff, true } -func (mp *MongoPersist) IsYesterday(startIndex uint64) (bool,string){ - timeStamp := int64(startIndex>>32) +func (mp *MongoPersist) IsYesterday(startIndex uint64) (bool, string) { + timeStamp := int64(startIndex >> 32) - startTime := time.Unix(timeStamp, 0).AddDate(0,0,1) + startTime := time.Unix(timeStamp, 0).AddDate(0, 0, 1) nowTm := time.Now() - return startTime.Year() == nowTm.Year() && startTime.Month() == nowTm.Month()&&startTime.Day() == nowTm.Day(),nowTm.Format("20060102") + return startTime.Year() == nowTm.Year() && startTime.Month() == nowTm.Month() && startTime.Day() == nowTm.Day(), nowTm.Format("20060102") } -func (mp *MongoPersist) getCollectCount(topic string,today string) (int64 ,error){ +func (mp *MongoPersist) getCollectCount(topic string, today string) (int64, error) { s := mp.mongo.TakeSession() ctx, cancel := s.GetDefaultContext() defer cancel() collectName := fmt.Sprintf("%s_%s", topic, today) count, err := s.Collection(mp.dbName, collectName).EstimatedDocumentCount(ctx) - return count,err + return count, err } // FindTopicData 查找数据 -func (mp *MongoPersist) FindTopicData(topic string, startIndex uint64, limit int64,topicBuff []TopicData) []TopicData { +func (mp *MongoPersist) FindTopicData(topic string, startIndex uint64, limit int64, topicBuff []TopicData) []TopicData { //某表找不到,一直往前找,找到当前置为止 for days := 1; days <= MaxDays; days++ { //是否可以跳天 @@ -290,12 +285,12 @@ func (mp *MongoPersist) FindTopicData(topic string, startIndex uint64, limit int IsJumpDays := true //如果是昨天,先判断当天有没有表数据 - bYesterday,strToday := mp.IsYesterday(startIndex) - if bYesterday { - count,err := mp.getCollectCount(topic,strToday) + bYesterday, strToday := mp.IsYesterday(startIndex) + if bYesterday { + count, err := mp.getCollectCount(topic, strToday) if err != nil { //失败时,重新开始 - log.SError("getCollectCount ",topic,"_",strToday," is fail:",err.Error()) + log.SError("getCollectCount ", topic, "_", strToday, " is fail:", err.Error()) return nil } //当天没有记录,则不能跳表,有可能当天还有数据 @@ -305,9 +300,9 @@ func (mp *MongoPersist) FindTopicData(topic string, startIndex uint64, limit int } //从startIndex开始一直往后查 - topicData, isSucc := mp.findTopicData(topic, startIndex, limit,topicBuff) + topicData, ok := mp.findTopicData(topic, startIndex, limit, topicBuff) //有数据或者数据库出错时返回,返回后,会进行下一轮的查询遍历 - if len(topicData) > 0 || isSucc == false { + if len(topicData) > 0 || ok == false { return topicData } @@ -411,14 +406,14 @@ func (mp *MongoPersist) PersistIndex(topic string, customerId string, index uint condition := bson.D{{Key: "Customer", Value: customerId}, {Key: "Topic", Value: topic}} upsert := bson.M{"Customer": customerId, "Topic": topic, "Index": index} - updata := bson.M{"$set": upsert} + update := bson.M{"$set": upsert} var UpdateOptionsOpts []*options.UpdateOptions UpdateOptionsOpts = append(UpdateOptionsOpts, options.Update().SetUpsert(true)) ctx, cancel := s.GetDefaultContext() defer cancel() - _, err := s.Collection(mp.dbName, CustomerCollectName).UpdateOne(ctx, condition, updata, UpdateOptionsOpts...) + _, err := s.Collection(mp.dbName, CustomerCollectName).UpdateOne(ctx, condition, update, UpdateOptionsOpts...) if err != nil { log.SError("PersistIndex fail :", err.Error()) } diff --git a/sysservice/messagequeueservice/Subscriber.go b/sysservice/messagequeueservice/Subscriber.go index 4deff8c..5fcd749 100644 --- a/sysservice/messagequeueservice/Subscriber.go +++ b/sysservice/messagequeueservice/Subscriber.go @@ -7,7 +7,7 @@ import ( "sync" ) -// 订阅器 +// Subscriber 订阅器 type Subscriber struct { customerLocker sync.RWMutex mapCustomer map[string]*CustomerSubscriber diff --git a/sysservice/messagequeueservice/TopicRoom.go b/sysservice/messagequeueservice/TopicRoom.go index 26a8fc5..c0f7f34 100644 --- a/sysservice/messagequeueservice/TopicRoom.go +++ b/sysservice/messagequeueservice/TopicRoom.go @@ -43,7 +43,7 @@ type TopicRoom struct { isStop int32 } -// maxProcessTopicBacklogNum:主题最大积压数量 +// Init maxProcessTopicBacklogNum:主题最大积压数量 func (tr *TopicRoom) Init(maxTopicBacklogNum int32, memoryQueueLen int32, topic string, queueWait *sync.WaitGroup, dataPersist QueueDataPersist) { if maxTopicBacklogNum == 0 { maxTopicBacklogNum = DefaultMaxTopicBacklogNum @@ -116,18 +116,18 @@ func (tr *TopicRoom) topicRoomRun() { for retryCount := 0; retryCount < maxTryPersistNum; { //持久化处理 stagingBuff, savedBuff, ret := tr.PersistTopicData(tr.topic, stagingBuff, retryCount+1) - + if ret == true { // 1. 把成功存储的数据放入内存中 if len(savedBuff) > 0 { tr.PushTopicDataToQueue(tr.topic, savedBuff) } - + // 2. 如果存档成功,并且有后续批次,则继续存档 - if ret == true && len(stagingBuff) > 0 { + if len(stagingBuff) > 0 { continue } - + // 3. 成功了,跳出 break } else { diff --git a/sysservice/rankservice/MongodbPersist.go b/sysservice/rankservice/MongodbPersist.go index 1777ba8..aa72452 100644 --- a/sysservice/rankservice/MongodbPersist.go +++ b/sysservice/rankservice/MongodbPersist.go @@ -14,37 +14,37 @@ import ( "time" ) -const batchRemoveNum = 128 //一切删除的最大数量 +const batchRemoveNum = 128 //一切删除的最大数量 // RankDataDB 排行表数据 type RankDataDB struct { - Id uint64 `bson:"_id"` - RefreshTime int64 `bson:"RefreshTime"` - SortData []int64 `bson:"SortData"` - Data []byte `bson:"Data"` - ExData []int64 `bson:"ExData"` + Id uint64 `bson:"_id"` + RefreshTime int64 `bson:"RefreshTime"` + SortData []int64 `bson:"SortData"` + Data []byte `bson:"Data"` + ExData []int64 `bson:"ExData"` } -// MongoPersist持久化Module +// MongoPersist 持久化Module type MongoPersist struct { service.Module mongo mongodbmodule.MongoModule - url string //Mongodb连接url - dbName string //数据库名称 - SaveInterval time.Duration //落地数据库时间间隔 + url string //Mongodb连接url + dbName string //数据库名称 + SaveInterval time.Duration //落地数据库时间间隔 sync.Mutex mapRemoveRankData map[uint64]map[uint64]struct{} //将要删除的排行数据 map[RankId]map[Key]struct{} mapUpsertRankData map[uint64]map[uint64]RankData //需要upsert的排行数据 map[RankId][key]RankData - mapRankSkip map[uint64]IRankSkip //所有的排行榜对象map[RankId]IRankSkip - maxRetrySaveCount int //存档重试次数 - retryTimeIntervalMs time.Duration //重试时间间隔 + mapRankSkip map[uint64]IRankSkip //所有的排行榜对象map[RankId]IRankSkip + maxRetrySaveCount int //存档重试次数 + retryTimeIntervalMs time.Duration //重试时间间隔 - lastSaveTime time.Time //最后一次存档时间 + lastSaveTime time.Time //最后一次存档时间 - stop int32 //是否停服 + stop int32 //是否停服 waitGroup sync.WaitGroup //等待停服 } @@ -84,12 +84,12 @@ func (mp *MongoPersist) ReadCfg() error { } //读取数据库配置 - saveMongoCfg,ok := mapDBServiceCfg["SaveMongo"] + saveMongoCfg, ok := mapDBServiceCfg["SaveMongo"] if ok == false { return fmt.Errorf("RankService.SaveMongo config is error") } - mongodbCfg,ok := saveMongoCfg.(map[string]interface{}) + mongodbCfg, ok := saveMongoCfg.(map[string]interface{}) if ok == false { return fmt.Errorf("RankService.SaveMongo config is error") } @@ -111,7 +111,7 @@ func (mp *MongoPersist) ReadCfg() error { return fmt.Errorf("RankService.SaveMongo.SaveIntervalMs config is error") } - mp.SaveInterval = time.Duration(saveInterval.(float64))*time.Millisecond + mp.SaveInterval = time.Duration(saveInterval.(float64)) * time.Millisecond maxRetrySaveCount, ok := mongodbCfg["MaxRetrySaveCount"] if ok == false { @@ -123,16 +123,16 @@ func (mp *MongoPersist) ReadCfg() error { if ok == false { return fmt.Errorf("RankService.SaveMongo.RetryTimeIntervalMs config is error") } - mp.retryTimeIntervalMs = time.Duration(retryTimeIntervalMs.(float64))*time.Millisecond + mp.retryTimeIntervalMs = time.Duration(retryTimeIntervalMs.(float64)) * time.Millisecond return nil } -//启服从数据库加载 +// OnStart 启服从数据库加载 func (mp *MongoPersist) OnStart() { } -func (mp *MongoPersist) OnSetupRank(manual bool,rankSkip *RankSkip) error{ +func (mp *MongoPersist) OnSetupRank(manual bool, rankSkip *RankSkip) error { if mp.mapRankSkip == nil { mp.mapRankSkip = map[uint64]IRankSkip{} } @@ -142,17 +142,17 @@ func (mp *MongoPersist) OnSetupRank(manual bool,rankSkip *RankSkip) error{ return nil } - log.Info("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()) + log.Info("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.Info("finish load rank ", rankSkip.GetRankName(), " from mongodb.") return nil } -func (mp *MongoPersist) loadFromDB(rankId uint64,rankCollectName string) error{ +func (mp *MongoPersist) loadFromDB(rankId uint64, rankCollectName string) error { s := mp.mongo.TakeSession() ctx, cancel := s.GetDefaultContext() defer cancel() @@ -164,14 +164,14 @@ func (mp *MongoPersist) loadFromDB(rankId uint64,rankCollectName string) error{ return err } - if cursor.Err()!=nil { + if cursor.Err() != nil { log.SError("find collect name ", rankCollectName, " is error:", cursor.Err().Error()) return err } rankSkip := mp.mapRankSkip[rankId] if rankSkip == nil { - err = fmt.Errorf("rank ", rankCollectName, " is not setup:") + err = fmt.Errorf("rank %s is not setup", rankCollectName) log.SError(err.Error()) return err } @@ -189,69 +189,68 @@ func (mp *MongoPersist) loadFromDB(rankId uint64,rankCollectName string) error{ rankData.Data = rankDataDB.Data rankData.Key = rankDataDB.Id rankData.SortData = rankDataDB.SortData - for _,eData := range rankDataDB.ExData{ - rankData.ExData = append(rankData.ExData,&rpc.ExtendIncData{InitValue:eData}) + for _, eData := range rankDataDB.ExData { + rankData.ExData = append(rankData.ExData, &rpc.ExtendIncData{InitValue: eData}) } //更新到排行榜 - rankSkip.UpsetRank(&rankData,rankDataDB.RefreshTime,true) + rankSkip.UpsetRank(&rankData, rankDataDB.RefreshTime, true) } return nil } -func (mp *MongoPersist) lazyInitRemoveMap(rankId uint64){ +func (mp *MongoPersist) lazyInitRemoveMap(rankId uint64) { if mp.mapRemoveRankData[rankId] == nil { - mp.mapRemoveRankData[rankId] = make(map[uint64]struct{},256) + mp.mapRemoveRankData[rankId] = make(map[uint64]struct{}, 256) } } -func (mp *MongoPersist) lazyInitUpsertMap(rankId uint64){ +func (mp *MongoPersist) lazyInitUpsertMap(rankId uint64) { if mp.mapUpsertRankData[rankId] == nil { - mp.mapUpsertRankData[rankId] = make(map[uint64]RankData,256) + mp.mapUpsertRankData[rankId] = make(map[uint64]RankData, 256) } } -func (mp *MongoPersist) OnEnterRank(rankSkip IRankSkip, enterData *RankData){ +func (mp *MongoPersist) OnEnterRank(rankSkip IRankSkip, enterData *RankData) { mp.Lock() defer mp.Unlock() - delete(mp.mapRemoveRankData,enterData.Key) + delete(mp.mapRemoveRankData, enterData.Key) mp.lazyInitUpsertMap(rankSkip.GetRankID()) mp.mapUpsertRankData[rankSkip.GetRankID()][enterData.Key] = *enterData } - -func (mp *MongoPersist) OnLeaveRank(rankSkip IRankSkip, leaveData *RankData){ +func (mp *MongoPersist) OnLeaveRank(rankSkip IRankSkip, leaveData *RankData) { mp.Lock() defer mp.Unlock() //先删掉更新中的数据 - delete(mp.mapUpsertRankData,leaveData.Key) + delete(mp.mapUpsertRankData, leaveData.Key) mp.lazyInitRemoveMap(rankSkip.GetRankID()) mp.mapRemoveRankData[rankSkip.GetRankID()][leaveData.Key] = struct{}{} } -func (mp *MongoPersist) OnChangeRankData(rankSkip IRankSkip, changeData *RankData){ +func (mp *MongoPersist) OnChangeRankData(rankSkip IRankSkip, changeData *RankData) { mp.Lock() defer mp.Unlock() //先删掉要删除的数据 - delete(mp.mapRemoveRankData,changeData.Key) + delete(mp.mapRemoveRankData, changeData.Key) //更新数据 mp.lazyInitUpsertMap(rankSkip.GetRankID()) mp.mapUpsertRankData[rankSkip.GetRankID()][changeData.Key] = *changeData } -//停存持久化到DB -func (mp *MongoPersist) OnStop(mapRankSkip map[uint64]*RankSkip){ - atomic.StoreInt32(&mp.stop,1) +// OnStop 停存持久化到DB +func (mp *MongoPersist) OnStop(mapRankSkip map[uint64]*RankSkip) { + atomic.StoreInt32(&mp.stop, 1) mp.waitGroup.Wait() } -func (mp *MongoPersist) JugeTimeoutSave() bool{ +func (mp *MongoPersist) JudgeTimeoutSave() bool { timeout := time.Now() isTimeOut := timeout.Sub(mp.lastSaveTime) >= mp.SaveInterval if isTimeOut == true { @@ -261,18 +260,18 @@ func (mp *MongoPersist) JugeTimeoutSave() bool{ return isTimeOut } -func (mp *MongoPersist) persistCoroutine(){ +func (mp *MongoPersist) persistCoroutine() { defer mp.waitGroup.Done() - for atomic.LoadInt32(&mp.stop)==0 { + for atomic.LoadInt32(&mp.stop) == 0 { //间隔时间sleep - time.Sleep(time.Second*1) + time.Sleep(time.Second * 1) //没有持久化数据continue if mp.hasPersistData() == false { continue } - if mp.JugeTimeoutSave() == false{ + if mp.JudgeTimeoutSave() == false { continue } @@ -284,20 +283,20 @@ func (mp *MongoPersist) persistCoroutine(){ mp.saveToDB() } -func (mp *MongoPersist) hasPersistData() bool{ +func (mp *MongoPersist) hasPersistData() bool { mp.Lock() defer mp.Unlock() - return len(mp.mapUpsertRankData)>0 || len(mp.mapRemoveRankData) >0 + return len(mp.mapUpsertRankData) > 0 || len(mp.mapRemoveRankData) > 0 } -func (mp *MongoPersist) saveToDB(){ +func (mp *MongoPersist) saveToDB() { 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.Dump(string(buf[:l]), log.String("error", errString)) } }() @@ -314,13 +313,13 @@ func (mp *MongoPersist) saveToDB(){ mp.upsertRankDataToDB(mapUpsertRankData) } - for len(mapRemoveRankData) >0 { + for len(mapRemoveRankData) > 0 { mp.removeRankDataToDB(mapRemoveRankData) } } -func (mp *MongoPersist) removeToDB(collectName string,keys []uint64) error{ +func (mp *MongoPersist) removeToDB(collectName string, keys []uint64) error { s := mp.mongo.TakeSession() ctx, cancel := s.GetDefaultContext() defer cancel() @@ -336,16 +335,16 @@ func (mp *MongoPersist) removeToDB(collectName string,keys []uint64) error{ return nil } -func (mp *MongoPersist) removeRankData(rankId uint64,keys []uint64) bool { +func (mp *MongoPersist) removeRankData(rankId uint64, keys []uint64) bool { rank := mp.mapRankSkip[rankId] - if rank== nil { - log.SError("cannot find rankId ",rankId,"config") + if rank == nil { + log.SError("cannot find rankId ", rankId, "config") return false } //不成功则重试maxRetrySaveCount次 - for i:=0;i= batchRemoveNum { - break - } - } + keyList := make([]uint64, 0, batchRemoveNum) + for key := range mapRemoveKey { + delete(mapRemoveKey, key) + keyList = append(keyList, key) + if len(keyList) >= batchRemoveNum { + break + } + } - mp.removeRankData(rankId,keyList) + mp.removeRankData(rankId, keyList) - //如果删完,删掉rankid下所有 - if len(mapRemoveKey) == 0 { - delete(mapRemoveRankData,rankId) - } + if len(mapRemoveKey) == 0 { + delete(mapRemoveRankData, rankId) + } } } diff --git a/sysservice/rankservice/RankInterface.go b/sysservice/rankservice/RankInterface.go index ba356e9..70b740a 100644 --- a/sysservice/rankservice/RankInterface.go +++ b/sysservice/rankservice/RankInterface.go @@ -1,8 +1,8 @@ package rankservice import ( - "github.com/duanhf2012/origin/v2/service" "github.com/duanhf2012/origin/v2/rpc" + "github.com/duanhf2012/origin/v2/service" ) type RankDataChangeType int8 @@ -11,19 +11,18 @@ type IRankSkip interface { GetRankID() uint64 GetRankName() string GetRankLen() uint64 - UpsetRank(upsetData *rpc.RankData,refreshTimestamp int64,fromLoad bool) RankDataChangeType + UpsetRank(upsetData *rpc.RankData, refreshTimestamp int64, fromLoad bool) RankDataChangeType } type IRankModule interface { service.IModule - - OnSetupRank(manual bool,rankSkip *RankSkip) error //当完成安装排行榜对象时 - OnStart() //服务开启时回调 + OnSetupRank(manual bool, rankSkip *RankSkip) error //当完成安装排行榜对象时 + OnStart() //服务开启时回调 OnEnterRank(rankSkip IRankSkip, enterData *RankData) //进入排行 OnLeaveRank(rankSkip IRankSkip, leaveData *RankData) //离开排行 OnChangeRankData(rankSkip IRankSkip, changeData *RankData) //当排行数据变化时 - OnStop(mapRankSkip map[uint64]*RankSkip) //服务结束时回调 + OnStop(mapRankSkip map[uint64]*RankSkip) //服务结束时回调 } type DefaultRankModule struct { diff --git a/sysservice/rankservice/RankService.go b/sysservice/rankservice/RankService.go index 80f7083..d0e977d 100644 --- a/sysservice/rankservice/RankService.go +++ b/sysservice/rankservice/RankService.go @@ -45,7 +45,7 @@ func (rs *RankService) OnRelease() { rs.rankModule.OnStop(rs.mapRankSkip) } -// 安装排行模块 +// SetupRankModule 安装排行模块 func (rs *RankService) SetupRankModule(rankModule IRankModule) { rs.rankModule = rankModule } diff --git a/sysservice/rankservice/RankSkip.go b/sysservice/rankservice/RankSkip.go index b92a19c..f1560c7 100644 --- a/sysservice/rankservice/RankSkip.go +++ b/sysservice/rankservice/RankSkip.go @@ -342,7 +342,7 @@ func (rs *RankSkip) GetRankNodeData(findKey uint64) (*RankData, uint64) { return rankNode, index + 1 } -// GetRankNodeDataByPos 获取,返回排名节点与名次 +// GetRankNodeDataByRank 获取,返回排名节点与名次 func (rs *RankSkip) GetRankNodeDataByRank(rank uint64) (*RankData, uint64) { rs.pickExpireKey() rankNode := rs.skipList.ByPosition(rank - 1) @@ -382,7 +382,7 @@ func (rs *RankSkip) GetRankKeyPrevToLimit(findKey, count uint64, result *rpc.Ran return nil } -// GetRankKeyPrevToLimit 获取key前count名的数据 +// GetRankKeyNextToLimit 获取key前count名的数据 func (rs *RankSkip) GetRankKeyNextToLimit(findKey, count uint64, result *rpc.RankDataList) error { if rs.GetRankLen() <= 0 { return fmt.Errorf("rank[%d] no data", rs.rankId) @@ -411,7 +411,7 @@ func (rs *RankSkip) GetRankKeyNextToLimit(findKey, count uint64, result *rpc.Ran return nil } -// GetRankList 获取排行榜数据,startPos开始的count个数据 +// GetRankDataFromToLimit 获取排行榜数据,startPos开始的count个数据 func (rs *RankSkip) GetRankDataFromToLimit(startPos, count uint64, result *rpc.RankDataList) error { if rs.GetRankLen() <= 0 { //初始排行榜可能没有数据 diff --git a/sysservice/tcpservice/tcpservice.go b/sysservice/tcpservice/tcpservice.go index 7c9db6f..a21af28 100644 --- a/sysservice/tcpservice/tcpservice.go +++ b/sysservice/tcpservice/tcpservice.go @@ -8,11 +8,11 @@ import ( "github.com/duanhf2012/origin/v2/network/processor" "github.com/duanhf2012/origin/v2/service" "github.com/duanhf2012/origin/v2/util/bytespool" - "runtime" - "sync" "github.com/google/uuid" - "time" + "runtime" "strings" + "sync" + "time" ) type TcpService struct { @@ -20,79 +20,80 @@ type TcpService struct { service.Service mapClientLocker sync.RWMutex - mapClient map[string] *Client - process processor.IProcessor + mapClient map[string]*Client + process processor.IProcessor } type TcpPackType int8 -const( - TPT_Connected TcpPackType = 0 + +const ( + TPT_Connected TcpPackType = 0 TPT_DisConnected TcpPackType = 1 - TPT_Pack TcpPackType = 2 - TPT_UnknownPack TcpPackType = 3 + TPT_Pack TcpPackType = 2 + TPT_UnknownPack TcpPackType = 3 ) type TcpPack struct { - Type TcpPackType //0表示连接 1表示断开 2表示数据 - ClientId string - Data interface{} + Type TcpPackType //0表示连接 1表示断开 2表示数据 + ClientId string + Data interface{} } type Client struct { - id string - tcpConn *network.TCPConn + id string + tcpConn *network.TCPConn tcpService *TcpService } -func (tcpService *TcpService) OnInit() error{ +func (tcpService *TcpService) OnInit() error { iConfig := tcpService.GetServiceCfg() if iConfig == nil { - return fmt.Errorf("%s service config is error!", tcpService.GetName()) + return fmt.Errorf("%s service config is error", tcpService.GetName()) } tcpCfg := iConfig.(map[string]interface{}) - addr,ok := tcpCfg["ListenAddr"] + addr, ok := tcpCfg["ListenAddr"] if ok == false { - return fmt.Errorf("%s service config is error!", tcpService.GetName()) + return fmt.Errorf("%s service config is error", tcpService.GetName()) } tcpService.tcpServer.Addr = addr.(string) - MaxConnNum,ok := tcpCfg["MaxConnNum"] + MaxConnNum, ok := tcpCfg["MaxConnNum"] if ok == true { tcpService.tcpServer.MaxConnNum = int(MaxConnNum.(float64)) } - - PendingWriteNum,ok := tcpCfg["PendingWriteNum"] + + PendingWriteNum, ok := tcpCfg["PendingWriteNum"] if ok == true { tcpService.tcpServer.PendingWriteNum = int(PendingWriteNum.(float64)) } - LittleEndian,ok := tcpCfg["LittleEndian"] + LittleEndian, ok := tcpCfg["LittleEndian"] if ok == true { tcpService.tcpServer.LittleEndian = LittleEndian.(bool) } - LenMsgLen,ok := tcpCfg["LenMsgLen"] + LenMsgLen, ok := tcpCfg["LenMsgLen"] if ok == true { tcpService.tcpServer.LenMsgLen = int(LenMsgLen.(float64)) } - MinMsgLen,ok := tcpCfg["MinMsgLen"] + MinMsgLen, ok := tcpCfg["MinMsgLen"] if ok == true { tcpService.tcpServer.MinMsgLen = uint32(MinMsgLen.(float64)) } - MaxMsgLen,ok := tcpCfg["MaxMsgLen"] + MaxMsgLen, ok := tcpCfg["MaxMsgLen"] if ok == true { tcpService.tcpServer.MaxMsgLen = uint32(MaxMsgLen.(float64)) } - readDeadline,ok := tcpCfg["ReadDeadline"] + readDeadline, ok := tcpCfg["ReadDeadline"] if ok == true { - tcpService.tcpServer.ReadDeadline = time.Second*time.Duration(readDeadline.(float64)) + tcpService.tcpServer.ReadDeadline = time.Second * time.Duration(readDeadline.(float64)) } - writeDeadline,ok := tcpCfg["WriteDeadline"] + writeDeadline, ok := tcpCfg["WriteDeadline"] if ok == true { - tcpService.tcpServer.WriteDeadline = time.Second*time.Duration(writeDeadline.(float64)) + tcpService.tcpServer.WriteDeadline = time.Second * time.Duration(writeDeadline.(float64)) } - tcpService.mapClient = make( map[string] *Client, tcpService.tcpServer.MaxConnNum) + tcpService.mapClient = make(map[string]*Client, tcpService.tcpServer.MaxConnNum) tcpService.tcpServer.NewAgent = tcpService.NewClient tcpService.tcpServer.Start() @@ -107,26 +108,25 @@ func (tcpService *TcpService) TcpEventHandler(ev event.IEvent) { case TPT_DisConnected: tcpService.process.DisConnectedRoute(pack.ClientId) case TPT_UnknownPack: - tcpService.process.UnknownMsgRoute(pack.ClientId,pack.Data,tcpService.recyclerReaderBytes) + tcpService.process.UnknownMsgRoute(pack.ClientId, pack.Data, tcpService.recyclerReaderBytes) case TPT_Pack: - tcpService.process.MsgRoute(pack.ClientId,pack.Data,tcpService.recyclerReaderBytes) + tcpService.process.MsgRoute(pack.ClientId, pack.Data, tcpService.recyclerReaderBytes) } } func (tcpService *TcpService) recyclerReaderBytes(data []byte) { } - -func (tcpService *TcpService) SetProcessor(process processor.IProcessor,handler event.IEventHandler){ +func (tcpService *TcpService) SetProcessor(process processor.IProcessor, handler event.IEventHandler) { tcpService.process = process - tcpService.RegEventReceiverFunc(event.Sys_Event_Tcp,handler, tcpService.TcpEventHandler) + tcpService.RegEventReceiverFunc(event.Sys_Event_Tcp, handler, tcpService.TcpEventHandler) } func (tcpService *TcpService) NewClient(conn *network.TCPConn) network.Agent { tcpService.mapClientLocker.Lock() defer tcpService.mapClientLocker.Unlock() - uuId,_ := uuid.NewUUID() + uuId, _ := uuid.NewUUID() clientId := strings.ReplaceAll(uuId.String(), "-", "") pClient := &Client{tcpConn: conn, id: clientId} pClient.tcpService = tcpService @@ -145,49 +145,49 @@ func (slf *Client) Run() { buf := make([]byte, 4096) l := runtime.Stack(buf, false) errString := fmt.Sprint(r) - log.Dump(string(buf[:l]),log.String("error",errString)) + log.Dump(string(buf[:l]), log.String("error", errString)) } }() - slf.tcpService.NotifyEvent(&event.Event{Type:event.Sys_Event_Tcp,Data:TcpPack{ClientId:slf.id,Type:TPT_Connected}}) - for{ + slf.tcpService.NotifyEvent(&event.Event{Type: event.Sys_Event_Tcp, Data: TcpPack{ClientId: slf.id, Type: TPT_Connected}}) + for { if slf.tcpConn == nil { break } slf.tcpConn.SetReadDeadline(slf.tcpService.tcpServer.ReadDeadline) - bytes,err := slf.tcpConn.ReadMsg() + 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.ErrorAttr("error", err), log.String("clientId", slf.id)) break } - data,err:=slf.tcpService.process.Unmarshal(slf.id,bytes) + data, err := slf.tcpService.process.Unmarshal(slf.id, bytes) if err != nil { - slf.tcpService.NotifyEvent(&event.Event{Type:event.Sys_Event_Tcp,Data:TcpPack{ClientId:slf.id,Type:TPT_UnknownPack,Data:bytes}}) + slf.tcpService.NotifyEvent(&event.Event{Type: event.Sys_Event_Tcp, Data: TcpPack{ClientId: slf.id, Type: TPT_UnknownPack, Data: bytes}}) continue } - slf.tcpService.NotifyEvent(&event.Event{Type:event.Sys_Event_Tcp,Data:TcpPack{ClientId:slf.id,Type:TPT_Pack,Data:data}}) + slf.tcpService.NotifyEvent(&event.Event{Type: event.Sys_Event_Tcp, Data: TcpPack{ClientId: slf.id, Type: TPT_Pack, Data: data}}) } } -func (slf *Client) OnClose(){ - slf.tcpService.NotifyEvent(&event.Event{Type:event.Sys_Event_Tcp,Data:TcpPack{ClientId:slf.id,Type:TPT_DisConnected}}) +func (slf *Client) OnClose() { + slf.tcpService.NotifyEvent(&event.Event{Type: event.Sys_Event_Tcp, Data: TcpPack{ClientId: slf.id, Type: TPT_DisConnected}}) slf.tcpService.mapClientLocker.Lock() defer slf.tcpService.mapClientLocker.Unlock() - delete (slf.tcpService.mapClient,slf.GetId()) + delete(slf.tcpService.mapClient, slf.GetId()) } -func (tcpService *TcpService) SendMsg(clientId string,msg interface{}) error{ +func (tcpService *TcpService) SendMsg(clientId string, msg interface{}) error { tcpService.mapClientLocker.Lock() - client,ok := tcpService.mapClient[clientId] - if ok == false{ + client, ok := tcpService.mapClient[clientId] + if ok == false { tcpService.mapClientLocker.Unlock() - return fmt.Errorf("client %d is disconnect!",clientId) + return fmt.Errorf("client %s is disconnect", clientId) } tcpService.mapClientLocker.Unlock() - bytes,err := tcpService.process.Marshal(clientId,msg) + bytes, err := tcpService.process.Marshal(clientId, msg) if err != nil { return err } @@ -198,53 +198,51 @@ func (tcpService *TcpService) Close(clientId string) { tcpService.mapClientLocker.Lock() defer tcpService.mapClientLocker.Unlock() - client,ok := tcpService.mapClient[clientId] - if ok == false{ + client, ok := tcpService.mapClient[clientId] + if ok == false { return } - if client.tcpConn!=nil { + if client.tcpConn != nil { client.tcpConn.Close() } return } -func (tcpService *TcpService) GetClientIp(clientid string) string{ +func (tcpService *TcpService) GetClientIp(clientId string) string { tcpService.mapClientLocker.Lock() defer tcpService.mapClientLocker.Unlock() - pClient,ok := tcpService.mapClient[clientid] - if ok == false{ + pClient, ok := tcpService.mapClient[clientId] + if ok == false { return "" } return pClient.tcpConn.GetRemoteIp() } - -func (tcpService *TcpService) SendRawMsg(clientId string,msg []byte) error{ +func (tcpService *TcpService) SendRawMsg(clientId string, msg []byte) error { tcpService.mapClientLocker.Lock() - client,ok := tcpService.mapClient[clientId] - if ok == false{ + client, ok := tcpService.mapClient[clientId] + if ok == false { tcpService.mapClientLocker.Unlock() - return fmt.Errorf("client %d is disconnect!",clientId) + return fmt.Errorf("client %s is disconnect", clientId) } tcpService.mapClientLocker.Unlock() return client.tcpConn.WriteMsg(msg) } -func (tcpService *TcpService) SendRawData(clientId string,data []byte) error{ +func (tcpService *TcpService) SendRawData(clientId string, data []byte) error { tcpService.mapClientLocker.Lock() - client,ok := tcpService.mapClient[clientId] - if ok == false{ + client, ok := tcpService.mapClient[clientId] + if ok == false { tcpService.mapClientLocker.Unlock() - return fmt.Errorf("client %d is disconnect!",clientId) + return fmt.Errorf("client %s is disconnect", clientId) } tcpService.mapClientLocker.Unlock() return client.tcpConn.WriteRawMsg(data) } - func (tcpService *TcpService) GetConnNum() int { tcpService.mapClientLocker.Lock() connNum := len(tcpService.mapClient) @@ -252,14 +250,14 @@ func (tcpService *TcpService) GetConnNum() int { return connNum } -func (server *TcpService) SetNetMempool(mempool bytespool.IBytesMempool){ - server.tcpServer.SetNetMempool(mempool) +func (tcpService *TcpService) SetNetMemPool(memPool bytespool.IBytesMemPool) { + tcpService.tcpServer.SetNetMemPool(memPool) } -func (server *TcpService) GetNetMempool() bytespool.IBytesMempool { - return server.tcpServer.GetNetMempool() +func (tcpService *TcpService) GetNetMemPool() bytespool.IBytesMemPool { + return tcpService.tcpServer.GetNetMemPool() } -func (server *TcpService) ReleaseNetMem(byteBuff []byte) { - server.tcpServer.GetNetMempool().ReleaseBytes(byteBuff) +func (tcpService *TcpService) ReleaseNetMem(byteBuff []byte) { + tcpService.tcpServer.GetNetMemPool().ReleaseBytes(byteBuff) } diff --git a/sysservice/wsservice/wsservice.go b/sysservice/wsservice/wsservice.go index 54e031d..8dfc704 100644 --- a/sysservice/wsservice/wsservice.go +++ b/sysservice/wsservice/wsservice.go @@ -7,9 +7,9 @@ import ( "github.com/duanhf2012/origin/v2/network" "github.com/duanhf2012/origin/v2/network/processor" "github.com/duanhf2012/origin/v2/service" - "sync" "github.com/google/uuid" "strings" + "sync" ) type WSService struct { @@ -17,16 +17,17 @@ type WSService struct { wsServer network.WSServer mapClientLocker sync.RWMutex - mapClient map[string] *WSClient + mapClient map[string]*WSClient process processor.IProcessor } type WSPackType int8 -const( - WPT_Connected WSPackType = 0 + +const ( + WPT_Connected WSPackType = 0 WPT_DisConnected WSPackType = 1 - WPT_Pack WSPackType = 2 - WPT_UnknownPack WSPackType = 3 + WPT_Pack WSPackType = 2 + WPT_UnknownPack WSPackType = 3 ) const Default_WS_MaxConnNum = 3000 @@ -34,8 +35,8 @@ const Default_WS_PendingWriteNum = 10000 const Default_WS_MaxMsgLen = 65535 type WSClient struct { - id string - wsConn *network.WSConn + id string + wsConn *network.WSConn wsService *WSService } @@ -46,44 +47,44 @@ type WSPack struct { Data interface{} } -func (ws *WSService) OnInit() error{ +func (ws *WSService) OnInit() error { iConfig := ws.GetServiceCfg() if iConfig == nil { - return fmt.Errorf("%s service config is error!", ws.GetName()) + return fmt.Errorf("%s service config is error", ws.GetName()) } wsCfg := iConfig.(map[string]interface{}) - addr,ok := wsCfg["ListenAddr"] + addr, ok := wsCfg["ListenAddr"] if ok == false { - return fmt.Errorf("%s service config is error!", ws.GetName()) + return fmt.Errorf("%s service config is error", ws.GetName()) } ws.wsServer.Addr = addr.(string) ws.wsServer.MaxConnNum = Default_WS_MaxConnNum ws.wsServer.PendingWriteNum = Default_WS_PendingWriteNum ws.wsServer.MaxMsgLen = Default_WS_MaxMsgLen - MaxConnNum,ok := wsCfg["MaxConnNum"] + MaxConnNum, ok := wsCfg["MaxConnNum"] if ok == true { ws.wsServer.MaxConnNum = int(MaxConnNum.(float64)) } - PendingWriteNum,ok := wsCfg["PendingWriteNum"] + PendingWriteNum, ok := wsCfg["PendingWriteNum"] if ok == true { ws.wsServer.PendingWriteNum = int(PendingWriteNum.(float64)) } - MaxMsgLen,ok := wsCfg["MaxMsgLen"] + MaxMsgLen, ok := wsCfg["MaxMsgLen"] if ok == true { ws.wsServer.MaxMsgLen = uint32(MaxMsgLen.(float64)) } - ws.mapClient = make( map[string] *WSClient, ws.wsServer.MaxConnNum) + ws.mapClient = make(map[string]*WSClient, ws.wsServer.MaxConnNum) ws.wsServer.NewAgent = ws.NewWSClient ws.wsServer.Start() return nil } -func (ws *WSService) SetMessageType(messageType int){ +func (ws *WSService) SetMessageType(messageType int) { ws.wsServer.SetMessageType(messageType) } @@ -95,15 +96,15 @@ func (ws *WSService) WSEventHandler(ev event.IEvent) { case WPT_DisConnected: pack.MsgProcessor.DisConnectedRoute(pack.ClientId) case WPT_UnknownPack: - pack.MsgProcessor.UnknownMsgRoute(pack.ClientId,pack.Data,ws.recyclerReaderBytes) + pack.MsgProcessor.UnknownMsgRoute(pack.ClientId, pack.Data, ws.recyclerReaderBytes) case WPT_Pack: - pack.MsgProcessor.MsgRoute(pack.ClientId,pack.Data,ws.recyclerReaderBytes) + pack.MsgProcessor.MsgRoute(pack.ClientId, pack.Data, ws.recyclerReaderBytes) } } -func (ws *WSService) SetProcessor(process processor.IProcessor,handler event.IEventHandler){ +func (ws *WSService) SetProcessor(process processor.IProcessor, handler event.IEventHandler) { ws.process = process - ws.RegEventReceiverFunc(event.Sys_Event_WebSocket,handler, ws.WSEventHandler) + ws.RegEventReceiverFunc(event.Sys_Event_WebSocket, handler, ws.WSEventHandler) } func (ws *WSService) NewWSClient(conn *network.WSConn) network.Agent { @@ -125,55 +126,55 @@ func (slf *WSClient) GetId() string { } func (slf *WSClient) Run() { - slf.wsService.NotifyEvent(&event.Event{Type:event.Sys_Event_WebSocket,Data:&WSPack{ClientId:slf.id,Type:WPT_Connected,MsgProcessor:slf.wsService.process}}) - for{ - bytes,err := slf.wsConn.ReadMsg() + slf.wsService.NotifyEvent(&event.Event{Type: event.Sys_Event_WebSocket, Data: &WSPack{ClientId: slf.id, Type: WPT_Connected, MsgProcessor: slf.wsService.process}}) + for { + bytes, err := slf.wsConn.ReadMsg() if err != nil { - log.Debug("read client id %s is error:%+v",slf.id,err) + log.Debug("read client id %s is error:%+v", slf.id, err) break } - data,err:=slf.wsService.process.Unmarshal(slf.id,bytes) + data, err := slf.wsService.process.Unmarshal(slf.id, bytes) if err != nil { - slf.wsService.NotifyEvent(&event.Event{Type:event.Sys_Event_WebSocket,Data:&WSPack{ClientId:slf.id,Type:WPT_UnknownPack,Data:bytes,MsgProcessor:slf.wsService.process}}) + slf.wsService.NotifyEvent(&event.Event{Type: event.Sys_Event_WebSocket, Data: &WSPack{ClientId: slf.id, Type: WPT_UnknownPack, Data: bytes, MsgProcessor: slf.wsService.process}}) continue } - slf.wsService.NotifyEvent(&event.Event{Type:event.Sys_Event_WebSocket,Data:&WSPack{ClientId:slf.id,Type:WPT_Pack,Data:data,MsgProcessor:slf.wsService.process}}) + slf.wsService.NotifyEvent(&event.Event{Type: event.Sys_Event_WebSocket, Data: &WSPack{ClientId: slf.id, Type: WPT_Pack, Data: data, MsgProcessor: slf.wsService.process}}) } } -func (slf *WSClient) OnClose(){ - slf.wsService.NotifyEvent(&event.Event{Type:event.Sys_Event_WebSocket,Data:&WSPack{ClientId:slf.id,Type:WPT_DisConnected,MsgProcessor:slf.wsService.process}}) +func (slf *WSClient) OnClose() { + slf.wsService.NotifyEvent(&event.Event{Type: event.Sys_Event_WebSocket, Data: &WSPack{ClientId: slf.id, Type: WPT_DisConnected, MsgProcessor: slf.wsService.process}}) slf.wsService.mapClientLocker.Lock() defer slf.wsService.mapClientLocker.Unlock() - delete (slf.wsService.mapClient,slf.GetId()) + delete(slf.wsService.mapClient, slf.GetId()) } -func (ws *WSService) SendMsg(clientid string,msg interface{}) error{ +func (ws *WSService) SendMsg(clientId string, msg interface{}) error { ws.mapClientLocker.Lock() - client,ok := ws.mapClient[clientid] - if ok == false{ + client, ok := ws.mapClient[clientId] + if ok == false { ws.mapClientLocker.Unlock() - return fmt.Errorf("client %s is disconnect!",clientid) + return fmt.Errorf("client %s is disconnect", clientId) } ws.mapClientLocker.Unlock() - bytes,err := ws.process.Marshal(clientid,msg) + bytes, err := ws.process.Marshal(clientId, msg) if err != nil { return err } return client.wsConn.WriteMsg(bytes) } -func (ws *WSService) Close(clientid string) { +func (ws *WSService) Close(clientId string) { ws.mapClientLocker.Lock() defer ws.mapClientLocker.Unlock() - client,ok := ws.mapClient[clientid] - if ok == false{ + client, ok := ws.mapClient[clientId] + if ok == false { return } - if client.wsConn!=nil { + if client.wsConn != nil { client.wsConn.Close() } diff --git a/util/aesencrypt/aes_encrypt.go b/util/aesencrypt/aes_encrypt.go index d42437e..da1ff52 100644 --- a/util/aesencrypt/aes_encrypt.go +++ b/util/aesencrypt/aes_encrypt.go @@ -9,7 +9,7 @@ import ( func NewAesEncrypt(key string) (aes *AesEncrypt, err error) { keyLen := len(key) if keyLen < 16 { - err = fmt.Errorf("The length of res key shall not be less than 16") + err = fmt.Errorf("the length of res key shall not be less than 16") return } aes = &AesEncrypt{ @@ -22,12 +22,12 @@ type AesEncrypt struct { StrKey string } -func (this *AesEncrypt) getKey() []byte { - keyLen := len(this.StrKey) +func (ae *AesEncrypt) getKey() []byte { + keyLen := len(ae.StrKey) if keyLen < 16 { panic("The length of res key shall not be less than 16") } - arrKey := []byte(this.StrKey) + arrKey := []byte(ae.StrKey) if keyLen >= 32 { //取前32个字节 return arrKey[:32] @@ -40,37 +40,37 @@ func (this *AesEncrypt) getKey() []byte { return arrKey[:16] } -//加密字符串 -func (this *AesEncrypt) Encrypt(strMesg string) ([]byte, error) { - key := this.getKey() - var iv = []byte(key)[:aes.BlockSize] - encrypted := make([]byte, len(strMesg)) +// Encrypt 加密字符串 +func (ae *AesEncrypt) Encrypt(str string) ([]byte, error) { + key := ae.getKey() + var iv = key[:aes.BlockSize] + encrypted := make([]byte, len(str)) aesBlockEncrypter, err := aes.NewCipher(key) if err != nil { return nil, err } aesEncrypter := cipher.NewCFBEncrypter(aesBlockEncrypter, iv) - aesEncrypter.XORKeyStream(encrypted, []byte(strMesg)) + aesEncrypter.XORKeyStream(encrypted, []byte(str)) return encrypted, nil } -//解密字符串 -func (this *AesEncrypt) Decrypt(src []byte) (strDesc string, err error) { +// Decrypt 解密字符串 +func (ae *AesEncrypt) Decrypt(src []byte) (strDesc string, err error) { defer func() { //错误处理 if e := recover(); e != nil { err = e.(error) } }() - key := this.getKey() - var iv = []byte(key)[:aes.BlockSize] + key := ae.getKey() + var iv = key[:aes.BlockSize] decrypted := make([]byte, len(src)) var aesBlockDecrypter cipher.Block - aesBlockDecrypter, err = aes.NewCipher([]byte(key)) + aesBlockDecrypter, err = aes.NewCipher(key) if err != nil { return "", err } aesDecrypter := cipher.NewCFBDecrypter(aesBlockDecrypter, iv) aesDecrypter.XORKeyStream(decrypted, src) return string(decrypted), nil -} \ No newline at end of file +} diff --git a/util/algorithms/BitwiseOperation.go b/util/algorithms/BitwiseOperation.go index 4b20ab5..3cd166f 100644 --- a/util/algorithms/BitwiseOperation.go +++ b/util/algorithms/BitwiseOperation.go @@ -42,7 +42,7 @@ func setBitTagByIndex[Number BitNumber, UNumber UnsignedNumber](bitBuff []Number func GetBitwiseTag[Number BitNumber, UNumber UnsignedNumber](bitBuff []Number, bitPositionIndex UNumber) (bool, error) { sliceIndex, sliceBitIndex, ret := getBitTagIndex(bitBuff, bitPositionIndex) if ret == false { - return false, errors.New("Invalid parameter") + return false, errors.New("invalid parameter") } return (bitBuff[sliceIndex] & (1 << sliceBitIndex)) > 0, nil diff --git a/util/bytespool/bytespool.go b/util/bytespool/bytespool.go index 42a8020..91f5c20 100644 --- a/util/bytespool/bytespool.go +++ b/util/bytespool/bytespool.go @@ -4,7 +4,7 @@ import ( "sync" ) -type IBytesMempool interface { +type IBytesMemPool interface { MakeBytes(size int) []byte ReleaseBytes(byteBuff []byte) bool } diff --git a/util/coroutine/coroutine.go b/util/coroutine/coroutine.go index a38a18f..306c76b 100644 --- a/util/coroutine/coroutine.go +++ b/util/coroutine/coroutine.go @@ -7,18 +7,18 @@ import ( "runtime/debug" ) -func F(callback interface{},recoverNum int, args ...interface{}) { +func F(callback interface{}, recoverNum int, args ...interface{}) { defer func() { if r := recover(); r != nil { var coreInfo string coreInfo = string(debug.Stack()) coreInfo += "\n" + fmt.Sprintf("Core information is %v\n", r) log.SError(coreInfo) - if recoverNum > 0{ + if recoverNum > 0 { recoverNum -= 1 } if recoverNum == -1 || recoverNum > 0 { - go F(callback,recoverNum, args...) + go F(callback, recoverNum, args...) } } }() @@ -36,10 +36,10 @@ func F(callback interface{},recoverNum int, args ...interface{}) { } func Go(callback interface{}, args ...interface{}) { - go F(callback,0, args...) + go F(callback, 0, args...) } -//-1表示一直恢复 -func GoRecover(callback interface{},recoverNum int, args ...interface{}) { - go F(callback,recoverNum, args...) -} \ No newline at end of file +// GoRecover -1表示一直恢复 +func GoRecover(callback interface{}, recoverNum int, args ...interface{}) { + go F(callback, recoverNum, args...) +} diff --git a/util/srand/slice.go b/util/srand/slice.go index 89ddd84..cb8975a 100644 --- a/util/srand/slice.go +++ b/util/srand/slice.go @@ -39,12 +39,13 @@ func RandN[E ~[]T, T any](arr E, num int) []T { } Shuffle(index) if len(index) > num { - index = index + index = index[:num] } ret := make([]T, 0, len(index)) for i := range index { ret = append(ret, arr[index[i]]) } + return ret } @@ -104,4 +105,4 @@ func GetPointerFunc[E ~[]T, T any](arr E, f func(T) bool) *T { } return &arr[index] -} \ No newline at end of file +} diff --git a/util/sync/MemPool.go b/util/sync/MemPool.go index bee47b0..b7ae737 100644 --- a/util/sync/MemPool.go +++ b/util/sync/MemPool.go @@ -5,34 +5,32 @@ import ( ) type Pool struct { - C chan interface{} //最大缓存的数量 + C chan interface{} //最大缓存的数量 syncPool sysSync.Pool } type IPoolData interface { Reset() - IsRef()bool + IsRef() bool Ref() UnRef() } -type PoolEx struct{ - C chan IPoolData //最大缓存的数量 +type PoolEx struct { + C chan IPoolData //最大缓存的数量 syncPool sysSync.Pool } -func (pool *Pool) Get() interface{}{ +func (pool *Pool) Get() interface{} { select { case d := <-pool.C: return d default: return pool.syncPool.Get() } - - return nil } -func (pool *Pool) Put(data interface{}){ +func (pool *Pool) Put(data interface{}) { select { case pool.C <- data: default: @@ -41,14 +39,14 @@ func (pool *Pool) Put(data interface{}){ } -func NewPool(C chan interface{},New func()interface{}) *Pool{ +func NewPool(C chan interface{}, New func() interface{}) *Pool { var p Pool p.C = C p.syncPool.New = New return &p } -func NewPoolEx(C chan IPoolData,New func()IPoolData) *PoolEx{ +func NewPoolEx(C chan IPoolData, New func() IPoolData) *PoolEx { var pool PoolEx pool.C = C pool.syncPool.New = func() interface{} { @@ -57,7 +55,7 @@ func NewPoolEx(C chan IPoolData,New func()IPoolData) *PoolEx{ return &pool } -func (pool *PoolEx) Get() IPoolData{ +func (pool *PoolEx) Get() IPoolData { select { case d := <-pool.C: if d.IsRef() { @@ -79,7 +77,7 @@ func (pool *PoolEx) Get() IPoolData{ return nil } -func (pool *PoolEx) Put(data IPoolData){ +func (pool *PoolEx) Put(data IPoolData) { if data.IsRef() == false { panic("Repeatedly freeing memory") } @@ -94,5 +92,3 @@ func (pool *PoolEx) Put(data IPoolData){ pool.syncPool.Put(data) } } - - diff --git a/util/timer/timer.go b/util/timer/timer.go index c111316..b0839ef 100644 --- a/util/timer/timer.go +++ b/util/timer/timer.go @@ -6,11 +6,10 @@ import ( "github.com/duanhf2012/origin/v2/util/sync" "reflect" "runtime" - "time" "sync/atomic" + "time" ) -// ITimer type ITimer interface { GetId() uint64 Cancel() @@ -27,10 +26,9 @@ type ITimer interface { type OnCloseTimer func(timer ITimer) type OnAddTimer func(timer ITimer) -// Timer type Timer struct { Id uint64 - cancelled int32 //是否关闭 + cancelled int32 //是否关闭 C chan ITimer //定时器管道 interval time.Duration // 时间间隔(用于循环定时器) fireTime time.Time // 触发时间 @@ -46,12 +44,10 @@ type Timer struct { ref bool } -// Ticker type Ticker struct { Timer } -// Cron type Cron struct { Timer } @@ -101,7 +97,6 @@ func releaseCron(cron *Cron) { cronPool.Put(cron) } -// one dispatcher per goroutine (goroutine not safe) type Dispatcher struct { ChanTimer chan ITimer } @@ -132,7 +127,7 @@ func (t *Timer) Do() { buf := make([]byte, 4096) l := runtime.Stack(buf, false) errString := fmt.Sprint(r) - log.Dump(string(buf[:l]),log.String("error",errString)) + log.Dump(string(buf[:l]), log.String("error", errString)) } }() @@ -172,10 +167,10 @@ func (t *Timer) GetInterval() time.Duration { } func (t *Timer) Cancel() { - atomic.StoreInt32(&t.cancelled,1) + atomic.StoreInt32(&t.cancelled, 1) } -// 判断定时器是否已经取消 +// IsActive 判断定时器是否已经取消 func (t *Timer) IsActive() bool { return atomic.LoadInt32(&t.cancelled) == 0 } @@ -218,7 +213,7 @@ func (c *Cron) Do() { buf := make([]byte, 4096) l := runtime.Stack(buf, false) errString := fmt.Sprint(r) - log.Dump(string(buf[:l]),log.String("error",errString)) + log.Dump(string(buf[:l]), log.String("error", errString)) } }() @@ -274,7 +269,7 @@ func (c *Ticker) Do() { buf := make([]byte, 4096) l := runtime.Stack(buf, false) errString := fmt.Sprint(r) - log.Dump(string(buf[:l]),log.String("error",errString)) + log.Dump(string(buf[:l]), log.String("error", errString)) } }()