-
Notifications
You must be signed in to change notification settings - Fork 0
/
health.go
98 lines (92 loc) · 2.5 KB
/
health.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package pluto
import (
"net/http"
"strings"
"github.com/aukbit/pluto/v6/client"
"github.com/aukbit/pluto/v6/reply"
"github.com/aukbit/pluto/v6/server"
"github.com/aukbit/pluto/v6/server/router"
healthpb "google.golang.org/grpc/health/grpc_health_v1"
)
func readyHealthHandler(w http.ResponseWriter, r *http.Request) {
var hcr = &healthpb.HealthCheckResponse{Status: 0}
ctx := r.Context()
s := FromContext(ctx)
if len(s.cfg.Servers) == 0 {
reply.Json(w, r, http.StatusServiceUnavailable, hcr)
return
}
// Test all servers
for _, srv := range s.cfg.Servers {
hcr = srv.Health()
if hcr.Status.String() != healthpb.HealthCheckResponse_SERVING.String() {
reply.Json(w, r, http.StatusServiceUnavailable, hcr)
return
}
}
reply.Json(w, r, http.StatusOK, hcr)
}
func liveHealthHandler(w http.ResponseWriter, r *http.Request) {
var hcr = &healthpb.HealthCheckResponse{Status: 0}
ctx := r.Context()
s := FromContext(ctx)
// Test all servers
for _, srv := range s.cfg.Servers {
hcr = srv.Health()
if hcr.Status.String() != healthpb.HealthCheckResponse_SERVING.String() {
reply.Json(w, r, http.StatusServiceUnavailable, hcr)
return
}
}
// Test all clients
for _, clt := range s.cfg.Clients {
hcr = clt.Health()
if hcr.Status.String() != healthpb.HealthCheckResponse_SERVING.String() {
reply.Json(w, r, http.StatusServiceUnavailable, hcr)
return
}
}
// Test service
hcr = s.Health()
if hcr.Status.String() != healthpb.HealthCheckResponse_SERVING.String() {
reply.Json(w, r, http.StatusServiceUnavailable, hcr)
return
}
reply.Json(w, r, http.StatusOK, hcr)
}
func healthHandler(w http.ResponseWriter, r *http.Request) {
var hcr = &healthpb.HealthCheckResponse{Status: 0}
ctx := r.Context()
m := router.FromContext(ctx, "module")
n := router.FromContext(ctx, "name")
s := FromContext(ctx)
switch m {
case "server":
name := strings.Replace(n, "_"+server.DefaultName, "", 1)
srv, ok := s.Server(name)
if !ok {
reply.Json(w, r, http.StatusNotFound, hcr)
return
}
hcr = srv.Health()
case "client":
name := strings.Replace(n, "_"+client.DefaultName, "", 1)
clt, ok := s.Client(name)
if !ok {
reply.Json(w, r, http.StatusNotFound, hcr)
return
}
hcr = clt.Health()
case "pluto":
if n != s.Name() {
reply.Json(w, r, http.StatusNotFound, hcr)
return
}
hcr = s.Health()
}
if hcr.Status.String() != healthpb.HealthCheckResponse_SERVING.String() {
reply.Json(w, r, http.StatusTooManyRequests, hcr)
return
}
reply.Json(w, r, http.StatusOK, hcr)
}