This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
||||
"log"
|
||||
"os"
|
||||
"runtime"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@@ -25,15 +26,37 @@ import (
|
||||
)
|
||||
|
||||
// PprofWeb 启动pprof性能分析web服务
|
||||
// PprofWeb 启动pprof web服务,仅重试2个端口保证监听成功
|
||||
func PprofWeb() {
|
||||
runtime.SetMutexProfileFraction(1) // (非必需)开启对锁调用的跟踪
|
||||
runtime.SetBlockProfileRate(1) // (非必需)开启对阻塞操作的跟踪
|
||||
err := http.ListenAndServe(":9909", nil)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
// 开启pprof跟踪
|
||||
runtime.SetMutexProfileFraction(1)
|
||||
runtime.SetBlockProfileRate(1)
|
||||
|
||||
// 定义2个重试端口:主端口9909 + 备用端口9910
|
||||
ports := []int{9909, 9910}
|
||||
|
||||
// 遍历端口尝试监听
|
||||
for _, port := range ports {
|
||||
addr := ":" + strconv.Itoa(port)
|
||||
fmt.Printf("[INFO] 尝试启动pprof服务,监听端口: %d\n", port)
|
||||
|
||||
// 尝试监听并处理错误
|
||||
err := http.ListenAndServe(addr, nil)
|
||||
// 只有端口被占用等致命错误才重试下一个端口
|
||||
if err != nil {
|
||||
fmt.Printf("[WARN] 端口%d监听失败: %v\n", port, err)
|
||||
continue
|
||||
}
|
||||
// 监听成功则直接返回
|
||||
return
|
||||
}
|
||||
|
||||
// 所有端口都失败时的兜底
|
||||
errMsg := fmt.Sprintf("[FATAL] 端口9909/9910均监听失败,pprof服务启动失败")
|
||||
fmt.Println(errMsg)
|
||||
// 可选:根据业务需求决定是否panic
|
||||
// panic(errMsg)
|
||||
}
|
||||
func cleanup() {
|
||||
fmt.Println("执行优雅清理资源...")
|
||||
|
||||
@@ -75,10 +98,11 @@ func main() {
|
||||
// }
|
||||
// 解析命令行参数
|
||||
cool.Config.GameOnlineID = gcmd.GetOpt("id", "1").Uint16()
|
||||
go Start() //注入service
|
||||
if cool.Config.GameOnlineID == 2 { //只分析1服务器的
|
||||
go PprofWeb()
|
||||
}
|
||||
go Start() //注入service
|
||||
// if cool.Config.GameOnlineID == 2 { //只分析1服务器的
|
||||
// go PprofWeb()
|
||||
// }
|
||||
go PprofWeb()
|
||||
//go PprofWeb()
|
||||
go monitorMemAndQuit()
|
||||
fmt.Println("Process start, pid:", os.Getpid())
|
||||
|
||||
Reference in New Issue
Block a user