-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathserver.go
62 lines (50 loc) · 1.45 KB
/
server.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package pprof
import (
"context"
"net/http"
"net/http/pprof"
"runtime"
"time"
"github.com/felixge/fgprof"
"github.com/projectdiscovery/gologger"
"github.com/projectdiscovery/utils/env"
)
const (
PPROFServerAddressENV = "PPROF_SERVER_ADDRESS"
)
type PprofServer struct {
server *http.Server
}
func NewPprofServer() *PprofServer {
address := env.GetEnvOrDefault(PPROFServerAddressENV, "127.0.0.1:8086")
mux := http.NewServeMux()
// Default pprof handlers
mux.HandleFunc("/debug/pprof/", pprof.Index)
mux.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
mux.HandleFunc("/debug/pprof/profile", pprof.Profile)
mux.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
mux.HandleFunc("/debug/pprof/trace", pprof.Trace)
// Also add fgprof for more detailed profiling
mux.Handle("/debug/fgprof", fgprof.Handler())
server := &http.Server{
Addr: address,
Handler: mux,
}
// Enable block and mutex profiling as well
runtime.SetBlockProfileRate(1)
runtime.SetMutexProfileFraction(1)
return &PprofServer{server: server}
}
func (p *PprofServer) Start() {
gologger.Info().Msgf("Listening pprof debug server on: %s", p.server.Addr)
go func() {
if err := p.server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
gologger.Error().Msgf("pprof server failed to start: %s", err)
}
}()
}
func (p *PprofServer) Stop() {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
_ = p.server.Shutdown(ctx)
}