-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdebug_server.go
72 lines (62 loc) · 1.56 KB
/
debug_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
63
64
65
66
67
68
69
70
71
72
package bristle
import (
"context"
"net/http"
_ "net/http/pprof"
"runtime"
"time"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/rs/zerolog/log"
)
type debugServer struct {
closer chan struct{}
config DebuggingConfig
http *http.Server
}
func newDebugServer(config DebuggingConfig) *debugServer {
http := http.Server{
Addr: config.Bind,
}
return &debugServer{closer: make(chan struct{}), config: config, http: &http}
}
func (d *debugServer) Close() {
d.closer <- struct{}{}
<-d.closer
}
func (d *debugServer) Run() {
if d.config.BlockProfileRate != nil {
runtime.SetBlockProfileRate(*d.config.BlockProfileRate)
} else {
runtime.SetBlockProfileRate(0)
}
if d.config.MutexProfileFraction != nil {
runtime.SetMutexProfileFraction(*d.config.MutexProfileFraction)
} else {
runtime.SetMutexProfileFraction(0)
}
if d.config.Metrics {
http.Handle("/metrics", promhttp.Handler())
}
errChan := make(chan error)
go func() {
log.Info().Msg("debug-server: starting http server")
err := d.http.ListenAndServe()
if err != http.ErrServerClosed {
log.Error().Err(err).Msg("debug-server: http server error")
errChan <- err
}
close(errChan)
}()
select {
case err := <-errChan:
log.Error().Err(err).Msg("debug-server: http listener exited")
case <-d.closer:
log.Info().Msg("debug-server: shutdown requested")
shutdownCtx, cancel := context.WithTimeout(context.Background(), time.Second*1)
d.http.Shutdown(shutdownCtx)
cancel()
<-errChan
log.Info().Msg("debug-server: shutdown completed")
close(d.closer)
}
}