pprof_debug.go 954 B

123456789101112131415161718192021222324252627282930313233
  1. //go:build debug
  2. package nebula
  3. import (
  4. "context"
  5. "errors"
  6. "log/slog"
  7. "net/http"
  8. _ "net/http/pprof" // registers pprof handlers on http.DefaultServeMux
  9. )
  10. // startPprofServer serves net/http/pprof on :6060 for the life of ctx. It is
  11. // only compiled into debug builds (`-tags debug`, `make debug`), so a debug
  12. // build announces itself with the Info line below.
  13. func startPprofServer(ctx context.Context, l *slog.Logger) {
  14. server := &http.Server{Addr: ":6060", Handler: nil}
  15. l.Info("Starting pprof debug server (debug build)", "addr", server.Addr)
  16. go func() {
  17. if err := server.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
  18. l.Error("pprof debug server stopped", "error", err)
  19. }
  20. }()
  21. // Shut down the server when the context is cancelled.
  22. go func() {
  23. <-ctx.Done()
  24. if err := server.Shutdown(context.Background()); err != nil {
  25. l.Debug("Error shutting down pprof debug server", "error", err)
  26. }
  27. }()
  28. }