1 Star 0 Fork 0

Souki/go-framework

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
pprof.go 1.56 KB
一键复制 编辑 原始数据 按行查看 历史
sage 提交于 2022-09-26 11:13 +08:00 . modify pprof
package httpserver
import (
"github.com/julienschmidt/httprouter"
"net/http"
"net/http/pprof"
)
var pprofFuncs = map[string]http.HandlerFunc{
"/debug/pprof/": pprof.Index,
"/debug/pprof/goroutine": pprof.Handler("goroutine").ServeHTTP,
"/debug/pprof/heap": pprof.Handler("heap").ServeHTTP,
"/debug/pprof/allocs": pprof.Handler("allocs").ServeHTTP,
"/debug/pprof/cmdline": pprof.Cmdline,
"/debug/pprof/profile": pprof.Profile,
"/debug/pprof/symbol": pprof.Symbol,
"/debug/pprof/trace": pprof.Trace,
}
//PProfRegRouter pprof-注册路由
//查看内存占用分析: go tool pprof -alloc_space "127.0.0.1:8888/debug/pprof/heap?seconds=30"
func PProfRegRouter(ro interface{}) {
//默认不传则直接注册http
if ro == nil {
for path, f := range pprofFuncs {
http.HandleFunc(path, f)
}
return
}
//支持本地 HttpServer 进行pprof处理
if r, ok := ro.(HttpServer); ok {
for path, f := range pprofFuncs {
r.RouteHandler(http.MethodGet, path, f, nil)
}
return
}
//支持 http.Server 的处理
if s, ok := ro.(*http.Server); ok {
s.Handler = PProfHandler(s.Handler.ServeHTTP)
return
}
//支持 httprouter.Router 的处理
if r, ok := ro.(*httprouter.Router); ok {
for path, f := range pprofFuncs {
r.HandlerFunc(http.MethodGet, path, f)
}
return
}
}
func PProfHandler(next http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
//针对pprof路径进行处理
if f, ok := pprofFuncs[r.URL.Path]; ok {
f(w, r)
return
}
if next != nil {
next(w, r)
}
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/scottq/go-framework.git
git@gitee.com:scottq/go-framework.git
scottq
go-framework
go-framework
v1.1.46

搜索帮助