perf与性能优化

This commit is contained in:
flswld
2023-04-15 20:00:37 +08:00
parent 149b773f4b
commit 094ad5add0
26 changed files with 403 additions and 308 deletions

View File

@@ -7,7 +7,6 @@ import (
"syscall"
"hk4e/common/config"
"hk4e/common/rpc"
"hk4e/gm/controller"
"hk4e/pkg/logger"
)
@@ -21,13 +20,7 @@ func Run(ctx context.Context, configFile string) error {
logger.CloseLogger()
}()
// natsrpc client
client, err := rpc.NewClient()
if err != nil {
return err
}
_ = controller.NewController(client.GM)
_ = controller.NewController()
c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGHUP, syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT)

View File

@@ -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
}

View File

@@ -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)
}