-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
endure.go
274 lines (221 loc) · 5.79 KB
/
endure.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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
package endure
import (
"log/slog"
"net/http"
// pprof will be enabled in debug mode
"net/http/pprof"
"reflect"
"sync"
"time"
"github.com/roadrunner-server/endure/v2/graph"
"github.com/roadrunner-server/endure/v2/logger"
"github.com/roadrunner-server/endure/v2/registar"
"github.com/roadrunner-server/errors"
"go.uber.org/zap"
)
// Endure struct represent main endure repr
type Endure struct {
/// NEW
registar *registar.Registar
///
mu sync.RWMutex
// Dependency graph
graph *graph.Graph
// log
log *zap.Logger
stopTimeout time.Duration
profiler bool
visualize bool
// main thread
handleErrorCh chan *result
userResultsCh chan *Result
}
// Options is the 'endure' options
type Options func(endure *Endure)
// New returns empty endure container
func New(level slog.Leveler, options ...Options) *Endure {
if level == nil {
level = slog.LevelDebug
}
// error handling is omitted because we are sure that the logger will be created
zlog, _ := logger.BuildLogger(level)
c := &Endure{
registar: registar.New(),
graph: graph.New(),
mu: sync.RWMutex{},
stopTimeout: time.Second * 30,
log: zlog.Named("endure"),
}
// Main thread channels
c.handleErrorCh = make(chan *result)
c.userResultsCh = make(chan *Result)
// append options
for _, option := range options {
option(c)
}
// start profiler server
if c.profiler {
profile()
}
return c
}
// Register registers the dependencies in the Endure graph without invoking any methods
func (e *Endure) Register(vertex any) error {
const op = errors.Op("endure_register")
e.mu.Lock()
defer e.mu.Unlock()
t := reflect.TypeOf(vertex)
// t.Kind() - ptr
// t.Elem().Kind() - Struct
if t.Kind() != reflect.Ptr {
return errors.E(op, errors.Register, errors.Errorf("you should pass pointer to the structure instead of value"))
}
/* Collector the type
Information we know at this step is:
1. vertexID
2. Vertex structure value (interface)
And we fill vertex with this information
*/
if e.graph.HasVertex(vertex) {
e.log.Warn("already registered", zap.Error(errors.Errorf("plugin `%s` is already registered", t.String())))
return nil
}
weight := uint(1)
if val, ok := vertex.(Weighted); ok {
weight = val.Weight()
e.log.Debug(
"weight added",
zap.String("type", reflect.TypeOf(vertex).Elem().String()),
zap.String("kind", reflect.TypeOf(vertex).Elem().Kind().String()),
zap.Uint64("value", uint64(weight)),
)
}
// push the vertex
e.graph.AddVertex(vertex, weight)
// add the dependency for the resolver
e.registar.Insert(vertex, reflect.TypeOf(vertex), "", weight)
e.log.Debug(
"type registered",
zap.String("type", reflect.TypeOf(vertex).Elem().String()),
zap.String("kind", reflect.TypeOf(vertex).Elem().Kind().String()),
zap.String("method", "plugin"),
)
/*
Add the types, which (if) current vertex provides
Information we know at this step is:
1. vertexID
2. Vertex structure value (interface)
3. Provided type
4. Provided type String fn
We add 3 and 4 points to the Vertex
*/
if val, ok := vertex.(Provider); ok {
// get types
outDeps := val.Provides()
// iter
for i := 0; i < len(outDeps); i++ {
e.registar.Insert(vertex, outDeps[i].Type, outDeps[i].Method, weight)
e.log.Debug(
"provided type registered",
zap.String("type", outDeps[i].Type.String()),
zap.String("kind", outDeps[i].Type.Kind().String()),
zap.String("method", outDeps[i].Method),
)
}
}
return nil
}
// RegisterAll is the helper for the register to register more than one structure in the endure
func (e *Endure) RegisterAll(plugins ...any) error {
const op = errors.Op("endure_register_all")
for _, plugin := range plugins {
err := e.Register(plugin)
if err != nil {
return errors.E(op, err)
}
}
return nil
}
func (e *Endure) Init() error {
e.mu.Lock()
defer e.mu.Unlock()
const op = errors.Op("endure_initialize")
if len(e.graph.Vertices()) == 0 {
return errors.E(op, errors.Str("no plugins registered"))
}
// traverse the graph
err := e.resolveEdges()
if err != nil {
return errors.E(op, errors.Init, err)
}
if e.visualize {
e.graph.WriteDotString()
}
err = e.init()
if err != nil {
return err
}
err = e.collects()
if err != nil {
return err
}
return nil
}
// Serve used to start serving vertices
// Do not change this method fn, sync with constants in the beginning of this file
func (e *Endure) Serve() (<-chan *Result, error) {
e.mu.Lock()
defer e.mu.Unlock()
e.log.Debug("preparing to serve")
e.startMainThread()
err := e.serve()
if err != nil {
return nil, err
}
e.log.Debug("serving")
return e.userResultsCh, nil
}
// Stop used to shutdown the Endure
// Do not change this method fn, sync with constants in the beginning of this file
func (e *Endure) Stop() error {
e.mu.Lock()
defer e.mu.Unlock()
if len(e.graph.Vertices()) == 0 {
return errors.E(errors.Str("no plugins registered"))
}
e.log.Debug("calling stop")
return e.stop()
}
func (e *Endure) Plugins() []string {
e.mu.RLock()
defer e.mu.RUnlock()
v := e.graph.TopologicalOrder()
plugins := make([]string, 0, len(v))
for i := 0; i < len(v); i++ {
if !v[i].IsActive() {
continue
}
if val, ok := v[i].Plugin().(Named); ok {
plugins = append(plugins, val.Name())
continue
}
plugins = append(plugins, v[i].ID().String())
}
return plugins
}
func profile() {
go func() {
mux := http.NewServeMux()
mux.HandleFunc("/debug/pprof/", pprof.Index)
mux.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
mux.HandleFunc("/debug/pprof/profile", pprof.Profile)
mux.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
mux.HandleFunc("/debug/pprof/trace", pprof.Trace)
srv := &http.Server{
ReadHeaderTimeout: time.Minute * 5,
Handler: mux,
Addr: "0.0.0.0:6061",
}
_ = srv.ListenAndServe()
}()
}