golang - net/http/pprof doesn't work

6.3k views Asked by At

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

There are 1 answers

0
Specode On BEST ANSWER

You set "WriteTimeout" less then profile write time.

on pprof.StartCPUProfile() execute, It already start write, see:

So http.WriteTimeout must be greater than profile write time.

Sorry for my poor English.