-
Notifications
You must be signed in to change notification settings - Fork 444
/
healthz.go
48 lines (42 loc) · 1.23 KB
/
healthz.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
package probes
import (
"context"
"fmt"
"net/http"
"time"
"github.com/solo-io/go-utils/contextutils"
)
func StartLivenessProbeServer(ctx context.Context) {
var server *http.Server
// Run the server in a goroutine
go func() {
mux := new(http.ServeMux)
mux.HandleFunc("/healthz", okHandler)
server = &http.Server{
Addr: fmt.Sprintf(":%d", 8765),
Handler: mux,
}
contextutils.LoggerFrom(ctx).Infof("healthz server starting at %s", server.Addr)
err := server.ListenAndServe()
if err == http.ErrServerClosed {
contextutils.LoggerFrom(ctx).Info("healthz server closed")
} else {
contextutils.LoggerFrom(ctx).Warnf("healthz server closed with unexpected error: %v", err)
}
}()
// Run a separate goroutine to handle the server shutdown when the context is cancelled
go func() {
<-ctx.Done()
if server != nil {
shutdownCtx, shutdownCancel := context.WithTimeout(context.Background(), 2*time.Second)
defer shutdownCancel()
if err := server.Shutdown(shutdownCtx); err != nil {
contextutils.LoggerFrom(shutdownCtx).Warnf("healthz server shutdown returned error: %v", err)
}
}
}()
}
func okHandler(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, "OK\n")
}