/
pprof_sidecar.go
62 lines (49 loc) · 1.25 KB
/
pprof_sidecar.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 pprofsidecar
import (
"context"
"net/http"
"os"
"os/signal"
"time"
"golang.org/x/sync/errgroup"
)
// Run starts an errgroup.Group that runs a http server for pprof along with whatever
// function/service is supposed to run
//
// This should not be used for http servers
func Run(ctx context.Context, srvAddr string, interrupt chan os.Signal, run func(context.Context) error) error {
if interrupt == nil {
interrupt = make(chan os.Signal)
defer close(interrupt)
signal.Notify(interrupt, os.Interrupt)
}
srv := &http.Server{Addr: srvAddr} // the pprof debug server (make sure to import pprof in main)
ctx, cancel := context.WithCancel(ctx)
defer cancel()
g, ctx := errgroup.WithContext(ctx)
// watches for interrupts
g.Go(func() error {
select {
case <-interrupt:
cancel()
return nil
case <-ctx.Done():
return ctx.Err()
}
})
// runs the function
g.Go(func() error {
defer cancel()
return run(ctx)
})
// runs the pprof server
g.Go(srv.ListenAndServe)
// kills the pprof server when necessary
g.Go(func() error {
<-ctx.Done() // something said we are done
shutdownCtx, cncl := context.WithTimeout(context.Background(), 2*time.Second)
defer cncl()
return srv.Shutdown(shutdownCtx)
})
return g.Wait()
}