github.com/ethersphere/bee/v2@v2.2.0/pkg/api/probe.go (about) 1 // Copyright 2022 The Swarm Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package api 6 7 import "sync/atomic" 8 9 // ProbeStatus is the status of a probe. 10 // ProbeStatus is treated as a sync/atomic int32. 11 type ProbeStatus int32 12 13 // get returns the value of the ProbeStatus. 14 func (ps *ProbeStatus) get() ProbeStatus { 15 return ProbeStatus(atomic.LoadInt32((*int32)(ps))) 16 } 17 18 // set updates the value of the ProbeStatus. 19 func (ps *ProbeStatus) set(v ProbeStatus) { 20 atomic.StoreInt32((*int32)(ps), int32(v)) 21 } 22 23 // String implements the fmt.Stringer interface. 24 func (ps ProbeStatus) String() string { 25 switch ps.get() { 26 case ProbeStatusOK: 27 return "ok" 28 case ProbeStatusNOK: 29 return "nok" 30 } 31 return "unknown" 32 } 33 34 const ( 35 // ProbeStatusOK indicates positive ProbeStatus status. 36 ProbeStatusOK ProbeStatus = 1 37 38 // ProbeStatusNOK indicates negative ProbeStatus status. 39 ProbeStatusNOK ProbeStatus = 0 40 ) 41 42 // Probe structure holds flags which indicate node healthiness (sometimes refert also as liveness) and readiness. 43 type Probe struct { 44 // Healthy probe indicates if node, due to any reason, needs to restarted. 45 healthy ProbeStatus 46 // Ready probe indicates that node is ready to start accepting traffic. 47 ready ProbeStatus 48 } 49 50 // NewProbe returns new Probe. 51 func NewProbe() *Probe { 52 return &Probe{} 53 } 54 55 // Healthy returns the value of the healthy status. 56 func (p *Probe) Healthy() ProbeStatus { 57 if p == nil { 58 return ProbeStatusNOK 59 } 60 return p.healthy.get() 61 62 } 63 64 // SetHealthy updates the value of the healthy status. 65 func (p *Probe) SetHealthy(ps ProbeStatus) { 66 p.healthy.set(ps) 67 } 68 69 // Ready returns the value of the ready status. 70 func (p *Probe) Ready() ProbeStatus { 71 if p == nil { 72 return ProbeStatusNOK 73 } 74 return p.ready.get() 75 } 76 77 // SetReady updates the value of the ready status. 78 func (p *Probe) SetReady(ps ProbeStatus) { 79 p.ready.set(ps) 80 }