github.com/msales/pkg/v3@v3.24.0/clix/profiler.go (about) 1 package clix 2 3 import ( 4 "context" 5 "net/http" 6 "net/http/pprof" 7 8 "gopkg.in/urfave/cli.v1" 9 ) 10 11 var profilerServer = &http.Server{} 12 13 // RunProfiler runs a profiler server. 14 func RunProfiler(c *cli.Context) error { 15 if !c.Bool(FlagProfiler) { 16 return nil 17 } 18 19 profilerServer.Handler = newProfilerMux() 20 profilerServer.Addr = ":" + c.String(FlagProfilerPort) 21 22 go func() { 23 err := profilerServer.ListenAndServe() 24 if err != nil && err != http.ErrServerClosed { 25 panic(err) 26 } 27 }() 28 29 return nil 30 } 31 32 // StopProfiler stops a running profiler server. 33 func StopProfiler() error { 34 return profilerServer.Shutdown(context.Background()) 35 } 36 37 func newProfilerMux() http.Handler { 38 mux := &http.ServeMux{} 39 40 mux.HandleFunc("/debug/pprof/", pprof.Index) 41 mux.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline) 42 mux.HandleFunc("/debug/pprof/profile", pprof.Profile) 43 mux.HandleFunc("/debug/pprof/symbol", pprof.Symbol) 44 mux.HandleFunc("/debug/pprof/trace", pprof.Trace) 45 46 return mux 47 }