/
pprof.go
55 lines (44 loc) · 911 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
53
54
55
package utils
import (
"context"
"net/http"
_ "net/http/pprof"
"time"
"go.uber.org/atomic"
)
var (
pprofStatus atomic.Bool
pprofServer *http.Server
)
// PprofIsRun _
func PprofIsRun() bool {
return pprofStatus.Load()
}
// StartPprof _
func StartPprof(addr string, handler http.Handler) error {
if !pprofStatus.CompareAndSwap(false, true) {
return nil
}
pprofServer = &http.Server{
Handler: handler,
Addr: addr,
}
err := pprofServer.ListenAndServe()
if err != nil {
pprofStatus.Store(false)
}
return err
}
// StopPprof _
func StopPprof(ctx context.Context) error {
if !pprofStatus.CompareAndSwap(true, false) {
return nil
}
return pprofServer.Shutdown(ctx)
}
// StopPprofWithTimeout _
func StopPprofWithTimeout(seconds int64) error {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*time.Duration(seconds))
defer cancel()
return StopPprof(ctx)
}