英文:
golang - net/http/pprof doesn't work
问题
我有一个客户端的HTTP服务:
s := &http.Server{
Addr: config.Port,
Handler: Controller.Log(http.DefaultServeMux),
ReadTimeout: 3 * time.Second,
WriteTimeout: 3 * time.Second,
}
http.HandleFunc("/exapmle/router/", exampleFunc)
err := s.ListenAndServe()
if err != nil {
log.Critical(err)
os.Exit(1)
}
它不起作用:
go tool pprof http://localhost:8201/debug/pprof/profile
返回错误:
Failed to fetch http://localhost:8201/debug/pprof/profile?seconds=30
谢谢。
编辑:
我认为问题是我重写了默认的HTTP服务器,net/http/pprof包注入了HTTP处理程序:
func init() {
http.Handle("/debug/pprof/", http.HandlerFunc(Index))
http.Handle("/debug/pprof/cmdline", http.HandlerFunc(Cmdline))
http.Handle("/debug/pprof/profile", http.HandlerFunc(Profile))
http.Handle("/debug/pprof/symbol", http.HandlerFunc(Symbol))
}
这个处理程序在我的代码中不起作用。
英文:
I have customer http service:
s := &http.Server{
Addr: config.Port,
Handler: Controller.Log(http.DefaultServeMux),
ReadTimeout: 3 * time.Second,
WriteTimeout: 3 * time.Second,
}
http.HandleFunc("/exapmle/router/", exampleFunc)
err := s.ListenAndServe()
if err != nil {
log.Critical(err)
os.Exit(1)
}
It's does't work:
go tool pprof http://localhost:8201/debug/pprof/profile
return error:
Failed to fetch http://localhost:8201/debug/pprof/profile?seconds=30
Thanks.
edit:
I think the problem is i rewrite default http server, net/http/pprof package inject http handler:
func init() {
http.Handle("/debug/pprof/", http.HandlerFunc(Index))
http.Handle("/debug/pprof/cmdline", http.HandlerFunc(Cmdline))
http.Handle("/debug/pprof/profile", http.HandlerFunc(Profile))
http.Handle("/debug/pprof/symbol", http.HandlerFunc(Symbol))
}
the handler does not work in my code.
答案1
得分: 10
你将"WriteTimeout"设置为小于配置文件写入时间。
在执行pprof.StartCPUProfile()时,它已经开始写入,请参考:
- http://golang.org/src/pkg/runtime/pprof/pprof.go#L565
- http://golang.org/src/pkg/runtime/pprof/pprof.go#L594
因此,http.WriteTimeout必须大于配置文件写入时间。
对于我的英语不好,我感到很抱歉。
英文:
You set "WriteTimeout" less then profile write time.
on pprof.StartCPUProfile() execute, It already start write, see:
- http://golang.org/src/pkg/runtime/pprof/pprof.go#L565
- http://golang.org/src/pkg/runtime/pprof/pprof.go#L594
So http.WriteTimeout must be greater than profile write time.
Sorry for my poor English.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论