-
Notifications
You must be signed in to change notification settings - Fork 5.5k
/
pprof.go
52 lines (43 loc) · 980 Bytes
/
pprof.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
package main
import (
"log"
"net/http"
_ "net/http/pprof" //nolint:gosec // Import for pprof, only enabled via CLI flag
"strings"
"time"
)
type Server interface {
Start(string)
ErrChan() <-chan error
}
type PprofServer struct {
err chan error
}
func NewPprofServer() *PprofServer {
return &PprofServer{
err: make(chan error),
}
}
func (p *PprofServer) Start(address string) {
go func() {
pprofHostPort := address
parts := strings.Split(pprofHostPort, ":")
if len(parts) == 2 && parts[0] == "" {
pprofHostPort = "localhost:" + parts[1]
}
pprofHostPort = "http://" + pprofHostPort + "/debug/pprof"
log.Printf("I! Starting pprof HTTP server at: %s", pprofHostPort)
server := &http.Server{
Addr: address,
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
}
if err := server.ListenAndServe(); err != nil {
p.err <- err
}
close(p.err)
}()
}
func (p *PprofServer) ErrChan() <-chan error {
return p.err
}