forked from irinazheltisheva/powergate
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodule.go
More file actions
70 lines (62 loc) · 1.53 KB
/
module.go
File metadata and controls
70 lines (62 loc) · 1.53 KB
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
package health
import (
"context"
"fmt"
"github.com/textileio/powergate/net"
)
// Module exposes the health api.
type Module struct {
net net.Module
}
// Status represents the node's health status.
type Status int
const (
// Unspecified is an unknown or empty status.
Unspecified Status = iota
// Ok specifies the node is healthy
Ok
// Degraded specifies there are problems with the node health.
Degraded
// Error specifies there was an error when determining node health.
Error
)
func (s Status) String() string {
names := [...]string{
"Ok",
"Degraded",
"Error",
}
if s < Ok || s > Error {
return "Unknown"
}
return names[s]
}
// New creates a new net module.
func New(net net.Module) *Module {
m := &Module{
net: net,
}
return m
}
// Check returns the current health status and any messages related to the status.
func (m *Module) Check(ctx context.Context) (status Status, messages []string, err error) {
peers, err := m.net.Peers(ctx)
if err != nil {
return Error, nil, err
}
for _, peer := range peers {
con, err := m.net.Connectedness(ctx, peer.AddrInfo.ID)
if err != nil {
messages = append(messages, fmt.Sprintf("error checking connectedness for peer %v: %v", peer.AddrInfo.ID.String(), err))
continue
}
if con == net.CannotConnect || con == net.Unspecified || con == net.Error {
messages = append(messages, fmt.Sprintf("degraded connectedness %v for peer %v", con, peer.AddrInfo.ID.String()))
}
}
status = Ok
if len(messages) > 0 {
status = Degraded
}
return status, messages, nil
}