2019-08-21 16:08:55 -04:00
|
|
|
// Package pprof implements a debug endpoint for getting profiles using the
|
2016-09-25 08:39:20 +01:00
|
|
|
// go pprof tooling.
|
2016-04-28 10:26:58 +01:00
|
|
|
package pprof
|
|
|
|
|
|
|
|
|
|
import (
|
2026-01-01 14:55:37 +05:30
|
|
|
"context"
|
2016-04-29 07:28:35 +01:00
|
|
|
"net"
|
2016-04-28 10:26:58 +01:00
|
|
|
"net/http"
|
2016-04-29 07:28:35 +01:00
|
|
|
pp "net/http/pprof"
|
2019-03-29 02:37:17 -04:00
|
|
|
"runtime"
|
2026-01-01 14:55:37 +05:30
|
|
|
"time"
|
2019-11-17 23:58:00 -08:00
|
|
|
|
|
|
|
|
"github.com/coredns/coredns/plugin/pkg/reuseport"
|
2016-04-28 10:26:58 +01:00
|
|
|
)
|
|
|
|
|
|
2016-09-23 09:14:12 +01:00
|
|
|
type handler struct {
|
2019-03-29 02:37:17 -04:00
|
|
|
addr string
|
|
|
|
|
rateBloc int
|
|
|
|
|
ln net.Listener
|
2026-01-01 14:55:37 +05:30
|
|
|
srv *http.Server
|
2019-03-29 02:37:17 -04:00
|
|
|
mux *http.ServeMux
|
2016-04-28 10:26:58 +01:00
|
|
|
}
|
|
|
|
|
|
2026-01-01 14:55:37 +05:30
|
|
|
const shutdownTimeout = 5 * time.Second
|
|
|
|
|
|
2016-09-23 09:14:12 +01:00
|
|
|
func (h *handler) Startup() error {
|
2019-11-17 23:58:00 -08:00
|
|
|
// Reloading the plugin without changing the listening address results
|
|
|
|
|
// in an error unless we reuse the port because Startup is called for
|
|
|
|
|
// new handlers before Shutdown is called for the old ones.
|
|
|
|
|
ln, err := reuseport.Listen("tcp", h.addr)
|
2016-09-21 17:01:19 +01:00
|
|
|
if err != nil {
|
2018-04-19 07:41:56 +01:00
|
|
|
log.Errorf("Failed to start pprof handler: %s", err)
|
2016-04-29 07:28:35 +01:00
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-21 17:01:19 +01:00
|
|
|
h.ln = ln
|
|
|
|
|
|
2016-04-29 07:28:35 +01:00
|
|
|
h.mux = http.NewServeMux()
|
2019-12-07 03:04:49 +08:00
|
|
|
h.mux.HandleFunc(path, func(rw http.ResponseWriter, req *http.Request) {
|
|
|
|
|
http.Redirect(rw, req, path+"/", http.StatusFound)
|
|
|
|
|
})
|
2016-04-29 07:28:35 +01:00
|
|
|
h.mux.HandleFunc(path+"/", pp.Index)
|
|
|
|
|
h.mux.HandleFunc(path+"/cmdline", pp.Cmdline)
|
|
|
|
|
h.mux.HandleFunc(path+"/profile", pp.Profile)
|
|
|
|
|
h.mux.HandleFunc(path+"/symbol", pp.Symbol)
|
|
|
|
|
h.mux.HandleFunc(path+"/trace", pp.Trace)
|
|
|
|
|
|
2019-03-29 02:37:17 -04:00
|
|
|
runtime.SetBlockProfileRate(h.rateBloc)
|
|
|
|
|
|
2026-01-01 14:55:37 +05:30
|
|
|
h.srv = &http.Server{
|
|
|
|
|
Handler: h.mux,
|
|
|
|
|
ReadTimeout: 5 * time.Second,
|
|
|
|
|
WriteTimeout: 5 * time.Second,
|
|
|
|
|
IdleTimeout: 5 * time.Second,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
go func() { h.srv.Serve(h.ln) }()
|
2016-04-28 10:26:58 +01:00
|
|
|
return nil
|
|
|
|
|
}
|
2016-04-29 07:28:35 +01:00
|
|
|
|
2016-09-23 09:14:12 +01:00
|
|
|
func (h *handler) Shutdown() error {
|
2026-01-01 14:55:37 +05:30
|
|
|
if h.srv != nil {
|
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), shutdownTimeout)
|
|
|
|
|
defer cancel()
|
|
|
|
|
if err := h.srv.Shutdown(ctx); err != nil {
|
|
|
|
|
log.Infof("Failed to stop pprof http server: %s", err)
|
|
|
|
|
return err
|
|
|
|
|
}
|
2016-04-29 07:28:35 +01:00
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
path = "/debug/pprof"
|
|
|
|
|
)
|