forked from deislabs/osiris
-
Notifications
You must be signed in to change notification settings - Fork 8
/
server.go
51 lines (44 loc) · 1.23 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
package healthz
import (
"context"
"fmt"
"net/http"
"time"
"net/http/pprof"
"github.com/golang/glog"
)
func RunServer(ctx context.Context, port int) {
mux := http.NewServeMux()
mux.HandleFunc("/healthz", HandleHealthCheckRequest)
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)
srv := &http.Server{
Addr: fmt.Sprintf(":%d", port),
Handler: mux,
}
doneCh := make(chan struct{})
go func() {
select {
case <-ctx.Done(): // Context was canceled or expired
glog.Info("Healthz server is shutting down")
// Allow up to five seconds for requests in progress to be completed
shutdownCtx, cancel := context.WithTimeout(
context.Background(),
time.Second*5,
)
defer cancel()
srv.Shutdown(shutdownCtx) // nolint: errcheck
case <-doneCh: // The server shut down on its own, perhaps due to error
}
}()
glog.Infof("Healthz server is listening on %s", srv.Addr)
err := srv.ListenAndServe()
if err != http.ErrServerClosed {
glog.Errorf("Healthz server error: %s", err)
err = nil
}
close(doneCh)
}