优化GM服务器
This commit is contained in:
1 parent
00d32292ad
commit
bea91fbf73
12 files changed
+152
-37
No files matched your search
+6
-1
@@ -7,8 +7,10 @@ import (
|
||||
"syscall"
|
||||
|
||||
"hk4e/common/config"
|
||||
"hk4e/common/mq"
|
||||
"hk4e/common/rpc"
|
||||
"hk4e/gm/controller"
|
||||
"hk4e/node/api"
|
||||
"hk4e/pkg/logger"
|
||||
)
|
||||
|
||||
@@ -27,7 +29,10 @@ func Run(ctx context.Context, configFile string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
_ = controller.NewController(discoveryClient)
|
||||
messageQueue := mq.NewMessageQueue(api.GM, "gm", nil)
|
||||
defer messageQueue.Close()
|
||||
|
||||
_ = controller.NewController(discoveryClient, messageQueue)
|
||||
|
||||
c := make(chan os.Signal, 1)
|
||||
signal.Notify(c, syscall.SIGHUP, syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT)
|
||||
|
||||
@@ -1,11 +1,14 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"hk4e/common/config"
|
||||
"hk4e/common/mq"
|
||||
"hk4e/common/rpc"
|
||||
"hk4e/pkg/logger"
|
||||
|
||||
@@ -13,22 +16,62 @@ import (
|
||||
)
|
||||
|
||||
type Controller struct {
|
||||
gmClientMap map[uint32]*rpc.GMClient
|
||||
gmClientMapLock sync.RWMutex
|
||||
discoveryClient *rpc.DiscoveryClient
|
||||
gmClientMap map[uint32]*rpc.GMClient
|
||||
gmClientMapLock sync.RWMutex
|
||||
discoveryClient *rpc.DiscoveryClient
|
||||
messageQueue *mq.MessageQueue
|
||||
globalGsOnlineMap map[uint32]string // 全服玩家在线表
|
||||
globalGsOnlineMapLock sync.RWMutex
|
||||
}
|
||||
|
||||
func NewController(discoveryClient *rpc.DiscoveryClient) (r *Controller) {
|
||||
func NewController(discoveryClient *rpc.DiscoveryClient, messageQueue *mq.MessageQueue) (r *Controller) {
|
||||
r = new(Controller)
|
||||
r.gmClientMap = make(map[uint32]*rpc.GMClient)
|
||||
r.discoveryClient = discoveryClient
|
||||
r.messageQueue = messageQueue
|
||||
go func() {
|
||||
for {
|
||||
_, ok := <-r.messageQueue.GetNetMsg()
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
}
|
||||
}()
|
||||
r.globalGsOnlineMap = make(map[uint32]string)
|
||||
r.syncGlobalGsOnlineMap()
|
||||
go r.autoSyncGlobalGsOnlineMap()
|
||||
go r.registerRouter()
|
||||
return r
|
||||
}
|
||||
|
||||
func (c *Controller) autoSyncGlobalGsOnlineMap() {
|
||||
ticker := time.NewTicker(time.Second * 60)
|
||||
for {
|
||||
<-ticker.C
|
||||
c.syncGlobalGsOnlineMap()
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Controller) syncGlobalGsOnlineMap() {
|
||||
rsp, err := c.discoveryClient.GetGlobalGsOnlineMap(context.TODO(), nil)
|
||||
if err != nil {
|
||||
logger.Error("get global gs online map error: %v", err)
|
||||
return
|
||||
}
|
||||
copyMap := make(map[uint32]string)
|
||||
for k, v := range rsp.OnlineMap {
|
||||
copyMap[k] = v
|
||||
}
|
||||
copyMapLen := len(copyMap)
|
||||
c.globalGsOnlineMapLock.Lock()
|
||||
c.globalGsOnlineMap = copyMap
|
||||
c.globalGsOnlineMapLock.Unlock()
|
||||
logger.Info("sync global gs online map finish, len: %v", copyMapLen)
|
||||
}
|
||||
|
||||
func (c *Controller) authorize() gin.HandlerFunc {
|
||||
return func(context *gin.Context) {
|
||||
if true {
|
||||
if context.GetHeader("Auth") == "flswld" {
|
||||
// 验证通过
|
||||
context.Next()
|
||||
return
|
||||
@@ -55,6 +98,7 @@ func (c *Controller) registerRouter() {
|
||||
gin.SetMode(gin.ReleaseMode)
|
||||
}
|
||||
engine := gin.Default()
|
||||
engine.GET("/server/online/stats", c.serverOnlineStats)
|
||||
engine.Use(c.authorize())
|
||||
engine.POST("/gm/cmd", c.gmCmd)
|
||||
engine.GET("/server/stop/state", c.serverStopState)
|
||||
|
||||
@@ -3,6 +3,7 @@ package controller
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"hk4e/common/mq"
|
||||
"hk4e/common/rpc"
|
||||
"hk4e/gs/api"
|
||||
"hk4e/pkg/logger"
|
||||
@@ -14,6 +15,7 @@ type GmCmdReq struct {
|
||||
FuncName string `json:"func_name"`
|
||||
ParamList []string `json:"param_list"`
|
||||
GsId uint32 `json:"gs_id"`
|
||||
GsAppid string `json:"gs_appid"`
|
||||
}
|
||||
|
||||
func (c *Controller) gmCmd(ctx *gin.Context) {
|
||||
@@ -25,28 +27,53 @@ func (c *Controller) gmCmd(ctx *gin.Context) {
|
||||
return
|
||||
}
|
||||
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 gmCmdReq.GsId != 0 {
|
||||
// 指定gsid
|
||||
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)
|
||||
ctx.JSON(http.StatusOK, &CommonRsp{Code: -1, Msg: "", Data: err})
|
||||
return
|
||||
}
|
||||
c.gmClientMapLock.Lock()
|
||||
c.gmClientMap[gmCmdReq.GsId] = gmClient
|
||||
c.gmClientMapLock.Unlock()
|
||||
}
|
||||
rsp, err := gmClient.Cmd(ctx.Request.Context(), &api.CmdRequest{
|
||||
FuncName: gmCmdReq.FuncName,
|
||||
ParamList: gmCmdReq.ParamList,
|
||||
})
|
||||
if err != nil {
|
||||
logger.Error("new gm client error: %v", err)
|
||||
ctx.JSON(http.StatusOK, &CommonRsp{Code: -1, Msg: "", Data: err})
|
||||
return
|
||||
}
|
||||
c.gmClientMapLock.Lock()
|
||||
c.gmClientMap[gmCmdReq.GsId] = gmClient
|
||||
c.gmClientMapLock.Unlock()
|
||||
ctx.JSON(http.StatusOK, rsp)
|
||||
} else if gmCmdReq.GsAppid != "" {
|
||||
// 指定gsappid
|
||||
c.messageQueue.SendToGs(gmCmdReq.GsAppid, &mq.NetMsg{
|
||||
MsgType: mq.MsgTypeServer,
|
||||
EventId: mq.ServerGmCmdNotify,
|
||||
ServerMsg: &mq.ServerMsg{
|
||||
GmCmdFuncName: gmCmdReq.FuncName,
|
||||
GmCmdParamList: gmCmdReq.ParamList,
|
||||
},
|
||||
})
|
||||
ctx.JSON(http.StatusOK, nil)
|
||||
} else {
|
||||
// 全服
|
||||
c.messageQueue.SendToAll(&mq.NetMsg{
|
||||
MsgType: mq.MsgTypeServer,
|
||||
EventId: mq.ServerGmCmdNotify,
|
||||
ServerMsg: &mq.ServerMsg{
|
||||
GmCmdFuncName: gmCmdReq.FuncName,
|
||||
GmCmdParamList: gmCmdReq.ParamList,
|
||||
},
|
||||
})
|
||||
ctx.JSON(http.StatusOK, nil)
|
||||
}
|
||||
rep, err := gmClient.Cmd(ctx.Request.Context(), &api.CmdRequest{
|
||||
FuncName: gmCmdReq.FuncName,
|
||||
ParamList: gmCmdReq.ParamList,
|
||||
})
|
||||
if err != nil {
|
||||
ctx.JSON(http.StatusOK, &CommonRsp{Code: -1, Msg: "", Data: err})
|
||||
return
|
||||
}
|
||||
ctx.JSON(http.StatusOK, rep)
|
||||
}
|
||||
@@ -122,3 +122,15 @@ func (c *Controller) serverDispatchCancel(ctx *gin.Context) {
|
||||
}
|
||||
ctx.JSON(http.StatusOK, &CommonRsp{Code: 0, Msg: "", Data: nil})
|
||||
}
|
||||
|
||||
type ServerOnlineStats struct {
|
||||
TotalOnlinePlayerNum uint32 `json:"total_online_player_num"`
|
||||
}
|
||||
|
||||
func (c *Controller) serverOnlineStats(ctx *gin.Context) {
|
||||
num := 0
|
||||
c.globalGsOnlineMapLock.RLock()
|
||||
num = len(c.globalGsOnlineMap)
|
||||
c.globalGsOnlineMapLock.RUnlock()
|
||||
ctx.JSON(http.StatusOK, &CommonRsp{Code: 0, Msg: "", Data: &ServerOnlineStats{TotalOnlinePlayerNum: uint32(num)}})
|
||||
}
|
||||
Reference in new issue
Block a user