支持配置启动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

@ -41,6 +41,8 @@ const (
DefaultLogSplitInterval = logger.TimeIntervalTypeHour DefaultLogSplitInterval = logger.TimeIntervalTypeHour
// DefaultStoreConnection 默认存储连接 // DefaultStoreConnection 默认存储连接
DefaultStoreConnection = true DefaultStoreConnection = true
// DefaultEnablePprof 默认关闭pprof
DefaultEnablePprof = false
) )
// WSServerConfig WS-Server的配置 // WSServerConfig WS-Server的配置
@ -58,6 +60,8 @@ type WSServerConfig struct {
LogSplitInterval logger.TimeIntervalType // 日至切割的时间间隔 LogSplitInterval logger.TimeIntervalType // 日至切割的时间间隔
StoreConnection bool // 存储连接 StoreConnection bool // 存储连接
ConnectionManager storage.IConnection // 连接管理实例 ConnectionManager storage.IConnection // 连接管理实例
EnablePprof bool // 开启pprof, 默认关闭
PprofPort int // pprof监听的端口
} }
// SetWSServerConfig 设置WS-Server的配置 // SetWSServerConfig 设置WS-Server的配置
@ -94,6 +98,18 @@ func SetWSServerLogEnable(logPath string, logFile string, logLevel zapcore.Level
} }
} }
// EnablePprof 开启PProf,由于多模块共享一个进程,任意一个模块开启,就认为是开启
//
// Author : go_developer@163.com<张德满>
//
// Date : 11:24 下午 2021/4/18
func EnablePprof(pprofPort int) SetWSServerConfig {
return func(wsc *WSServerConfig) {
wsc.EnablePprof = true
wsc.PprofPort = pprofPort
}
}
// DisableStoreConnection 禁用连接存储 // DisableStoreConnection 禁用连接存储
// //
// Author : go_developer@163.com<张德满> // Author : go_developer@163.com<张德满>

View File

@ -10,6 +10,8 @@ package websocket
import ( import (
"fmt" "fmt"
"github.com/gin-contrib/pprof"
"github.com/go-developer/gopkg/logger" "github.com/go-developer/gopkg/logger"
"go.uber.org/zap" "go.uber.org/zap"
@ -158,6 +160,15 @@ func initServer(wsInstance abstract.IWebsocket) {
}) })
} }
currentWSServer := getWsServer(wsInstance.GetServerPort(), wsInstance.GetModuleFlag()) 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. 建立连接的函数注册回调函数 // 1. 建立连接的函数注册回调函数
// // // //
@ -215,19 +226,6 @@ func initServer(wsInstance abstract.IWebsocket) {
) )
commandTable[wsInstance.GetModuleFlag()][cmd.GetCommand()] = cmd 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 ... // dispatchCommand 调度command ...
@ -315,7 +313,16 @@ func dispatchCommand(ctx *context.WSContext, data []byte) error {
return nil return nil
} }
// 启动所有端口的监听
func run() { 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 <-sigChan
// TODO : 增加后置hook // TODO : 增加后置hook
} }

View File

@ -65,6 +65,7 @@ func (e Example) GetServerPort() int {
func (e Example) GetWSServerConfig() []config.SetWSServerConfig { func (e Example) GetWSServerConfig() []config.SetWSServerConfig {
return []config.SetWSServerConfig{ return []config.SetWSServerConfig{
config.SetWSServerLogEnable("./logs", e.GetModuleFlag()+".log", zapcore.DebugLevel, logger.TimeIntervalTypeHour), config.SetWSServerLogEnable("./logs", e.GetModuleFlag()+".log", zapcore.DebugLevel, logger.TimeIntervalTypeHour),
config.EnablePprof(10100),
} }
} }

1
go.mod
View File

@ -3,6 +3,7 @@ module github.com/go-developer/websocket
go 1.16 go 1.16
require ( require (
github.com/gin-contrib/pprof v1.3.0 // indirect
github.com/gin-gonic/gin v1.6.3 github.com/gin-gonic/gin v1.6.3
github.com/go-developer/gopkg v0.0.0-20210417123142-b08b27daae93 github.com/go-developer/gopkg v0.0.0-20210417123142-b08b27daae93
github.com/go-playground/validator/v10 v10.4.1 // indirect github.com/go-playground/validator/v10 v10.4.1 // indirect

3
go.sum
View File

@ -7,8 +7,11 @@ github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc= github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ= github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ=
github.com/gin-contrib/pprof v1.3.0 h1:G9eK6HnbkSqDZBYbzG4wrjCsA4e+cvYAHUZw6W+W9K0=
github.com/gin-contrib/pprof v1.3.0/go.mod h1:waMjT1H9b179t3CxuG1cV3DHpga6ybizwfBaM5OXaB0=
github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE= github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=
github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI= github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI=
github.com/gin-gonic/gin v1.6.2/go.mod h1:75u5sXoLsGZoRN5Sgbi1eraJ4GU3++wFwWzhwvtwp4M=
github.com/gin-gonic/gin v1.6.3 h1:ahKqKTFpO5KTPHxWZjEdPScmYaGtLo8Y4DMHoEsnp14= github.com/gin-gonic/gin v1.6.3 h1:ahKqKTFpO5KTPHxWZjEdPScmYaGtLo8Y4DMHoEsnp14=
github.com/gin-gonic/gin v1.6.3/go.mod h1:75u5sXoLsGZoRN5Sgbi1eraJ4GU3++wFwWzhwvtwp4M= github.com/gin-gonic/gin v1.6.3/go.mod h1:75u5sXoLsGZoRN5Sgbi1eraJ4GU3++wFwWzhwvtwp4M=
github.com/go-developer/gopkg v0.0.0-20210409075258-6a35eb1a9d4c h1:WotMdy0sk6f9+kMxzqBbdzlZtok7ieiTHnn1eVeqTTE= github.com/go-developer/gopkg v0.0.0-20210409075258-6a35eb1a9d4c h1:WotMdy0sk6f9+kMxzqBbdzlZtok7ieiTHnn1eVeqTTE=