-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathliveness_probe.go
71 lines (58 loc) · 1.57 KB
/
liveness_probe.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
package probes
import (
"fmt"
"net/http"
ws "github.com/gorilla/websocket"
"github.com/itering/substrate-api-rpc/rpc"
"k8s.io/klog/v2"
)
type LivenessProbe struct{}
type ProbeRequest struct {
Name string
Request []byte
}
var livenessProbeRequests []ProbeRequest
func init() {
livenessProbeRequests = []ProbeRequest{
{
Name: "system_health",
Request: rpc.SystemHealth(0),
},
{
Name: "system_chain",
Request: rpc.SystemChain(0),
},
{
Name: "system_properties",
Request: rpc.SystemProperties(0),
},
{
Name: "chain_getBlockHash",
Request: rpc.ChainGetBlockHash(0, 0),
},
}
}
func (p *LivenessProbe) Probe(conn *ws.Conn) (int, error) {
for _, p := range livenessProbeRequests {
if _, err := sendWsRequest(conn, p.Name, p.Request); err != nil {
return http.StatusServiceUnavailable, err
}
}
return http.StatusOK, nil
}
func sendWsRequest(conn *ws.Conn, name string, data []byte) (*rpc.JsonRpcResult, error) {
remoteAddr := conn.RemoteAddr().String()
klog.V(5).Infof("sendWsRequest (%s, %s): %s", remoteAddr, name, data)
v := &rpc.JsonRpcResult{}
if err := conn.WriteMessage(ws.TextMessage, data); err != nil {
return nil, fmt.Errorf("conn.WriteMessage (%s, %s): %w", remoteAddr, name, err)
}
if err := conn.ReadJSON(v); err != nil {
return nil, fmt.Errorf("conn.ReadJSON (%s, %s): %w", remoteAddr, name, err)
}
if v.Error != nil {
return nil, fmt.Errorf("RPC error (%s, %s): %s", remoteAddr, name, v.Error.Message)
}
klog.V(4).Infof("RPC result (%s, %s): %+v", remoteAddr, name, v.Result)
return v, nil
}