-
Notifications
You must be signed in to change notification settings - Fork 795
/
modules.go
237 lines (193 loc) · 6.25 KB
/
modules.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
package modules
import (
"fmt"
"sort"
"github.com/go-kit/log"
"github.com/pkg/errors"
"github.com/cortexproject/cortex/pkg/util/services"
)
// module is the basic building block of the application
type module struct {
// dependencies of this module
deps []string
// initFn for this module (can return nil)
initFn func() (services.Service, error)
// is this module user visible (i.e intended to be passed to `InitModuleServices`)
userVisible bool
}
// Manager is a component that initialises modules of the application
// in the right order of dependencies.
type Manager struct {
modules map[string]*module
logger log.Logger
}
// UserInvisibleModule is an option for `RegisterModule` that marks module not visible to user. Modules are user visible by default.
func UserInvisibleModule(m *module) {
m.userVisible = false
}
// NewManager creates a new Manager
func NewManager(logger log.Logger) *Manager {
return &Manager{
modules: make(map[string]*module),
logger: logger,
}
}
// RegisterModule registers a new module with name, init function, and options. Name must
// be unique to avoid overwriting modules. If initFn is nil, the module will not initialise.
// Modules are user visible by default.
func (m *Manager) RegisterModule(name string, initFn func() (services.Service, error), options ...func(option *module)) {
m.modules[name] = &module{
initFn: initFn,
userVisible: true,
}
for _, o := range options {
o(m.modules[name])
}
}
// AddDependency adds a dependency from name(source) to dependsOn(targets)
// An error is returned if the source module name is not found
func (m *Manager) AddDependency(name string, dependsOn ...string) error {
if mod, ok := m.modules[name]; ok {
mod.deps = append(mod.deps, dependsOn...)
} else {
return fmt.Errorf("no such module: %s", name)
}
return nil
}
// InitModuleServices initialises given modules by initialising all their dependencies
// in the right order. Modules are wrapped in such a way that they start after their
// dependencies have been started and stop before their dependencies are stopped.
func (m *Manager) InitModuleServices(modules ...string) (map[string]services.Service, error) {
servicesMap := map[string]services.Service{}
initMap := map[string]bool{}
for _, module := range modules {
if err := m.initModule(module, initMap, servicesMap); err != nil {
return nil, err
}
}
return servicesMap, nil
}
func (m *Manager) initModule(name string, initMap map[string]bool, servicesMap map[string]services.Service) error {
if _, ok := m.modules[name]; !ok {
return fmt.Errorf("unrecognised module name: %s", name)
}
// initialize all of our dependencies first
deps := m.orderedDeps(name)
deps = append(deps, name) // lastly, initialize the requested module
for ix, n := range deps {
// Skip already initialized modules
if initMap[n] {
continue
}
mod := m.modules[n]
var serv services.Service
if mod.initFn != nil {
s, err := mod.initFn()
if err != nil {
return errors.Wrap(err, fmt.Sprintf("error initialising module: %s", n))
}
if s != nil {
// We pass servicesMap, which isn't yet complete. By the time service starts,
// it will be fully built, so there is no need for extra synchronization.
serv = newModuleServiceWrapper(servicesMap, n, m.logger, s, m.DependenciesForModule(n), m.findInverseDependencies(n, deps[ix+1:]))
}
}
if serv != nil {
servicesMap[n] = serv
}
initMap[n] = true
}
return nil
}
// UserVisibleModuleNames gets list of module names that are
// user visible. Returned list is sorted in increasing order.
func (m *Manager) UserVisibleModuleNames() []string {
var result []string
for key, val := range m.modules {
if val.userVisible {
result = append(result, key)
}
}
sort.Strings(result)
return result
}
// IsUserVisibleModule check if given module is public or not. Returns true
// if and only if the given module is registered and is public.
func (m *Manager) IsUserVisibleModule(mod string) bool {
val, ok := m.modules[mod]
if ok {
return val.userVisible
}
return false
}
// IsModuleRegistered checks if the given module has been registered or not. Returns true
// if the module has previously been registered via a call to RegisterModule, false otherwise.
func (m *Manager) IsModuleRegistered(mod string) bool {
_, ok := m.modules[mod]
return ok
}
// listDeps recursively gets a list of dependencies for a passed moduleName
func (m *Manager) listDeps(mod string) []string {
deps := m.modules[mod].deps
for _, d := range m.modules[mod].deps {
deps = append(deps, m.listDeps(d)...)
}
return deps
}
// orderedDeps gets a list of all dependencies ordered so that items are always after any of their dependencies.
func (m *Manager) orderedDeps(mod string) []string {
deps := m.listDeps(mod)
// get a unique list of moduleNames, with a flag for whether they have been added to our result
uniq := map[string]bool{}
for _, dep := range deps {
uniq[dep] = false
}
result := make([]string, 0, len(uniq))
// keep looping through all modules until they have all been added to the result.
for len(result) < len(uniq) {
OUTER:
for name, added := range uniq {
if added {
continue
}
for _, dep := range m.modules[name].deps {
// stop processing this module if one of its dependencies has
// not been added to the result yet.
if !uniq[dep] {
continue OUTER
}
}
// if all of the module's dependencies have been added to the result slice,
// then we can safely add this module to the result slice as well.
uniq[name] = true
result = append(result, name)
}
}
return result
}
// find modules in the supplied list, that depend on mod
func (m *Manager) findInverseDependencies(mod string, mods []string) []string {
result := []string(nil)
for _, n := range mods {
for _, d := range m.modules[n].deps {
if d == mod {
result = append(result, n)
break
}
}
}
return result
}
// DependenciesForModule returns transitive dependencies for given module, sorted by name.
func (m *Manager) DependenciesForModule(module string) []string {
dedup := map[string]bool{}
for _, d := range m.listDeps(module) {
dedup[d] = true
}
result := make([]string, 0, len(dedup))
for d := range dedup {
result = append(result, d)
}
sort.Strings(result)
return result
}