-
Notifications
You must be signed in to change notification settings - Fork 0
/
organism.go
81 lines (65 loc) · 1.4 KB
/
organism.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
package organism
// New create new organism
func New() *Organism {
o := &Organism{}
o.core = o.GrowLimb()
return o
}
// Organism is your application, it has core and limbs. Each Limb describes important part of your app, and if this part
// is dead, then organism is partially dead too (liveness probe). If each Limb of Organism, and the core too, are ready
// to work, then whole Organism ready too work too (readiness probe)
type Organism struct {
limbs []*Limb
core *Limb
}
// GrowLimb grow new Limb for Organism and returns it
func (o *Organism) GrowLimb() *Limb {
limb := newLimb()
o.limbs = append(o.limbs, limb)
return limb
}
func (o *Organism) IsReady() bool {
for _, l := range o.limbs {
if !l.IsReady() {
return false
}
}
return true
}
func (o *Organism) IsAlive() bool {
for _, l := range o.limbs {
if !l.IsAlive() {
return false
}
}
return true
}
// Ready marks core of Organism as ready
func (o *Organism) Ready() {
o.core.Ready()
}
// Ready marks core of Organism as dead
func (o *Organism) Die() {
o.core.Die()
}
func newLimb() *Limb {
return &Limb{isAlive: true}
}
type Limb struct {
isReady bool
isAlive bool
}
// Ready marks Limb as ready
func (l *Limb) Ready() {
l.isReady = true
}
// Die marks Limb as dead (not alive)
func (l *Limb) Die() {
l.isAlive = false
}
func (l *Limb) IsReady() bool {
return l.isReady
}
func (l *Limb) IsAlive() bool {
return l.isAlive
}