支持配置启动PPROF

This commit is contained in:
2021-04-19 00:03:25 +08:00
parent 844da3f325
commit d2a6f851f8
5 changed files with 41 additions and 13 deletions

View File

@ -10,6 +10,8 @@ package websocket
import (
"fmt"
"github.com/gin-contrib/pprof"
"github.com/go-developer/gopkg/logger"
"go.uber.org/zap"
@ -158,6 +160,15 @@ func initServer(wsInstance abstract.IWebsocket) {
})
}
currentWSServer := getWsServer(wsInstance.GetServerPort(), wsInstance.GetModuleFlag())
// 注册pprof
if currentWSServer.conf.EnablePprof {
pprofGinRouter, exist := ginRouterTable[currentWSServer.conf.PprofPort]
if !exist {
pprofGinRouter = gin.Default()
ginRouterTable[currentWSServer.conf.PprofPort] = pprofGinRouter
}
pprof.Register(pprofGinRouter)
}
// 注册回调函数
// 1. 建立连接的函数注册回调函数
// //
@ -215,19 +226,6 @@ func initServer(wsInstance abstract.IWebsocket) {
)
commandTable[wsInstance.GetModuleFlag()][cmd.GetCommand()] = cmd
}
go func() {
if err := ginRouterTable[wsInstance.GetServerPort()].Run(fmt.Sprintf(":%d", wsInstance.GetServerPort())); nil != err {
log(
getLoggerInstance(wsInstance.GetModuleFlag(), nil),
logFuncPanic,
"模块启动端口监听失败",
getLoadDataList(nil,
zap.String("module_flag", wsInstance.GetModuleFlag()),
zap.Error(err),
),
)
}
}()
}
// dispatchCommand 调度command ...
@ -315,7 +313,16 @@ func dispatchCommand(ctx *context.WSContext, data []byte) error {
return nil
}
// 启动所有端口的监听
func run() {
for port, ginInstance := range ginRouterTable {
go func(ginInstance *gin.Engine, port int) {
if err := ginInstance.Run(fmt.Sprintf(":%d", port)); nil != err {
panic(fmt.Sprintf("%d 启动端口监听失败, 失败原因 : %s", err.Error()))
}
}(ginInstance, port)
}
<-sigChan
// TODO : 增加后置hook
}