-
Notifications
You must be signed in to change notification settings - Fork 17
/
state.go
128 lines (99 loc) · 2.23 KB
/
state.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package state
import (
"sync"
"github.com/luizbafilho/fusis/types"
)
//go:generate mockery -name=State
type Services []types.Service
type Destinations map[string]types.Destination
type Checks []types.CheckSpec
type State interface {
GetServices() []types.Service
AddService(svc types.Service)
// SetDestinations(dsts Destinations)
GetDestinations(svc *types.Service) []types.Destination
AddDestination(dst types.Destination)
DeleteDestination(dst types.Destination)
AddCheck(c types.CheckSpec)
GetChecks() []types.CheckSpec
SetChecks(checks Checks)
Copy() State
}
// State...
type FusisState struct {
sync.RWMutex
services Services
destinations Destinations
checks Checks
}
// New creates a new Engine
func New() (State, error) {
state := &FusisState{
services: Services{},
destinations: Destinations{},
}
return state, nil
}
func (s *FusisState) Copy() State {
s.RLock()
defer s.RUnlock()
new := &FusisState{
services: Services{},
destinations: Destinations{},
}
copy(new.services, s.services)
for _, dst := range s.destinations {
new.AddDestination(dst)
}
return new
}
func (s *FusisState) AddCheck(check types.CheckSpec) {
s.Lock()
defer s.Unlock()
s.checks = append(s.checks, check)
}
func (s *FusisState) GetChecks() []types.CheckSpec {
s.RLock()
defer s.RUnlock()
return s.checks
}
func (s *FusisState) SetChecks(checks Checks) {
s.Lock()
defer s.Unlock()
s.checks = checks
}
func (s *FusisState) GetServices() []types.Service {
s.RLock()
defer s.RUnlock()
return s.services
}
func (s *FusisState) AddService(svc types.Service) {
s.Lock()
defer s.Unlock()
s.services = append(s.services, svc)
}
func (s *FusisState) GetDestinations(svc *types.Service) []types.Destination {
s.RLock()
defer s.RUnlock()
dsts := []types.Destination{}
if svc == nil {
for _, d := range s.destinations {
dsts = append(dsts, d)
}
return dsts
}
for _, d := range s.destinations {
if d.ServiceId == svc.GetId() {
dsts = append(dsts, d)
}
}
return dsts
}
func (s *FusisState) AddDestination(dst types.Destination) {
s.Lock()
defer s.Unlock()
s.destinations[dst.GetId()] = dst
}
func (s *FusisState) DeleteDestination(dst types.Destination) {
delete(s.destinations, dst.GetId())
}