mirror of
https://github.com/FlourishingWorld/hk4e.git
synced 2026-02-04 15:32:26 +08:00
perf与性能优化
This commit is contained in:
@@ -3,6 +3,7 @@ package controller
|
||||
import (
|
||||
"net/http"
|
||||
"strconv"
|
||||
"sync"
|
||||
|
||||
"hk4e/common/config"
|
||||
"hk4e/common/rpc"
|
||||
@@ -12,12 +13,13 @@ import (
|
||||
)
|
||||
|
||||
type Controller struct {
|
||||
gm *rpc.GMClient
|
||||
gmClientMap map[uint32]*rpc.GMClient
|
||||
gmClientMapLock sync.RWMutex
|
||||
}
|
||||
|
||||
func NewController(gm *rpc.GMClient) (r *Controller) {
|
||||
func NewController() (r *Controller) {
|
||||
r = new(Controller)
|
||||
r.gm = gm
|
||||
r.gmClientMap = make(map[uint32]*rpc.GMClient)
|
||||
go r.registerRouter()
|
||||
return r
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package controller
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"hk4e/common/rpc"
|
||||
"hk4e/gs/api"
|
||||
"hk4e/pkg/logger"
|
||||
|
||||
@@ -10,8 +11,9 @@ import (
|
||||
)
|
||||
|
||||
type GmCmdReq struct {
|
||||
FuncName string `json:"func_name"`
|
||||
Param []string `json:"param"`
|
||||
FuncName string `json:"func_name"`
|
||||
ParamList []string `json:"param_list"`
|
||||
GsId uint32 `json:"gs_id"`
|
||||
}
|
||||
|
||||
func (c *Controller) gmCmd(context *gin.Context) {
|
||||
@@ -21,14 +23,28 @@ func (c *Controller) gmCmd(context *gin.Context) {
|
||||
logger.Error("parse json error: %v", err)
|
||||
return
|
||||
}
|
||||
rep, err := c.gm.Cmd(context.Request.Context(), &api.CmdRequest{
|
||||
FuncName: gmCmdReq.FuncName,
|
||||
Param: gmCmdReq.Param,
|
||||
logger.Info("GmCmdReq: %v", gmCmdReq)
|
||||
c.gmClientMapLock.RLock()
|
||||
gmClient, exist := c.gmClientMap[gmCmdReq.GsId]
|
||||
c.gmClientMapLock.RUnlock()
|
||||
if !exist {
|
||||
var err error = nil
|
||||
gmClient, err = rpc.NewGMClient(gmCmdReq.GsId)
|
||||
if err != nil {
|
||||
logger.Error("new gm client error: %v", err)
|
||||
return
|
||||
}
|
||||
c.gmClientMapLock.Lock()
|
||||
c.gmClientMap[gmCmdReq.GsId] = gmClient
|
||||
c.gmClientMapLock.Unlock()
|
||||
}
|
||||
rep, err := gmClient.Cmd(context.Request.Context(), &api.CmdRequest{
|
||||
FuncName: gmCmdReq.FuncName,
|
||||
ParamList: gmCmdReq.ParamList,
|
||||
})
|
||||
if err != nil {
|
||||
context.JSON(http.StatusInternalServerError, err)
|
||||
return
|
||||
}
|
||||
context.JSON(http.StatusOK, rep)
|
||||
logger.Info("%v", gmCmdReq)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user