profiler.go raw
1 package service
2
3 import (
4 "context"
5
6 "errors"
7 "net/http"
8 "net/http/pprof"
9 )
10
11 func startProfiler(ctx context.Context, addr string) {
12 mux := http.NewServeMux()
13 mux.HandleFunc("/debug/pprof/", pprof.Index)
14 mux.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
15 mux.HandleFunc("/debug/pprof/profile", pprof.Profile)
16 mux.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
17 mux.HandleFunc("/debug/pprof/trace", pprof.Trace)
18
19 server := &http.Server{
20 Addr: addr,
21 Handler: mux,
22 }
23
24 go func() {
25 <-ctx.Done()
26 err := server.Shutdown(context.Background())
27 if err != nil {
28 panic("pprof server shutdown failed: " + err.Error())
29 }
30 }()
31
32 go func() {
33 err := server.ListenAndServe()
34 if err != nil && !errors.Is(err, http.ErrServerClosed) {
35 panic("pprof server failed: " + err.Error())
36 }
37 }()
38 }
39