优化rpc-减少gc

This commit is contained in:
boyce
2020-11-04 15:06:39 +08:00
parent 2fc1217b18
commit 4d088d66da
4 changed files with 51 additions and 37 deletions

View File

@@ -182,24 +182,25 @@ func (cls *Cluster) GetRpcClient(nodeId int) *rpc.Client {
return c.client
}
func GetRpcClient(nodeId int,serviceMethod string,clientList *[]*rpc.Client) error {
func GetRpcClient(nodeId int,serviceMethod string,clientList []*rpc.Client) (error,int) {
if nodeId>0 {
pClient := GetCluster().GetRpcClient(nodeId)
if pClient==nil {
return fmt.Errorf("cannot find nodeid %d!",nodeId)
return fmt.Errorf("cannot find nodeid %d!",nodeId),0
}
*clientList = append(*clientList,pClient)
return nil
clientList[0] = pClient
return nil,1
}
serviceAndMethod := strings.Split(serviceMethod,".")
if len(serviceAndMethod)!=2 {
return fmt.Errorf("servicemethod param %s is error!",serviceMethod)
findIndex := strings.Index(serviceMethod,".")
if findIndex==-1 {
return fmt.Errorf("servicemethod param %s is error!",serviceMethod),0
}
serviceName := serviceMethod[:findIndex]
//1.找到对应的rpcNodeid
GetCluster().GetNodeIdByService(serviceAndMethod[0],clientList)
return nil
return GetCluster().GetNodeIdByService(serviceName,clientList)
}
func GetRpcServer() *rpc.Server{

View File

@@ -194,10 +194,11 @@ func (cls *Cluster) IsConfigService(serviceName string) bool {
return false
}
func (cls *Cluster) GetNodeIdByService(serviceName string,rpcClientList *[]*rpc.Client) {
func (cls *Cluster) GetNodeIdByService(serviceName string,rpcClientList []*rpc.Client) (error,int) {
cls.locker.RLock()
defer cls.locker.RUnlock()
nodeIdList,ok := cls.mapServiceNode[serviceName]
count := 0
if ok == true {
for _,nodeId := range nodeIdList {
pClient := GetCluster().GetRpcClient(nodeId)
@@ -205,9 +206,15 @@ func (cls *Cluster) GetNodeIdByService(serviceName string,rpcClientList *[]*rpc.
log.Error("Cannot connect node id %d",nodeId)
continue
}
*rpcClientList = append(*rpcClientList,pClient)
rpcClientList[count] = pClient
count++
if count>=cap(rpcClientList) {
break
}
}
}
return nil,count
}
func (cls *Cluster) getServiceCfg(serviceName string) interface{}{