forked from ethersphere/bee
/
probe.go
80 lines (65 loc) · 1.89 KB
/
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
72
73
74
75
76
77
78
79
80
// Copyright 2022 The Swarm Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package api
import "sync/atomic"
// ProbeStatus is the status of a probe.
// ProbeStatus is treated as a sync/atomic int32.
type ProbeStatus int32
// get returns the value of the ProbeStatus.
func (ps *ProbeStatus) get() ProbeStatus {
return ProbeStatus(atomic.LoadInt32((*int32)(ps)))
}
// set updates the value of the ProbeStatus.
func (ps *ProbeStatus) set(v ProbeStatus) {
atomic.StoreInt32((*int32)(ps), int32(v))
}
// String implements the fmt.Stringer interface.
func (ps ProbeStatus) String() string {
switch ps.get() {
case ProbeStatusOK:
return "ok"
case ProbeStatusNOK:
return "nok"
}
return "unknown"
}
const (
// ProbeStatusOK indicates positive ProbeStatus status.
ProbeStatusOK ProbeStatus = 1
// ProbeStatusNOK indicates negative ProbeStatus status.
ProbeStatusNOK ProbeStatus = 0
)
// Probe structure holds flags which indicate node healthiness (sometimes refert also as liveness) and readiness.
type Probe struct {
// Healthy probe indicates if node, due to any reason, needs to restarted.
healthy ProbeStatus
// Ready probe indicates that node is ready to start accepting traffic.
ready ProbeStatus
}
// NewProbe returns new Probe.
func NewProbe() *Probe {
return &Probe{}
}
// Healthy returns the value of the healthy status.
func (p *Probe) Healthy() ProbeStatus {
if p == nil {
return ProbeStatusNOK
}
return p.healthy.get()
}
// SetHealthy updates the value of the healthy status.
func (p *Probe) SetHealthy(ps ProbeStatus) {
p.healthy.set(ps)
}
// Ready returns the value of the ready status.
func (p *Probe) Ready() ProbeStatus {
if p == nil {
return ProbeStatusNOK
}
return p.ready.get()
}
// SetReady updates the value of the ready status.
func (p *Probe) SetReady(ps ProbeStatus) {
p.ready.set(ps)
}